1: <?php
2: /**
3: * CLogRouter class file.
4: *
5: * @author Qiang Xue <qiang.xue@gmail.com>
6: * @link http://www.yiiframework.com/
7: * @copyright 2008-2013 Yii Software LLC
8: * @license http://www.yiiframework.com/license/
9: */
10:
11: /**
12: * CLogRouter manages log routes that record log messages in different media.
13: *
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.
18: *
19: * Log routes may be configured in application configuration like following:
20: * <pre>
21: * array(
22: * 'preload'=>array('log'), // preload log component when app starts
23: * 'components'=>array(
24: * 'log'=>array(
25: * 'class'=>'CLogRouter',
26: * 'routes'=>array(
27: * array(
28: * 'class'=>'CFileLogRoute',
29: * 'levels'=>'trace, info',
30: * 'categories'=>'system.*',
31: * ),
32: * array(
33: * 'class'=>'CEmailLogRoute',
34: * 'levels'=>'error, warning',
35: * 'emails'=>array('admin@example.com'),
36: * ),
37: * ),
38: * ),
39: * ),
40: * )
41: * </pre>
42: *
43: * You can specify multiple routes with different filtering conditions and different
44: * targets, even if the routes are of the same type.
45: *
46: * @property array $routes The currently initialized routes.
47: *
48: * @author Qiang Xue <qiang.xue@gmail.com>
49: * @package system.logging
50: * @since 1.0
51: */
52: class CLogRouter extends CApplicationComponent
53: {
54: private $_routes=array();
55:
56: /**
57: * Initializes this application component.
58: * This method is required by the IApplicationComponent interface.
59: */
60: public function init()
61: {
62: parent::init();
63: foreach($this->_routes as $name=>$route)
64: {
65: $route=Yii::createComponent($route);
66: $route->init();
67: $this->_routes[$name]=$route;
68: }
69: Yii::getLogger()->attachEventHandler('onFlush',array($this,'collectLogs'));
70: Yii::app()->attachEventHandler('onEndRequest',array($this,'processLogs'));
71: }
72:
73: /**
74: * @return array the currently initialized routes
75: */
76: public function getRoutes()
77: {
78: return new CMap($this->_routes);
79: }
80:
81: /**
82: * @param array $config list of route configurations. Each array element represents
83: * the configuration for a single route and has the following array structure:
84: * <ul>
85: * <li>class: specifies the class name or alias for the route class.</li>
86: * <li>name-value pairs: configure the initial property values of the route.</li>
87: * </ul>
88: */
89: public function setRoutes($config)
90: {
91: foreach($config as $name=>$route)
92: $this->_routes[$name]=$route;
93: }
94:
95: /**
96: * Collects log messages from a logger.
97: * This method is an event handler to the {@link CLogger::onFlush} event.
98: * @param CEvent $event event parameter
99: */
100: public function collectLogs($event)
101: {
102: $logger=Yii::getLogger();
103: $dumpLogs=isset($event->params['dumpLogs']) && $event->params['dumpLogs'];
104: foreach($this->_routes as $route)
105: {
106: /* @var $route CLogRoute */
107: if($route->enabled)
108: $route->collectLogs($logger,$dumpLogs);
109: }
110: }
111:
112: /**
113: * Collects and processes log messages from a logger.
114: * This method is an event handler to the {@link CApplication::onEndRequest} event.
115: * @since 1.1.0
116: */
117: public function processLogs()
118: {
119: $logger=Yii::getLogger();
120: $logger->flush(true);
121: }
122: }
123: