3
* CLogRouter class file.
5
* @author Qiang Xue <qiang.xue@gmail.com>
6
* @link http://www.yiiframework.com/
7
* @copyright Copyright © 2008-2011 Yii Software LLC
8
* @license http://www.yiiframework.com/license/
12
* CLogRouter manages log routes that record log messages in different media.
14
* For example, a file log route {@link CFileLogRoute} records log messages
15
* in log files. An email log route {@link CEmailLogRoute} sends log messages
16
* to specific email addresses. See {@link CLogRoute} for more details about
17
* different log routes.
19
* Log routes may be configured in application configuration like following:
22
* 'preload'=>array('log'), // preload log component when app starts
23
* 'components'=>array(
25
* 'class'=>'CLogRouter',
28
* 'class'=>'CFileLogRoute',
29
* 'levels'=>'trace, info',
30
* 'categories'=>'system.*',
33
* 'class'=>'CEmailLogRoute',
34
* 'levels'=>'error, warning',
35
* 'email'=>'admin@example.com',
43
* You can specify multiple routes with different filtering conditions and different
44
* targets, even if the routes are of the same type.
46
* @author Qiang Xue <qiang.xue@gmail.com>
47
* @version $Id: CLogRouter.php 2799 2011-01-01 19:31:13Z qiang.xue $
48
* @package system.logging
51
class CLogRouter extends CApplicationComponent
53
private $_routes=array();
56
* Initializes this application component.
57
* This method is required by the IApplicationComponent interface.
59
public function init()
62
foreach($this->_routes as $name=>$route)
64
$route=Yii::createComponent($route);
66
$this->_routes[$name]=$route;
68
Yii::getLogger()->attachEventHandler('onFlush',array($this,'collectLogs'));
69
Yii::app()->attachEventHandler('onEndRequest',array($this,'processLogs'));
73
* @return array the currently initialized routes
75
public function getRoutes()
77
return new CMap($this->_routes);
81
* @param array $config list of route configurations. Each array element represents
82
* the configuration for a single route and has the following array structure:
84
* <li>class: specifies the class name or alias for the route class.</li>
85
* <li>name-value pairs: configure the initial property values of the route.</li>
88
public function setRoutes($config)
90
foreach($config as $name=>$route)
91
$this->_routes[$name]=$route;
95
* Collects log messages from a logger.
96
* This method is an event handler to the {@link CLogger::onFlush} event.
97
* @param CEvent $event event parameter
99
public function collectLogs($event)
101
$logger=Yii::getLogger();
102
foreach($this->_routes as $route)
105
$route->collectLogs($logger,false);
110
* Collects and processes log messages from a logger.
111
* This method is an event handler to the {@link CApplication::onEndRequest} event.
112
* @param CEvent $event event parameter
115
public function processLogs($event)
117
$logger=Yii::getLogger();
118
foreach($this->_routes as $route)
121
$route->collectLogs($logger,true);