1: <?php
2: /**
3: * CLogRoute 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: * CLogRoute is the base class for all log route classes.
13: *
14: * A log route object retrieves log messages from a logger and sends it
15: * somewhere, such as files, emails.
16: * The messages being retrieved may be filtered first before being sent
17: * to the destination. The filters include log level filter and log category filter.
18: *
19: * To specify level filter, set {@link levels} property,
20: * which takes a string of comma-separated desired level names (e.g. 'Error, Debug').
21: * To specify category filter, set {@link categories} property,
22: * which takes a string of comma-separated desired category names (e.g. 'System.Web, System.IO').
23: *
24: * Level filter and category filter are combinational, i.e., only messages
25: * satisfying both filter conditions will they be returned.
26: *
27: * @author Qiang Xue <qiang.xue@gmail.com>
28: * @package system.logging
29: * @since 1.0
30: */
31: abstract class CLogRoute extends CComponent
32: {
33: /**
34: * @var boolean whether to enable this log route. Defaults to true.
35: */
36: public $enabled=true;
37: /**
38: * @var string list of levels separated by comma or space. Defaults to empty, meaning all levels.
39: */
40: public $levels='';
41: /**
42: * @var mixed array of categories, or string list separated by comma or space.
43: * Defaults to empty array, meaning all categories.
44: */
45: public $categories=array();
46: /**
47: * @var mixed array of categories, or string list separated by comma or space, to EXCLUDE from logs.
48: * Defaults to empty array, meaning no categories are excluded.
49: * This will exclude any categories after $categories has been ran.
50: */
51: public $except=array();
52: /**
53: * @var mixed the additional filter (eg {@link CLogFilter}) that can be applied to the log messages.
54: * The value of this property will be passed to {@link Yii::createComponent} to create
55: * a log filter object. As a result, this can be either a string representing the
56: * filter class name or an array representing the filter configuration.
57: * In general, the log filter class should implement {@link ILogFilter} interface.
58: * If you want to apply multiple filters you can use {@link CChainedLogFilter} to do so.
59: * Defaults to null, meaning no filter will be used.
60: */
61: public $filter;
62: /**
63: * @var array the logs that are collected so far by this log route.
64: * @since 1.1.0
65: */
66: public $logs=array();
67:
68:
69: /**
70: * Initializes the route.
71: * This method is invoked after the route is created by the route manager.
72: */
73: public function init()
74: {
75: }
76:
77: /**
78: * Formats a log message given different fields.
79: * @param string $message message content
80: * @param integer $level message level
81: * @param string $category message category
82: * @param integer $time timestamp
83: * @return string formatted message
84: */
85: protected function formatLogMessage($message,$level,$category,$time)
86: {
87: return @date('Y/m/d H:i:s',$time)." [$level] [$category] $message\n";
88: }
89:
90: /**
91: * Retrieves filtered log messages from logger for further processing.
92: * @param CLogger $logger logger instance
93: * @param boolean $processLogs whether to process the logs after they are collected from the logger
94: */
95: public function collectLogs($logger, $processLogs=false)
96: {
97: $logs=$logger->getLogs($this->levels,$this->categories,$this->except);
98: $this->logs=empty($this->logs) ? $logs : array_merge($this->logs,$logs);
99: if($processLogs && !empty($this->logs))
100: {
101: if($this->filter!==null)
102: Yii::createComponent($this->filter)->filter($this->logs);
103: if($this->logs!==array())
104: $this->processLogs($this->logs);
105: $this->logs=array();
106: }
107: }
108:
109: /**
110: * Processes log messages and sends them to specific destination.
111: * Derived child classes must implement this method.
112: * @param array $logs list of messages. Each array element represents one message
113: * with the following structure:
114: * array(
115: * [0] => message (string)
116: * [1] => level (string)
117: * [2] => category (string)
118: * [3] => timestamp (float, obtained by microtime(true));
119: */
120: abstract protected function processLogs($logs);
121: }
122: