log.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. *
  33. * // Log an arrays data
  34. * Log::write('info', array('name' => 'Sawny', 'passwd' => '1234', array(1337, 21, 0)), true);
  35. * //Result: Array ( [name] => Sawny [passwd] => 1234 [0] => Array ( [0] => 1337 [1] => 21 [2] => 0 ) )
  36. * //If we had omit the third parameter the result had been: Array
  37. * </code>
  38. *
  39. * @param string $type
  40. * @param string $message
  41. * @return void
  42. */
  43. public static function write($type, $message, $pretty_print = false)
  44. {
  45. $message = ($pretty_print) ? print_r($message, true) : $message;
  46. // If there is a listener for the log event, we'll delegate the logging
  47. // to the event and not write to the log files. This allows for quick
  48. // swapping of log implementations for debugging.
  49. if (Event::listeners('laravel.log'))
  50. {
  51. Event::fire('laravel.log', array($type, $message));
  52. }
  53. $trace=debug_backtrace();
  54. foreach($trace as $item)
  55. {
  56. if (isset($item['class']) AND $item['class'] == __CLASS__)
  57. {
  58. continue;
  59. }
  60. $caller = $item;
  61. break;
  62. }
  63. $function = $caller['function'];
  64. if (isset($caller['class']))
  65. {
  66. $class = $caller['class'] . '::';
  67. }
  68. else
  69. {
  70. $class = '';
  71. }
  72. $message = static::format($type, $class . $function . ' - ' . $message);
  73. File::append(path('storage').'logs/'.date('Y-m-d').'.log', $message);
  74. }
  75. /**
  76. * Format a log message for logging.
  77. *
  78. * @param string $type
  79. * @param string $message
  80. * @return string
  81. */
  82. protected static function format($type, $message)
  83. {
  84. return date('Y-m-d H:i:s').' '.Str::upper($type)." - {$message}".PHP_EOL;
  85. }
  86. /**
  87. * Dynamically write a log message.
  88. *
  89. * <code>
  90. * // Write an "error" message to the log file
  91. * Log::error('This is an error!');
  92. *
  93. * // Write a "warning" message to the log file
  94. * Log::warning('This is a warning!');
  95. *
  96. * // Log an arrays data
  97. * Log::info(array('name' => 'Sawny', 'passwd' => '1234', array(1337, 21, 0)), true);
  98. * //Result: Array ( [name] => Sawny [passwd] => 1234 [0] => Array ( [0] => 1337 [1] => 21 [2] => 0 ) )
  99. * //If we had omit the second parameter the result had been: Array
  100. * </code>
  101. */
  102. public static function __callStatic($method, $parameters)
  103. {
  104. $parameters[1] = (empty($parameters[1])) ? false : $parameters[1];
  105. static::write($method, $parameters[0], $parameters[1]);
  106. }
  107. }