Yii2 Bootstrap 3
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
3.3 KiB

14 years ago
<?php
/**
* Router class file.
*
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\logging;
/**
* Router manages [[Target|log targets]] that record log messages in different media.
*
* For example, a [[FileTarget|file log target]] records log messages
* in files; an [[EmailTarget|email log target]] sends log messages
* to specific email addresses. Each log target may specify filters on
* message levels and categories to record specific messages only.
*
* Router and the targets it manages may be configured in application configuration,
* like the following:
*
* ~~~
* array(
* // preload log component when application starts
* 'preload' => array('log'),
* 'components' => array(
* 'log' => array(
* 'class' => '\yii\logging\Router',
* 'targets' => array(
* 'file' => array(
* 'class' => '\yii\logging\FileTarget',
* 'levels' => 'trace, info',
* 'categories' => 'yii\*',
* ),
* 'email' => array(
* 'class' => '\yii\logging\EmailTarget',
* 'levels' => 'error, warning',
* 'emails' => array('admin@example.com'),
* ),
* ),
* ),
* ),
* )
* ~~~
*
* Each log target can have a name and can be referenced via the [[targets]] property
* as follows:
*
* ~~~
13 years ago
* \Yii::$application->log->targets['file']->enabled = false;
14 years ago
* ~~~
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class Router extends \yii\base\ApplicationComponent
{
/**
13 years ago
* @var Target[] list of log target objects or configurations. If the latter, target objects will
* be created in [[init()]] by calling [[\Yii::createObject()]] with the corresponding object configuration.
*/
13 years ago
public $targets = array();
14 years ago
/**
* Initializes this application component.
* This method is invoked when the Router component is created by the application.
* The method attaches the [[processLogs]] method to both the [[Logger::flush]] event
* and the [[\yii\base\Application::afterRequest]] event.
14 years ago
*/
public function init()
{
parent::init();
13 years ago
foreach ($this->targets as $name => $target) {
if (!$target instanceof Target) {
$this->targets[$name] = \Yii::createObject($target);
14 years ago
}
}
13 years ago
\Yii::getLogger()->on('flush', array($this, 'processMessages'));
if (\Yii::$application !== null) {
\Yii::$application->on('afterRequest', array($this, 'processMessages'));
}
14 years ago
}
/**
* Retrieves and processes log messages from the system logger.
* This method mainly serves the event handler to [[Logger::onFlush]]
* and [[\yii\base\Application::onEndRequest]] 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)
{
14 years ago
$messages = \Yii::getLogger()->messages;
13 years ago
$final = $event->name !== 'flush';
foreach ($this->targets as $target) {
14 years ago
if ($target->enabled) {
13 years ago
$target->processMessages($messages, $final);
14 years ago
}
}
}
}