log.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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::format($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 format($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. $message = date('Y-m-d H:i:s').' '.Str::upper($type)." - {$message}".PHP_EOL;
  41. File::append($GLOBALS['STORAGE_PATH'].'logs/'.date('Y-m-d').'.log', $message);
  42. }
  43. /**
  44. * Dynamically write a log message.
  45. *
  46. * <code>
  47. * // Write an "error" message to the log file
  48. * Log::error('This is an error!');
  49. *
  50. * // Write a "warning" message to the log file
  51. * Log::warning('This is a warning!');
  52. * </code>
  53. */
  54. public static function __callStatic($method, $parameters)
  55. {
  56. static::write($method, $parameters[0]);
  57. }
  58. }