PageRenderTime 59ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

/laravel/log.php

https://bitbucket.org/joostory/rest-laravel
PHP | 85 lines | 28 code | 9 blank | 48 comment | 1 complexity | 527727e6b5eaf1467ed3679aaf69ac2b MD5 | raw file
  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. $message = static::format($type, $message);
  48. File::append(path('storage').'logs/'.date('Y-m-d').'.log', $message);
  49. }
  50. /**
  51. * Format a log message for logging.
  52. *
  53. * @param string $type
  54. * @param string $message
  55. * @return string
  56. */
  57. protected static function format($type, $message)
  58. {
  59. return date('Y-m-d H:i:s').' '.Str::upper($type)." - {$message}".PHP_EOL;
  60. }
  61. /**
  62. * Dynamically write a log message.
  63. *
  64. * <code>
  65. * // Write an "error" message to the log file
  66. * Log::error('This is an error!');
  67. *
  68. * // Write a "warning" message to the log file
  69. * Log::warning('This is a warning!');
  70. * </code>
  71. */
  72. public static function __callStatic($method, $parameters)
  73. {
  74. static::write($method, $parameters[0]);
  75. }
  76. }