CLogRouter.php
3.51 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
/**
* CLogRouter class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright © 2008-2011 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
/**
* CLogRouter manages log routes that record log messages in different media.
*
* For example, a file log route {@link CFileLogRoute} records log messages
* in log files. An email log route {@link CEmailLogRoute} sends log messages
* to specific email addresses. See {@link CLogRoute} for more details about
* different log routes.
*
* Log routes may be configured in application configuration like following:
* <pre>
* array(
* 'preload'=>array('log'), // preload log component when app starts
* 'components'=>array(
* 'log'=>array(
* 'class'=>'CLogRouter',
* 'routes'=>array(
* array(
* 'class'=>'CFileLogRoute',
* 'levels'=>'trace, info',
* 'categories'=>'system.*',
* ),
* array(
* 'class'=>'CEmailLogRoute',
* 'levels'=>'error, warning',
* 'emails'=>array('admin@example.com'),
* ),
* ),
* ),
* ),
* )
* </pre>
*
* You can specify multiple routes with different filtering conditions and different
* targets, even if the routes are of the same type.
*
* @property array $routes The currently initialized routes.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id$
* @package system.logging
* @since 1.0
*/
class CLogRouter extends CApplicationComponent
{
private $_routes=array();
/**
* Initializes this application component.
* This method is required by the IApplicationComponent interface.
*/
public function init()
{
parent::init();
foreach($this->_routes as $name=>$route)
{
$route=Yii::createComponent($route);
$route->init();
$this->_routes[$name]=$route;
}
Yii::getLogger()->attachEventHandler('onFlush',array($this,'collectLogs'));
Yii::app()->attachEventHandler('onEndRequest',array($this,'processLogs'));
}
/**
* @return array the currently initialized routes
*/
public function getRoutes()
{
return new CMap($this->_routes);
}
/**
* @param array $config list of route configurations. Each array element represents
* the configuration for a single route and has the following array structure:
* <ul>
* <li>class: specifies the class name or alias for the route class.</li>
* <li>name-value pairs: configure the initial property values of the route.</li>
* </ul>
*/
public function setRoutes($config)
{
foreach($config as $name=>$route)
$this->_routes[$name]=$route;
}
/**
* Collects log messages from a logger.
* This method is an event handler to the {@link CLogger::onFlush} event.
* @param CEvent $event event parameter
*/
public function collectLogs($event)
{
$logger=Yii::getLogger();
$dumpLogs=isset($event->params['dumpLogs']) && $event->params['dumpLogs'];
foreach($this->_routes as $route)
{
if($route->enabled)
$route->collectLogs($logger,$dumpLogs);
}
}
/**
* Collects and processes log messages from a logger.
* This method is an event handler to the {@link CApplication::onEndRequest} event.
* @param CEvent $event event parameter
* @since 1.1.0
*/
public function processLogs($event)
{
$logger=Yii::getLogger();
foreach($this->_routes as $route)
{
if($route->enabled)
$route->collectLogs($logger,true);
}
}
}