From 41ba44b7299da4c434be02e1178ebccbcd8085da Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Sun, 20 Jan 2013 09:00:22 -0500 Subject: [PATCH] adjusted logger event. --- framework/logging/Logger.php | 22 ++++++++++++++++++---- framework/logging/Router.php | 12 +++++------- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/framework/logging/Logger.php b/framework/logging/Logger.php index e1f809f..2659ea7 100644 --- a/framework/logging/Logger.php +++ b/framework/logging/Logger.php @@ -25,9 +25,13 @@ use yii\base\Exception; class Logger extends \yii\base\Component { /** - * @event Event an event that is triggered when messages are being flushed from memory to targets + * @event Event an event that is triggered when [[flush()]] is called. */ const EVENT_FLUSH = 'flush'; + /** + * @event Event an event that is triggered when [[flush()]] is called at the end of application. + */ + const EVENT_FINAL_FLUSH = 'finalFlush'; /** * Error message level. An error message is one that indicates the abnormal termination of the @@ -88,6 +92,15 @@ class Logger extends \yii\base\Component public $messages = array(); /** + * Initializes the logger by registering [[flush()]] as a shutdown function. + */ + public function init() + { + parent::init(); + register_shutdown_function(array($this, 'flush'), true); + } + + /** * Logs a message with the given type and category. * If `YII_DEBUG` is true and `YII_TRACE_LEVEL` is greater than 0, then additional * call stack information about application code will be appended to the message. @@ -120,11 +133,12 @@ class Logger extends \yii\base\Component /** * Flushes log messages from memory to targets. - * This method will trigger a [[flush]] event. + * This method will trigger a [[flush]] or [[finalFlush]] event depending on the $final value. + * @param boolean $final whether this is a final call during a request. */ - public function flush() + public function flush($final = false) { - $this->trigger('flush'); + $this->trigger($final ? 'finalFlush' : 'flush'); $this->messages = array(); } diff --git a/framework/logging/Router.php b/framework/logging/Router.php index aaac8f8..017135f 100644 --- a/framework/logging/Router.php +++ b/framework/logging/Router.php @@ -83,23 +83,21 @@ class Router extends Component } Yii::getLogger()->on(Logger::EVENT_FLUSH, array($this, 'processMessages')); - if (Yii::$application !== null) { - Yii::$application->on(Application::EVENT_AFTER_REQUEST, array($this, 'processMessages')); - } + Yii::getLogger()->on(Logger::EVENT_FINAL_FLUSH, array($this, 'processMessages')); } /** * Retrieves and processes log messages from the system logger. - * This method mainly serves the event handler to [[Logger::onFlush]] - * and [[Application::onEndRequest]] events. - * It will retrieve the available log messages from the [[Yii::getLogger|system logger]] + * This method mainly serves the event handler to [[Logger::flush]] + * and [[Application::endRequest]] events. + * It will retrieve the available log messages from the [[Yii::getLogger()|system logger]] * and invoke the registered [[targets|log targets]] to do the actual processing. * @param \yii\base\Event $event event parameter */ public function processMessages($event) { $messages = Yii::getLogger()->messages; - $final = $event->name === Application::EVENT_AFTER_REQUEST; + $final = $event->name === Logger::EVENT_FINAL_FLUSH; foreach ($this->targets as $target) { if ($target->enabled) { $target->processMessages($messages, $final);