CLogRoute.php
3.74 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
<?php
/**
* CLogRoute 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/
*/
/**
* CLogRoute is the base class for all log route classes.
*
* A log route object retrieves log messages from a logger and sends it
* somewhere, such as files, emails.
* The messages being retrieved may be filtered first before being sent
* to the destination. The filters include log level filter and log category filter.
*
* To specify level filter, set {@link levels} property,
* which takes a string of comma-separated desired level names (e.g. 'Error, Debug').
* To specify category filter, set {@link categories} property,
* which takes a string of comma-separated desired category names (e.g. 'System.Web, System.IO').
*
* Level filter and category filter are combinational, i.e., only messages
* satisfying both filter conditions will they be returned.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id$
* @package system.logging
* @since 1.0
*/
abstract class CLogRoute extends CComponent
{
/**
* @var boolean whether to enable this log route. Defaults to true.
*/
public $enabled=true;
/**
* @var string list of levels separated by comma or space. Defaults to empty, meaning all levels.
*/
public $levels='';
/**
* @var string list of categories separated by comma or space. Defaults to empty, meaning all categories.
*/
public $categories='';
/**
* @var mixed the additional filter (eg {@link CLogFilter}) that can be applied to the log messages.
* The value of this property will be passed to {@link Yii::createComponent} to create
* a log filter object. As a result, this can be either a string representing the
* filter class name or an array representing the filter configuration.
* In general, the log filter class should implement {@link ILogFilter} interface.
* Defaults to null, meaning no filter will be used.
*/
public $filter;
/**
* @var array the logs that are collected so far by this log route.
* @since 1.1.0
*/
public $logs=array();
/**
* Initializes the route.
* This method is invoked after the route is created by the route manager.
*/
public function init()
{
}
/**
* Formats a log message given different fields.
* @param string $message message content
* @param integer $level message level
* @param string $category message category
* @param integer $time timestamp
* @return string formatted message
*/
protected function formatLogMessage($message,$level,$category,$time)
{
return @date('Y/m/d H:i:s',$time)." [$level] [$category] $message\n";
}
/**
* Retrieves filtered log messages from logger for further processing.
* @param CLogger $logger logger instance
* @param boolean $processLogs whether to process the logs after they are collected from the logger
*/
public function collectLogs($logger, $processLogs=false)
{
$logs=$logger->getLogs($this->levels,$this->categories);
$this->logs=empty($this->logs) ? $logs : array_merge($this->logs,$logs);
if($processLogs && !empty($this->logs))
{
if($this->filter!==null)
Yii::createComponent($this->filter)->filter($this->logs);
if($this->logs!==array())
$this->processLogs($this->logs);
$this->logs=array();
}
}
/**
* Processes log messages and sends them to specific destination.
* Derived child classes must implement this method.
* @param array $logs list of messages. Each array element represents one message
* with the following structure:
* array(
* [0] => message (string)
* [1] => level (string)
* [2] => category (string)
* [3] => timestamp (float, obtained by microtime(true));
*/
abstract protected function processLogs($logs);
}