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.

100 lines
3.0 KiB

14 years ago
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
14 years ago
* @license http://www.yiiframework.com/license/
*/
namespace yii\logging;
use Yii;
use yii\base\Component;
use yii\base\Application;
14 years ago
/**
* 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(
12 years ago
* 'class' => 'yii\logging\Router',
14 years ago
* 'targets' => array(
* 'file' => array(
12 years ago
* 'class' => 'yii\logging\FileTarget',
* 'levels' => array('trace', 'info'),
* 'categories' => array('yii\*'),
14 years ago
* ),
* 'email' => array(
12 years ago
* 'class' => 'yii\logging\EmailTarget',
* 'levels' => array('error', 'warning'),
14 years ago
* 'emails' => array('admin@example.com'),
* ),
* ),
* ),
* ),
* )
* ~~~
*
* Each log target can have a name and can be referenced via the [[targets]] property
* as follows:
*
* ~~~
* Yii::$app->log->targets['file']->enabled = false;
14 years ago
* ~~~
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class Router extends Component
14 years ago
{
/**
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.
12 years ago
* The method attaches the [[processLogs]] method to both the [[Logger::EVENT_FLUSH]] event
* and the [[Logger::EVENT_FINAL_FLUSH]] 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
}
}
Yii::getLogger()->router = $this;
14 years ago
}
/**
* Dispatches log messages to [[targets]].
* This method is called by [[Logger]] when its [[Logger::flush()]] method is called.
* It will forward the messages to each log target registered in [[targets]].
* @param array $messages the messages to be processed
* @param boolean $final whether this is the final call during a request cycle
14 years ago
*/
public function dispatch($messages, $final = false)
14 years ago
{
13 years ago
foreach ($this->targets as $target) {
14 years ago
if ($target->enabled) {
$target->collect($messages, $final);
14 years ago
}
}
}
}