PageRenderTime 36ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/laravel/laravel/log.php

https://bitbucket.org/awisalkarni/awatt-php
PHP | 99 lines | 30 code | 11 blank | 58 comment | 1 complexity | 667aa5cb205ea581e6fe20acb28a9ff1 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. *
  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. $message = static::format($type, $message);
  54. File::append(path('storage').'logs/'.date('Y-m-d').'.log', $message);
  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. *
  77. * // Log an arrays data
  78. * Log::info(array('name' => 'Sawny', 'passwd' => '1234', array(1337, 21, 0)), true);
  79. * //Result: Array ( [name] => Sawny [passwd] => 1234 [0] => Array ( [0] => 1337 [1] => 21 [2] => 0 ) )
  80. * //If we had omit the second parameter the result had been: Array
  81. * </code>
  82. */
  83. public static function __callStatic($method, $parameters)
  84. {
  85. $parameters[1] = (empty($parameters[1])) ? false : $parameters[1];
  86. static::write($method, $parameters[0], $parameters[1]);
  87. }
  88. }