| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* TLogger class file
|
|
|
4 |
*
|
|
|
5 |
* @author Qiang Xue <qiang.xue@gmail.com>
|
|
|
6 |
* @link http://www.pradosoft.com/
|
|
|
7 |
* @copyright Copyright © 2005-2008 PradoSoft
|
|
|
8 |
* @license http://www.pradosoft.com/license/
|
|
|
9 |
* @version $Id: TLogger.php 2541 2008-10-21 15:05:13Z qiang.xue $
|
|
|
10 |
* @package System.Util
|
|
|
11 |
*/
|
|
|
12 |
|
|
|
13 |
/**
|
|
|
14 |
* TLogger class.
|
|
|
15 |
*
|
|
|
16 |
* TLogger records log messages in memory and implements the methods to
|
|
|
17 |
* retrieve the messages with filter conditions, including log levels and
|
|
|
18 |
* log categories.
|
|
|
19 |
*
|
|
|
20 |
* @author Qiang Xue <qiang.xue@gmail.com>
|
|
|
21 |
* @version $Id: TLogger.php 2541 2008-10-21 15:05:13Z qiang.xue $
|
|
|
22 |
* @package System.Util
|
|
|
23 |
* @since 3.0
|
|
|
24 |
*/
|
|
|
25 |
class TLogger extends TComponent
|
|
|
26 |
{
|
|
|
27 |
/**
|
|
|
28 |
* Log levels.
|
|
|
29 |
*/
|
|
|
30 |
const DEBUG=0x01;
|
|
|
31 |
const INFO=0x02;
|
|
|
32 |
const NOTICE=0x04;
|
|
|
33 |
const WARNING=0x08;
|
|
|
34 |
const ERROR=0x10;
|
|
|
35 |
const ALERT=0x20;
|
|
|
36 |
const FATAL=0x40;
|
|
|
37 |
/**
|
|
|
38 |
* @var array log messages
|
|
|
39 |
*/
|
|
|
40 |
private $_logs=array();
|
|
|
41 |
/**
|
|
|
42 |
* @var integer log levels (bits) to be filtered
|
|
|
43 |
*/
|
|
|
44 |
private $_levels;
|
|
|
45 |
/**
|
|
|
46 |
* @var array list of categories to be filtered
|
|
|
47 |
*/
|
|
|
48 |
private $_categories;
|
|
|
49 |
|
|
|
50 |
/**
|
|
|
51 |
* Logs a message.
|
|
|
52 |
* Messages logged by this method may be retrieved via {@link getLogs}.
|
|
|
53 |
* @param string message to be logged
|
|
|
54 |
* @param integer level of the message. Valid values include
|
|
|
55 |
* TLogger::DEBUG, TLogger::INFO, TLogger::NOTICE, TLogger::WARNING,
|
|
|
56 |
* TLogger::ERROR, TLogger::ALERT, TLogger::FATAL.
|
|
|
57 |
* @param string category of the message
|
|
|
58 |
*/
|
|
|
59 |
public function log($message,$level,$category='Uncategorized')
|
|
|
60 |
{
|
|
|
61 |
$this->_logs[]=array($message,$level,$category,microtime(true));
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
/**
|
|
|
65 |
* Retrieves log messages.
|
|
|
66 |
* Messages may be filtered by log levels and/or categories.
|
|
|
67 |
* A level filter is specified by an integer, whose bits indicate the levels interested.
|
|
|
68 |
* For example, (TLogger::INFO | TLogger::WARNING) specifies INFO and WARNING levels.
|
|
|
69 |
* A category filter is specified by concatenating interested category names
|
|
|
70 |
* with commas. A message whose category name starts with any filtering category
|
|
|
71 |
* will be returned. For example, a category filter 'System.Web, System.IO'
|
|
|
72 |
* will return messages under categories such as 'System.Web', 'System.IO',
|
|
|
73 |
* 'System.Web.UI', 'System.Web.UI.WebControls', etc.
|
|
|
74 |
* Level filter and category filter are combinational, i.e., only messages
|
|
|
75 |
* satisfying both filter conditions will they be returned.
|
|
|
76 |
* @param integer level filter
|
|
|
77 |
* @param string category filter
|
|
|
78 |
* @param array list of messages. Each array elements represents one message
|
|
|
79 |
* with the following structure:
|
|
|
80 |
* array(
|
|
|
81 |
* [0] => message
|
|
|
82 |
* [1] => level
|
|
|
83 |
* [2] => category
|
|
|
84 |
* [3] => timestamp (by microtime(), float number));
|
|
|
85 |
*/
|
|
|
86 |
public function getLogs($levels=null,$categories=null)
|
|
|
87 |
{
|
|
|
88 |
$this->_levels=$levels;
|
|
|
89 |
$this->_categories=$categories;
|
|
|
90 |
if(empty($levels) && empty($categories))
|
|
|
91 |
return $this->_logs;
|
|
|
92 |
else if(empty($levels))
|
|
|
93 |
return array_values(array_filter(array_filter($this->_logs,array($this,'filterByCategories'))));
|
|
|
94 |
else if(empty($categories))
|
|
|
95 |
return array_values(array_filter(array_filter($this->_logs,array($this,'filterByLevels'))));
|
|
|
96 |
else
|
|
|
97 |
{
|
|
|
98 |
$ret=array_values(array_filter(array_filter($this->_logs,array($this,'filterByLevels'))));
|
|
|
99 |
return array_values(array_filter(array_filter($ret,array($this,'filterByCategories'))));
|
|
|
100 |
}
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
/**
|
|
|
104 |
* Filter function used by {@link getLogs}
|
|
|
105 |
* @param array element to be filtered
|
|
|
106 |
*/
|
|
|
107 |
private function filterByCategories($value)
|
|
|
108 |
{
|
|
|
109 |
foreach($this->_categories as $category)
|
|
|
110 |
{
|
|
|
111 |
if($value[2]===$category || strpos($value[2],$category.'.')===0)
|
|
|
112 |
return $value;
|
|
|
113 |
}
|
|
|
114 |
return false;
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
/**
|
|
|
118 |
* Filter function used by {@link getLogs}
|
|
|
119 |
* @param array element to be filtered
|
|
|
120 |
*/
|
|
|
121 |
private function filterByLevels($value)
|
|
|
122 |
{
|
|
|
123 |
if($value[1] & $this->_levels)
|
|
|
124 |
return $value;
|
|
|
125 |
else
|
|
|
126 |
return false;
|
|
|
127 |
}
|
|
|
128 |
}
|
|
|
129 |
|