1 |
<?php |
<?php |
2 |
// $Id$ |
// $Id$ |
3 |
// $Horde: horde/lib/Log.php,v 1.15 2000/06/29 23:39:45 jon Exp $ |
// $PEAR: Log.php,v 1.16 2003/01/02 04:41:38 jon Exp $ |
4 |
|
// $Horde: horde/lib/Log.php,v 1.15 2000/06/29 23:39:45 jon Exp $ |
5 |
/** |
|
6 |
* The Log:: class implements both an abstraction for various logging |
require_once 'PEAR.php'; |
7 |
* mechanisms and the Subject end of a Subject-Observer pattern. |
|
8 |
* |
define('PEAR_LOG_EMERG', 0); |
9 |
* @author Chuck Hagenbuch <chuck@horde.org> |
define('PEAR_LOG_ALERT', 1); |
10 |
* @author Jon Parise <jon@csh.rit.edu> |
define('PEAR_LOG_CRIT', 2); |
11 |
* @version $Revision$ |
define('PEAR_LOG_ERR', 3); |
12 |
* @since Horde 1.3 |
define('PEAR_LOG_WARNING', 4); |
13 |
*/ |
define('PEAR_LOG_NOTICE', 5); |
14 |
class Log { |
define('PEAR_LOG_INFO', 6); |
15 |
|
define('PEAR_LOG_DEBUG', 7); |
16 |
// {{{ properties |
|
17 |
|
/** |
18 |
/** Boolean indicating whether or not the log connection is |
* The Log:: class implements both an abstraction for various logging |
19 |
currently open. */ |
* mechanisms and the Subject end of a Subject-Observer pattern. |
20 |
var $opened = false; |
* |
21 |
|
* @author Chuck Hagenbuch <chuck@horde.org> |
22 |
/** String holding the identifier that will be stored along with |
* @author Jon Parise <jon@php.net> |
23 |
each logged message. */ |
* @version $Revision$ |
24 |
var $ident = ''; |
* @since Horde 1.3 |
25 |
|
* @package Log |
26 |
/** Array holding all Log_observer objects that wish to be notified |
*/ |
27 |
of any messages that we handle. */ |
class Log extends PEAR { |
28 |
var $listeners = array(); |
|
29 |
|
/** |
30 |
// }}} |
* Indicates whether or not the log can been opened / connected. |
31 |
|
* |
32 |
// {{{ factory() |
* @var boolean |
33 |
/** |
* @access private |
34 |
* Attempts to return a concrete Log instance of $log_type. |
*/ |
35 |
* |
var $_opened = false; |
36 |
* @param $log_type The type of concrete Log subclass to return. |
|
37 |
* Attempt to dynamically include the code for this |
/** |
38 |
* subclass. Currently, valid values are 'syslog', |
* The label that uniquely identifies this set of log messages. |
39 |
* 'sql', 'file', and 'mcal'. |
* |
40 |
* |
* @var string |
41 |
* @param $log_name (optional) The name of the actually log file, |
* @access private |
42 |
* table, or other specific store to use. Defaults |
*/ |
43 |
* to an empty string, with which the subclass will |
var $_ident = ''; |
44 |
* attempt to do something intelligent. |
|
45 |
* |
/** |
46 |
* @param $ident (optional) The indentity reported to the log system. |
* The maximum priority level at which to log a message. |
47 |
* |
* |
48 |
* @param $conf (optional) A hash containing any additional |
* @var int |
49 |
* configuration information that a subclass might need. |
* @access private |
50 |
* |
*/ |
51 |
* @return The newly created concrete Log instance, or an |
var $_maxLevel = PEAR_LOG_DEBUG; |
52 |
* false on an error. |
|
53 |
*/ |
/** |
54 |
function factory ($log_type, $log_name = '', $ident = '', $conf = array()) { |
* Holds all Log_observer objects that wish to be notified of new messages. |
55 |
$log_type = strtolower($log_type); |
* |
56 |
$classfile = 'Log/' . $log_type . '.php'; |
* @var array |
57 |
if (@include_once $classfile) { |
* @access private |
58 |
$class = 'Log_' . $log_type; |
*/ |
59 |
return new $class($log_name, $ident, $conf); |
var $_listeners = array(); |
60 |
} else { |
|
61 |
return false; |
|
62 |
} |
/** |
63 |
} |
* Attempts to return a concrete Log instance of $type. |
64 |
// }}} |
* |
65 |
|
* @param string $type The type of concrete Log subclass to return. |
66 |
// {{{ singleton() |
* Attempt to dynamically include the code for |
67 |
/** |
* this subclass. Currently, valid values are |
68 |
* Attempts to return a reference to a concrete Log instance of |
* 'console', 'syslog', 'sql', 'file', and 'mcal'. |
69 |
* $log_type, only creating a new instance if no log instance with |
* |
70 |
* the same parameters currently exists. |
* @param string $name The name of the actually log file, table, or |
71 |
* |
* other specific store to use. Defaults to an |
72 |
* You should use this if there are multiple places you might |
* empty string, with which the subclass will |
73 |
* create a logger, you don't want to create multiple loggers, and |
* attempt to do something intelligent. |
74 |
* you don't want to check for the existance of one each time. The |
* |
75 |
* singleton pattern does all the checking work for you. |
* @param string $ident The identity reported to the log system. |
76 |
* |
* |
77 |
* <b>You MUST call this method with the $var = &Log::singleton() |
* @param array $conf A hash containing any additional configuration |
78 |
* syntax. Without the ampersand (&) in front of the method name, |
* information that a subclass might need. |
79 |
* you will not get a reference, you will get a copy.</b> |
* |
80 |
* |
* @param int $maxLevel Maximum priority level at which to log. |
81 |
* @param $log_type The type of concrete Log subclass to return. |
* |
82 |
* Attempt to dynamically include the code for |
* @return object Log The newly created concrete Log instance, or an |
83 |
* this subclass. Currently, valid values are |
* false on an error. |
84 |
* 'syslog', 'sql', 'file', and 'mcal'. |
* @access public |
85 |
* |
*/ |
86 |
* @param $log_name (optional) The name of the actually log file, |
function &factory($type, $name = '', $ident = '', $conf = array(), |
87 |
* table, or other specific store to use. Defaults |
$maxLevel = PEAR_LOG_DEBUG) |
88 |
* to an empty string, with which the subclass will |
{ |
89 |
* attempt to do something intelligent. |
$type = strtolower($type); |
90 |
* |
$classfile = 'Log/' . $type . '.php'; |
91 |
* @param $ident (optional) The identity reported to the log system. |
if (@include_once $classfile) { |
92 |
* |
$class = 'Log_' . $type; |
93 |
* @param $conf (optional) A hash containing any additional |
return new $class($name, $ident, $conf, $maxLevel); |
94 |
* configuration information that a subclass might need. |
} else { |
95 |
* |
return false; |
96 |
* @return The concrete Log reference, or false on an error. |
} |
97 |
*/ |
} |
98 |
function &singleton ($log_type, $log_name = '', $ident = '', $conf = array()) { |
|
99 |
static $instances; |
/** |
100 |
if (!isset($instances)) $instances = array(); |
* Attempts to return a reference to a concrete Log instance of $type, only |
101 |
|
* creating a new instance if no log instance with the same parameters |
102 |
$signature = md5($log_type . '][' . $log_name . '][' . $ident . '][' . implode('][', $conf)); |
* currently exists. |
103 |
if (!isset($instances[$signature])) { |
* |
104 |
$instances[$signature] = Log::factory($log_type, $log_name, $ident, $conf); |
* You should use this if there are multiple places you might create a |
105 |
} |
* logger, you don't want to create multiple loggers, and you don't want to |
106 |
return $instances[$signature]; |
* check for the existance of one each time. The singleton pattern does all |
107 |
} |
* the checking work for you. |
108 |
// }}} |
* |
109 |
|
* <b>You MUST call this method with the $var = &Log::singleton() syntax. |
110 |
// {{{ priorityToString() |
* Without the ampersand (&) in front of the method name, you will not get |
111 |
/** |
* a reference, you will get a copy.</b> |
112 |
* Returns the string representation of a LOG_* integer constant. |
* |
113 |
* |
* @param string $type The type of concrete Log subclass to return. |
114 |
* @param $priority The LOG_* integer constant. |
* Attempt to dynamically include the code for |
115 |
* |
* this subclass. Currently, valid values are |
116 |
* @return The string representation of $priority. |
* 'console', 'syslog', 'sql', 'file', and 'mcal'. |
117 |
*/ |
* |
118 |
function priorityToString ($priority) { |
* @param string $name The name of the actually log file, table, or |
119 |
$priorities = array( |
* other specific store to use. Defaults to an |
120 |
LOG_EMERG => 'emergency', |
* empty string, with which the subclass will |
121 |
LOG_ALERT => 'alert', |
* attempt to do something intelligent. |
122 |
LOG_CRIT => 'critical', |
* |
123 |
LOG_ERR => 'error', |
* @param string $ident The identity reported to the log system. |
124 |
LOG_WARNING => 'warning', |
* |
125 |
LOG_NOTICE => 'notice', |
* @param array $conf A hash containing any additional configuration |
126 |
LOG_INFO => 'info', |
* information that a subclass might need. |
127 |
LOG_DEBUG => 'debug' |
* |
128 |
); |
* @param int $maxLevel Minimum priority level at which to log. |
129 |
return $priorities[$priority]; |
* |
130 |
} |
* @return object Log The newly created concrete Log instance, or an |
131 |
// }}} |
* false on an error. |
132 |
|
* @access public |
133 |
// {{{ attach() |
*/ |
134 |
/** |
function &singleton($type, $name = '', $ident = '', $conf = array(), |
135 |
* Adds a Log_observer instance to the list of observers that are |
$maxLevel = PEAR_LOG_DEBUG) |
136 |
* be notified when a message is logged. |
{ |
137 |
* |
static $instances; |
138 |
* @param $logObserver The Log_observer instance to be added to |
if (!isset($instances)) $instances = array(); |
139 |
* the $listeners array. |
|
140 |
*/ |
$signature = serialize(array($type, $name, $ident, $conf, $maxLevel)); |
141 |
function attach (&$logObserver) { |
if (!isset($instances[$signature])) { |
142 |
if (!is_object($logObserver)) |
$instances[$signature] = &Log::factory($type, $name, $ident, $conf, |
143 |
return false; |
$maxLevel); |
144 |
|
} |
145 |
$logObserver->_listenerID = uniqid(rand()); |
|
146 |
|
return $instances[$signature]; |
147 |
$this->listeners[$logObserver->_listenerID] = &$logObserver; |
} |
148 |
} |
|
149 |
// }}} |
/** |
150 |
|
* Abstract implementation of the close() method. |
151 |
// {{{ detach() |
*/ |
152 |
/** |
function close() |
153 |
* Removes a Log_observer instance from the list of observers. |
{ |
154 |
* |
return false; |
155 |
* @param $logObserver The Log_observer instance to be removed |
} |
156 |
* from the $listeners array. |
|
157 |
*/ |
/** |
158 |
function detach ($logObserver) { |
* Abstract implementation of the log() method. |
159 |
if (isset($this->listeners[$logObserver->_listenerID])) |
*/ |
160 |
unset($this->listeners[$logObserver->_listenerID]); |
function log($message, $priority = PEAR_LOG_INFO) |
161 |
} |
{ |
162 |
// }}} |
return false; |
163 |
|
} |
164 |
// {{{ notifyAll() |
|
165 |
/** |
/** |
166 |
* Sends any Log_observer objects listening to this Log the message |
* Returns the string representation of a PEAR_LOG_* integer constant. |
167 |
* that was just logged. |
* |
168 |
* |
* @param int $priority A PEAR_LOG_* integer constant. |
169 |
* @param $messageOb The data structure holding all relevant log |
* |
170 |
* information - the message, the priority, what |
* @return string The string representation of $priority. |
171 |
* log this is, etc. |
*/ |
172 |
*/ |
function priorityToString($priority) |
173 |
function notifyAll ($messageOb) { |
{ |
174 |
reset($this->listeners); |
$priorities = array( |
175 |
foreach ($this->listeners as $listener) { |
PEAR_LOG_EMERG => 'emergency', |
176 |
if ($messageOb['priority'] <= $listener->priority) |
PEAR_LOG_ALERT => 'alert', |
177 |
$listener->notify($messageOb); |
PEAR_LOG_CRIT => 'critical', |
178 |
} |
PEAR_LOG_ERR => 'error', |
179 |
} |
PEAR_LOG_WARNING => 'warning', |
180 |
// }}} |
PEAR_LOG_NOTICE => 'notice', |
181 |
|
PEAR_LOG_INFO => 'info', |
182 |
// {{{ isComposite() |
PEAR_LOG_DEBUG => 'debug' |
183 |
/** |
); |
184 |
* @return a Boolean: true if this is a composite class, false |
|
185 |
* otherwise. The composite subclass overrides this to return |
return $priorities[$priority]; |
186 |
* true. |
} |
187 |
*/ |
|
188 |
function isComposite () { |
/** |
189 |
return false; |
* Adds a Log_observer instance to the list of observers that are be |
190 |
} |
* notified when a message is logged. |
191 |
// }}} |
* |
192 |
} |
* @param object Log_observer &$logObserver The Log_observer instance to |
193 |
|
* be added to the $listeners |
194 |
?> |
* array. |
195 |
|
* @access public |
196 |
|
*/ |
197 |
|
function attach(&$logObserver) |
198 |
|
{ |
199 |
|
if (!is_object($logObserver)) { |
200 |
|
return false; |
201 |
|
} |
202 |
|
|
203 |
|
$logObserver->_listenerID = uniqid(rand()); |
204 |
|
|
205 |
|
$this->_listeners[$logObserver->_listenerID] = &$logObserver; |
206 |
|
} |
207 |
|
|
208 |
|
/** |
209 |
|
* Removes a Log_observer instance from the list of observers. |
210 |
|
* |
211 |
|
* @param object Log_observer $logObserver The Log_observer instance to |
212 |
|
* be removed from the $listeners |
213 |
|
* array. |
214 |
|
* @access public |
215 |
|
*/ |
216 |
|
function detach($logObserver) |
217 |
|
{ |
218 |
|
if (isset($this->_listeners[$logObserver->_listenerID])) { |
219 |
|
unset($this->_listeners[$logObserver->_listenerID]); |
220 |
|
} |
221 |
|
} |
222 |
|
|
223 |
|
/** |
224 |
|
* Sends any Log_observer objects listening to this Log the message that |
225 |
|
* was just logged. |
226 |
|
* |
227 |
|
* @param array $msgObj The data structure holding all relevant log |
228 |
|
* information - the message, the priority, what |
229 |
|
* log this is, etc. |
230 |
|
*/ |
231 |
|
function notifyAll($msgObj) |
232 |
|
{ |
233 |
|
reset($this->_listeners); |
234 |
|
foreach ($this->_listeners as $listener) { |
235 |
|
if ($msgObj['priority'] <= $listener->priority) { |
236 |
|
$listener->notify($msgObj); |
237 |
|
} |
238 |
|
} |
239 |
|
} |
240 |
|
|
241 |
|
/** |
242 |
|
* Indicates whether this is a composite class. |
243 |
|
* |
244 |
|
* @return boolean True if this is a composite class. |
245 |
|
*/ |
246 |
|
function isComposite() |
247 |
|
{ |
248 |
|
return false; |
249 |
|
} |
250 |
|
} |
251 |
|
|
252 |
|
?> |