Commit 41ba44b7 by Qiang Xue

adjusted logger event.

parent fb75c958
...@@ -25,9 +25,13 @@ use yii\base\Exception; ...@@ -25,9 +25,13 @@ use yii\base\Exception;
class Logger extends \yii\base\Component 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'; 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 * 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 ...@@ -88,6 +92,15 @@ class Logger extends \yii\base\Component
public $messages = array(); 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. * Logs a message with the given type and category.
* If `YII_DEBUG` is true and `YII_TRACE_LEVEL` is greater than 0, then additional * 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. * call stack information about application code will be appended to the message.
...@@ -120,11 +133,12 @@ class Logger extends \yii\base\Component ...@@ -120,11 +133,12 @@ class Logger extends \yii\base\Component
/** /**
* Flushes log messages from memory to targets. * 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(); $this->messages = array();
} }
......
...@@ -83,23 +83,21 @@ class Router extends Component ...@@ -83,23 +83,21 @@ class Router extends Component
} }
Yii::getLogger()->on(Logger::EVENT_FLUSH, array($this, 'processMessages')); Yii::getLogger()->on(Logger::EVENT_FLUSH, array($this, 'processMessages'));
if (Yii::$application !== null) { Yii::getLogger()->on(Logger::EVENT_FINAL_FLUSH, array($this, 'processMessages'));
Yii::$application->on(Application::EVENT_AFTER_REQUEST, array($this, 'processMessages'));
}
} }
/** /**
* Retrieves and processes log messages from the system logger. * Retrieves and processes log messages from the system logger.
* This method mainly serves the event handler to [[Logger::onFlush]] * This method mainly serves the event handler to [[Logger::flush]]
* and [[Application::onEndRequest]] events. * and [[Application::endRequest]] events.
* It will retrieve the available log messages from the [[Yii::getLogger|system logger]] * 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. * and invoke the registered [[targets|log targets]] to do the actual processing.
* @param \yii\base\Event $event event parameter * @param \yii\base\Event $event event parameter
*/ */
public function processMessages($event) public function processMessages($event)
{ {
$messages = Yii::getLogger()->messages; $messages = Yii::getLogger()->messages;
$final = $event->name === Application::EVENT_AFTER_REQUEST; $final = $event->name === Logger::EVENT_FINAL_FLUSH;
foreach ($this->targets as $target) { foreach ($this->targets as $target) {
if ($target->enabled) { if ($target->enabled) {
$target->processMessages($messages, $final); $target->processMessages($messages, $final);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment