Router.php 2.99 KB
Newer Older
w  
Qiang Xue committed
1 2 3
<?php
/**
 * @link http://www.yiiframework.com/
Qiang Xue committed
4
 * @copyright Copyright (c) 2008 Yii Software LLC
w  
Qiang Xue committed
5 6 7 8 9
 * @license http://www.yiiframework.com/license/
 */

namespace yii\logging;

Qiang Xue committed
10 11 12 13
use Yii;
use yii\base\Component;
use yii\base\Application;

w  
Qiang Xue committed
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/**
 * 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(
Qiang Xue committed
31
 *             'class' => 'yii\logging\Router',
w  
Qiang Xue committed
32 33
 *             'targets' => array(
 *                 'file' => array(
Qiang Xue committed
34 35 36
 *                     'class' => 'yii\logging\FileTarget',
 *                     'levels' => array('trace', 'info'),
 *                     'categories' => array('yii\*'),
w  
Qiang Xue committed
37 38
 *                 ),
 *                 'email' => array(
Qiang Xue committed
39 40
 *                     'class' => 'yii\logging\EmailTarget',
 *                     'levels' => array('error', 'warning'),
w  
Qiang Xue committed
41 42 43 44 45 46 47 48 49 50 51 52
 *                     'emails' => array('admin@example.com'),
 *                 ),
 *             ),
 *         ),
 *     ),
 * )
 * ~~~
 *
 * Each log target can have a name and can be referenced via the [[targets]] property
 * as follows:
 *
 * ~~~
Qiang Xue committed
53
 * Yii::$app->log->targets['file']->enabled = false;
w  
Qiang Xue committed
54 55 56 57 58
 * ~~~
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
Qiang Xue committed
59
class Router extends Component
w  
Qiang Xue committed
60
{
Qiang Xue committed
61
	/**
Qiang Xue committed
62
	 * @var Target[] list of log target objects or configurations. If the latter, target objects will
Qiang Xue committed
63
	 * be created in [[init()]] by calling [[Yii::createObject()]] with the corresponding object configuration.
Qiang Xue committed
64
	 */
Qiang Xue committed
65
	public $targets = array();
w  
Qiang Xue committed
66 67 68 69

	/**
	 * Initializes this application component.
	 * This method is invoked when the Router component is created by the application.
Qiang Xue committed
70 71
	 * The method attaches the [[processLogs]] method to both the [[Logger::EVENT_FLUSH]] event
	 * and the [[Logger::EVENT_FINAL_FLUSH]] event.
w  
Qiang Xue committed
72 73 74 75
	 */
	public function init()
	{
		parent::init();
Qiang Xue committed
76 77
		foreach ($this->targets as $name => $target) {
			if (!$target instanceof Target) {
Qiang Xue committed
78
				$this->targets[$name] = Yii::createObject($target);
w  
Qiang Xue committed
79 80
			}
		}
Qiang Xue committed
81
		Yii::getLogger()->router = $this;
w  
Qiang Xue committed
82 83 84
	}

	/**
Qiang Xue committed
85 86 87 88 89
	 * 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
w  
Qiang Xue committed
90
	 */
Qiang Xue committed
91
	public function dispatch($messages, $final = false)
w  
Qiang Xue committed
92
	{
Qiang Xue committed
93
		foreach ($this->targets as $target) {
w  
Qiang Xue committed
94
			if ($target->enabled) {
Qiang Xue committed
95
				$target->collect($messages, $final);
w  
Qiang Xue committed
96 97 98 99
			}
		}
	}
}