From fadadd0f62968826ff56f4ecd5282a87b2c3365f Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Thu, 23 Feb 2012 14:50:07 -0600 Subject: [PATCH] allow events to override the log class. --- laravel/log.php | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/laravel/log.php b/laravel/log.php index 2d6bf49d..4cd3a30e 100644 --- a/laravel/log.php +++ b/laravel/log.php @@ -10,7 +10,7 @@ class Log { */ public static function exception($e) { - static::write('error', static::format($e)); + static::write('error', static::exception_line($e)); } /** @@ -19,7 +19,7 @@ public static function exception($e) * @param Exception $e * @return string */ - protected static function format($e) + protected static function exception_line($e) { return $e->getMessage().' in '.$e->getFile().' on line '.$e->getLine(); } @@ -41,9 +41,34 @@ protected static function format($e) */ public static function write($type, $message) { - $message = date('Y-m-d H:i:s').' '.Str::upper($type)." - {$message}".PHP_EOL; + // If there is a listener for the log event, we'll delegate the logging + // to the event and not write to the log files. This allows for quick + // swapping of log implementations for debugging. + if (Event::listeners('laravel.log')) + { + Event::fire('laravel.log', array($type, $message)); + } - File::append(path('storage').'logs/'.date('Y-m-d').'.log', $message); + // If there aren't listeners on the log event, we'll just write to the + // log files using the default conventions, writing one log file per + // day so they files don't get too crowded. + else + { + $message = static::format($type, $message); + + File::append(path('storage').'logs/'.date('Y-m-d').'.log', $message); + } + } + + /** + * Format a log message for logging. + * + * @param string $type + * @param + */ + protected static function format($type, $message) + { + return date('Y-m-d H:i:s').' '.Str::upper($type)." - {$message}".PHP_EOL; } /**