Router.php 4.03 KB
Newer Older
w  
Qiang Xue committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
<?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:
 *
 * ~~~
Qiang Xue committed
51
 * \Yii::$application->log->targets['file']->enabled = false;
w  
Qiang Xue committed
52 53 54 55 56 57 58
 * ~~~
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class Router extends \yii\base\ApplicationComponent
{
Qiang Xue committed
59 60 61
	/**
	 * @var \yii\base\Dictionary
	 */
w  
Qiang Xue committed
62 63 64 65 66 67 68 69 70 71 72 73 74
	private $_targets;

	/**
	 * Constructor.
	 */
	public function __construct()
	{
		$this->_targets = new \yii\base\Dictionary;
	}

	/**
	 * Initializes this application component.
	 * This method is invoked when the Router component is created by the application.
Qiang Xue committed
75 76
	 * The method attaches the [[processLogs]] method to both the [[Logger::flush]] event
	 * and the [[\yii\base\Application::afterRequest]] event.
w  
Qiang Xue committed
77 78 79 80
	 */
	public function init()
	{
		parent::init();
Qiang Xue committed
81
		\Yii::getLogger()->on('flush', array($this, 'processMessages'));
Qiang Xue committed
82
		if (($app = \Yii::$application) !== null) {
Qiang Xue committed
83
			$app->on('afterRequest', array($this, 'processMessages'));
w  
Qiang Xue committed
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
		}
	}

	/**
	 * Returns the log targets managed by this log router.
	 * The keys of the dictionary are the names of the log targets.
	 * You can use the name to access a specific log target. For example,
	 *
	 * ~~~
	 * $target = $router->targets['file'];
	 * ~~~
	 * @return \yii\base\Dictionary the targets managed by this log router.
	 */
	public function getTargets()
	{
		return $this->_targets;
	}

	/**
	 * Sets the log targets.
	 * @param array $config list of log target configurations. Each array element
	 * represents the configuration for creating a single log target. It will be
Qiang Xue committed
106
	 * passed to [[\Yii::createObject()]] to create the target instance.
w  
Qiang Xue committed
107 108 109 110 111 112
	 */
	public function setTargets($config)
	{
		foreach ($config as $name => $target) {
			if ($target instanceof Target) {
				$this->_targets[$name] = $target;
Qiang Xue committed
113
			} else {
Qiang Xue committed
114
				$this->_targets[$name] = \Yii::createObject($target);
w  
Qiang Xue committed
115 116 117 118 119 120 121 122 123 124 125 126 127 128
			}
		}
	}

	/**
	 * 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)
	{
w  
Qiang Xue committed
129
		$messages = \Yii::getLogger()->messages;
Qiang Xue committed
130 131
		$export = !isset($event->data['export']) || $event->data['export'];
		$final = !isset($event->data['flush']) || !$event->data['flush'];
w  
Qiang Xue committed
132 133
		foreach ($this->_targets as $target) {
			if ($target->enabled) {
w  
Qiang Xue committed
134
				$target->processMessages($messages, $export, $final);
w  
Qiang Xue committed
135 136 137 138
			}
		}
	}
}