~horux-dev/horux-webcli/thfo

« back to all changes in this revision

Viewing changes to yii/framework/logging/CLogRouter.php

  • Committer: Thierry Forchelet
  • Date: 2011-02-25 13:30:15 UTC
  • Revision ID: thierry.forchelet@letux.ch-20110225133015-zxyj9w7sqv8ly971
Initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * CLogRouter class file.
 
4
 *
 
5
 * @author Qiang Xue <qiang.xue@gmail.com>
 
6
 * @link http://www.yiiframework.com/
 
7
 * @copyright Copyright &copy; 2008-2011 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
 *                     'email'=>'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
 * @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
 
49
 * @since 1.0
 
50
 */
 
51
class CLogRouter extends CApplicationComponent
 
52
{
 
53
        private $_routes=array();
 
54
 
 
55
        /**
 
56
         * Initializes this application component.
 
57
         * This method is required by the IApplicationComponent interface.
 
58
         */
 
59
        public function init()
 
60
        {
 
61
                parent::init();
 
62
                foreach($this->_routes as $name=>$route)
 
63
                {
 
64
                        $route=Yii::createComponent($route);
 
65
                        $route->init();
 
66
                        $this->_routes[$name]=$route;
 
67
                }
 
68
                Yii::getLogger()->attachEventHandler('onFlush',array($this,'collectLogs'));
 
69
                Yii::app()->attachEventHandler('onEndRequest',array($this,'processLogs'));
 
70
        }
 
71
 
 
72
        /**
 
73
         * @return array the currently initialized routes
 
74
         */
 
75
        public function getRoutes()
 
76
        {
 
77
                return new CMap($this->_routes);
 
78
        }
 
79
 
 
80
        /**
 
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:
 
83
         * <ul>
 
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>
 
86
         * </ul>
 
87
         */
 
88
        public function setRoutes($config)
 
89
        {
 
90
                foreach($config as $name=>$route)
 
91
                        $this->_routes[$name]=$route;
 
92
        }
 
93
 
 
94
        /**
 
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
 
98
         */
 
99
        public function collectLogs($event)
 
100
        {
 
101
                $logger=Yii::getLogger();
 
102
                foreach($this->_routes as $route)
 
103
                {
 
104
                        if($route->enabled)
 
105
                                $route->collectLogs($logger,false);
 
106
                }
 
107
        }
 
108
 
 
109
        /**
 
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
 
113
         * @since 1.1.0
 
114
         */
 
115
        public function processLogs($event)
 
116
        {
 
117
                $logger=Yii::getLogger();
 
118
                foreach($this->_routes as $route)
 
119
                {
 
120
                        if($route->enabled)
 
121
                                $route->collectLogs($logger,true);
 
122
                }
 
123
        }
 
124
}