3 * $Header: /repository/pear/Log/Log.php,v 1.51 2005/09/06 05:54:36 jon Exp $
4 * $Horde: horde/lib/Log.php,v 1.15 2000/06/29 23:39:45 jon Exp $
6 * @version $Revision: 1.51 $
10 define('PEAR_LOG_EMERG', 0); /** System is unusable */
11 define('PEAR_LOG_ALERT', 1); /** Immediate action required */
12 define('PEAR_LOG_CRIT', 2); /** Critical conditions */
13 define('PEAR_LOG_ERR', 3); /** Error conditions */
14 define('PEAR_LOG_WARNING', 4); /** Warning conditions */
15 define('PEAR_LOG_NOTICE', 5); /** Normal but significant */
16 define('PEAR_LOG_INFO', 6); /** Informational */
17 define('PEAR_LOG_DEBUG', 7); /** Debug-level messages */
19 define('PEAR_LOG_ALL', bindec('11111111')); /** All messages */
20 define('PEAR_LOG_NONE', bindec('00000000')); /** No message */
22 /* Log types for PHP's native error_log() function. */
23 define('PEAR_LOG_TYPE_SYSTEM', 0); /** Use PHP's system logger */
24 define('PEAR_LOG_TYPE_MAIL', 1); /** Use PHP's mail() function */
25 define('PEAR_LOG_TYPE_DEBUG', 2); /** Use PHP's debugging connection */
26 define('PEAR_LOG_TYPE_FILE', 3); /** Append to a file */
29 * The Log:: class implements both an abstraction for various logging
30 * mechanisms and the Subject end of a Subject-Observer pattern.
32 * @author Chuck Hagenbuch <chuck@horde.org>
33 * @author Jon Parise <jon@php.net>
40 * Indicates whether or not the log can been opened / connected.
48 * Instance-specific unique identification number.
56 * The label that uniquely identifies this set of log messages.
64 * The default priority to use when logging an event.
69 var $_priority = PEAR_LOG_INFO
;
72 * The bitmask of allowed log levels.
76 var $_mask = PEAR_LOG_ALL
;
79 * Holds all Log_observer objects that wish to be notified of new messages.
84 var $_listeners = array();
88 * Attempts to return a concrete Log instance of type $handler.
90 * @param string $handler The type of concrete Log subclass to return.
91 * Attempt to dynamically include the code for
92 * this subclass. Currently, valid values are
93 * 'console', 'syslog', 'sql', 'file', and 'mcal'.
95 * @param string $name The name of the actually log file, table, or
96 * other specific store to use. Defaults to an
97 * empty string, with which the subclass will
98 * attempt to do something intelligent.
100 * @param string $ident The identity reported to the log system.
102 * @param array $conf A hash containing any additional configuration
103 * information that a subclass might need.
105 * @param int $level Log messages up to and including this level.
107 * @return object Log The newly created concrete Log instance, or an
112 function &factory($handler, $name = '', $ident = '', $conf = array(),
113 $level = PEAR_LOG_DEBUG
)
115 $handler = strtolower($handler);
116 $class = 'Log_' . $handler;
117 $classfile = 'Log/' . $handler . '.php';
120 * Attempt to include our version of the named class, but don't treat
121 * a failure as fatal. The caller may have already included their own
122 * version of the named class.
124 if (!class_exists($class)) {
125 @include_once
$classfile;
128 /* If the class exists, return a new instance of it. */
129 if (class_exists($class)) {
130 $obj = new $class($name, $ident, $conf, $level);
138 * Attempts to return a reference to a concrete Log instance of type
139 * $handler, only creating a new instance if no log instance with the same
140 * parameters currently exists.
142 * You should use this if there are multiple places you might create a
143 * logger, you don't want to create multiple loggers, and you don't want to
144 * check for the existence of one each time. The singleton pattern does all
145 * the checking work for you.
147 * <b>You MUST call this method with the $var = &Log::singleton() syntax.
148 * Without the ampersand (&) in front of the method name, you will not get
149 * a reference, you will get a copy.</b>
151 * @param string $handler The type of concrete Log subclass to return.
152 * Attempt to dynamically include the code for
153 * this subclass. Currently, valid values are
154 * 'console', 'syslog', 'sql', 'file', and 'mcal'.
156 * @param string $name The name of the actually log file, table, or
157 * other specific store to use. Defaults to an
158 * empty string, with which the subclass will
159 * attempt to do something intelligent.
161 * @param string $ident The identity reported to the log system.
163 * @param array $conf A hash containing any additional configuration
164 * information that a subclass might need.
166 * @param int $level Log messages up to and including this level.
168 * @return object Log The newly created concrete Log instance, or an
173 function &singleton($handler, $name = '', $ident = '', $conf = array(),
174 $level = PEAR_LOG_DEBUG
)
177 if (!isset($instances)) $instances = array();
179 $signature = serialize(array($handler, $name, $ident, $conf, $level));
180 if (!isset($instances[$signature])) {
181 $instances[$signature] = &Log
::factory($handler, $name, $ident,
185 return $instances[$signature];
189 * Abstract implementation of the open() method.
198 * Abstract implementation of the close() method.
207 * Abstract implementation of the flush() method.
216 * Abstract implementation of the log() method.
219 function log($message, $priority = null)
225 * A convenience function for logging a emergency event. It will log a
226 * message at the PEAR_LOG_EMERG log level.
228 * @param mixed $message String or object containing the message
231 * @return boolean True if the message was successfully logged.
236 function emerg($message)
238 return $this->log($message, PEAR_LOG_EMERG
);
242 * A convenience function for logging an alert event. It will log a
243 * message at the PEAR_LOG_ALERT log level.
245 * @param mixed $message String or object containing the message
248 * @return boolean True if the message was successfully logged.
253 function alert($message)
255 return $this->log($message, PEAR_LOG_ALERT
);
259 * A convenience function for logging a critical event. It will log a
260 * message at the PEAR_LOG_CRIT log level.
262 * @param mixed $message String or object containing the message
265 * @return boolean True if the message was successfully logged.
270 function crit($message)
272 return $this->log($message, PEAR_LOG_CRIT
);
276 * A convenience function for logging a error event. It will log a
277 * message at the PEAR_LOG_ERR log level.
279 * @param mixed $message String or object containing the message
282 * @return boolean True if the message was successfully logged.
287 function err($message)
289 return $this->log($message, PEAR_LOG_ERR
);
293 * A convenience function for logging a warning event. It will log a
294 * message at the PEAR_LOG_WARNING log level.
296 * @param mixed $message String or object containing the message
299 * @return boolean True if the message was successfully logged.
304 function warning($message)
306 return $this->log($message, PEAR_LOG_WARNING
);
310 * A convenience function for logging a notice event. It will log a
311 * message at the PEAR_LOG_NOTICE log level.
313 * @param mixed $message String or object containing the message
316 * @return boolean True if the message was successfully logged.
321 function notice($message)
323 return $this->log($message, PEAR_LOG_NOTICE
);
327 * A convenience function for logging a information event. It will log a
328 * message at the PEAR_LOG_INFO log level.
330 * @param mixed $message String or object containing the message
333 * @return boolean True if the message was successfully logged.
338 function info($message)
340 return $this->log($message, PEAR_LOG_INFO
);
344 * A convenience function for logging a debug event. It will log a
345 * message at the PEAR_LOG_DEBUG log level.
347 * @param mixed $message String or object containing the message
350 * @return boolean True if the message was successfully logged.
355 function debug($message)
357 return $this->log($message, PEAR_LOG_DEBUG
);
361 * Returns the string representation of the message data.
363 * If $message is an object, _extractMessage() will attempt to extract
364 * the message text using a known method (such as a PEAR_Error object's
365 * getMessage() method). If a known method, cannot be found, the
366 * serialized representation of the object will be returned.
368 * If the message data is already a string, it will be returned unchanged.
370 * @param mixed $message The original message data. This may be a
371 * string or any object.
373 * @return string The string representation of the message.
377 function _extractMessage($message)
380 * If we've been given an object, attempt to extract the message using
381 * a known method. If we can't find such a method, default to the
382 * "human-readable" version of the object.
384 * We also use the human-readable format for arrays.
386 if (is_object($message)) {
387 if (method_exists($message, 'getmessage')) {
388 $message = $message->getMessage();
389 } else if (method_exists($message, 'tostring')) {
390 $message = $message->toString();
391 } else if (method_exists($message, '__tostring')) {
392 if (version_compare(PHP_VERSION
, '5.0.0', 'ge')) {
393 $message = (string)$message;
395 $message = $message->__toString();
398 $message = print_r($message, true);
400 } else if (is_array($message)) {
401 if (isset($message['message'])) {
402 $message = $message['message'];
404 $message = print_r($message, true);
408 /* Otherwise, we assume the message is a string. */
413 * Returns the string representation of a PEAR_LOG_* integer constant.
415 * @param int $priority A PEAR_LOG_* integer constant.
417 * @return string The string representation of $level.
421 function priorityToString($priority)
424 PEAR_LOG_EMERG
=> 'emergency',
425 PEAR_LOG_ALERT
=> 'alert',
426 PEAR_LOG_CRIT
=> 'critical',
427 PEAR_LOG_ERR
=> 'error',
428 PEAR_LOG_WARNING
=> 'warning',
429 PEAR_LOG_NOTICE
=> 'notice',
430 PEAR_LOG_INFO
=> 'info',
431 PEAR_LOG_DEBUG
=> 'debug'
434 return $levels[$priority];
438 * Returns the the PEAR_LOG_* integer constant for the given string
439 * representation of a priority name. This function performs a
440 * case-insensitive search.
442 * @param string $name String containing a priority name.
444 * @return string The PEAR_LOG_* integer contstant corresponding
445 * the the specified priority name.
449 function stringToPriority($name)
452 'emergency' => PEAR_LOG_EMERG
,
453 'alert' => PEAR_LOG_ALERT
,
454 'critical' => PEAR_LOG_CRIT
,
455 'error' => PEAR_LOG_ERR
,
456 'warning' => PEAR_LOG_WARNING
,
457 'notice' => PEAR_LOG_NOTICE
,
458 'info' => PEAR_LOG_INFO
,
459 'debug' => PEAR_LOG_DEBUG
462 return $levels[strtolower($name)];
466 * Calculate the log mask for the given priority.
468 * @param integer $priority The priority whose mask will be calculated.
470 * @return integer The calculated log mask.
475 function MASK($priority)
477 return (1 << $priority);
481 * Calculate the log mask for all priorities up to the given priority.
483 * @param integer $priority The maximum priority covered by this mask.
485 * @return integer The calculated log mask.
490 function UPTO($priority)
492 return ((1 << ($priority +
1)) - 1);
496 * Set and return the level mask for the current Log instance.
498 * @param integer $mask A bitwise mask of log levels.
500 * @return integer The current level mask.
505 function setMask($mask)
507 $this->_mask
= $mask;
513 * Returns the current level mask.
515 * @return interger The current level mask.
526 * Check if the given priority is included in the current level mask.
528 * @param integer $priority The priority to check.
530 * @return boolean True if the given priority is included in the current
536 function _isMasked($priority)
538 return (Log
::MASK($priority) & $this->_mask
);
542 * Returns the current default priority.
544 * @return integer The current default priority.
549 function getPriority()
551 return $this->_priority
;
555 * Sets the default priority to the specified value.
557 * @param integer $priority The new default priority.
562 function setPriority($priority)
564 $this->_priority
= $priority;
568 * Adds a Log_observer instance to the list of observers that are listening
569 * for messages emitted by this Log instance.
571 * @param object $observer The Log_observer instance to attach as a
574 * @param boolean True if the observer is successfully attached.
579 function attach(&$observer)
581 if (!is_a($observer, 'Log_observer')) {
585 $this->_listeners
[$observer->_id
] = &$observer;
591 * Removes a Log_observer instance from the list of observers.
593 * @param object $observer The Log_observer instance to detach from
594 * the list of listeners.
596 * @param boolean True if the observer is successfully detached.
601 function detach($observer)
603 if (!is_a($observer, 'Log_observer') ||
604 !isset($this->_listeners
[$observer->_id
])) {
608 unset($this->_listeners
[$observer->_id
]);
614 * Informs each registered observer instance that a new message has been
617 * @param array $event A hash describing the log event.
621 function _announce($event)
623 foreach ($this->_listeners
as $id => $listener) {
624 if ($event['priority'] <= $this->_listeners
[$id]->_priority
) {
625 $this->_listeners
[$id]->notify($event);
631 * Indicates whether this is a composite class.
633 * @return boolean True if this is a composite class.
638 function isComposite()
644 * Sets this Log instance's identification string.
646 * @param string $ident The new identification string.
651 function setIdent($ident)
653 $this->_ident
= $ident;
657 * Returns the current identification string.
659 * @return string The current Log instance's identification string.
666 return $this->_ident
;