log.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. }