PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/laravel/log.php

https://bitbucket.org/khrrsn/kutting-edge-2
PHP | 90 lines | 31 code | 9 blank | 50 comment | 1 complexity | 36f0352502918bbbfb61eaa401338163 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php namespace Laravel;
  2. class Log {
  3. /**
  4. * Log an exception to the log file.
  5. *
  6. * @param Exception $e
  7. * @return void
  8. */
  9. public static function exception($e)
  10. {
  11. static::write('error', static::exception_line($e));
  12. }
  13. /**
  14. * Format a log friendly message from the given exception.
  15. *
  16. * @param Exception $e
  17. * @return string
  18. */
  19. protected static function exception_line($e)
  20. {
  21. return $e->getMessage().' in '.$e->getFile().' on line '.$e->getLine();
  22. }
  23. /**
  24. * Write a message to the log file.
  25. *
  26. * <code>
  27. * // Write an "error" messge to the log file
  28. * Log::write('error', 'Something went horribly wrong!');
  29. *
  30. * // Write an "error" message using the class' magic method
  31. * Log::error('Something went horribly wrong!');
  32. * </code>
  33. *
  34. * @param string $type
  35. * @param string $message
  36. * @return void
  37. */
  38. public static function write($type, $message)
  39. {
  40. // If there is a listener for the log event, we'll delegate the logging
  41. // to the event and not write to the log files. This allows for quick
  42. // swapping of log implementations for debugging.
  43. if (Event::listeners('laravel.log'))
  44. {
  45. Event::fire('laravel.log', array($type, $message));
  46. }
  47. // If there aren't listeners on the log event, we'll just write to the
  48. // log files using the default conventions, writing one log file per
  49. // day so they files don't get too crowded.
  50. else
  51. {
  52. $message = static::format($type, $message);
  53. File::append(path('storage').'logs/'.date('Y-m-d').'.log', $message);
  54. }
  55. }
  56. /**
  57. * Format a log message for logging.
  58. *
  59. * @param string $type
  60. * @param
  61. */
  62. protected static function format($type, $message)
  63. {
  64. return date('Y-m-d H:i:s').' '.Str::upper($type)." - {$message}".PHP_EOL;
  65. }
  66. /**
  67. * Dynamically write a log message.
  68. *
  69. * <code>
  70. * // Write an "error" message to the log file
  71. * Log::error('This is an error!');
  72. *
  73. * // Write a "warning" message to the log file
  74. * Log::warning('This is a warning!');
  75. * </code>
  76. */
  77. public static function __callStatic($method, $parameters)
  78. {
  79. static::write($method, $parameters[0]);
  80. }
  81. }