log.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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" message 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 the 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 string $message
  61. * @return string
  62. */
  63. protected static function format($type, $message)
  64. {
  65. return date('Y-m-d H:i:s').' '.Str::upper($type)." - {$message}".PHP_EOL;
  66. }
  67. /**
  68. * Dynamically write a log message.
  69. *
  70. * <code>
  71. * // Write an "error" message to the log file
  72. * Log::error('This is an error!');
  73. *
  74. * // Write a "warning" message to the log file
  75. * Log::warning('This is a warning!');
  76. * </code>
  77. */
  78. public static function __callStatic($method, $parameters)
  79. {
  80. static::write($method, $parameters[0]);
  81. }
  82. }