4 * $Horde: horde/lib/Log.php,v 1.15 2000/06/29 23:39:45 jon Exp $
6 * @version $Revision: 284340 $
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', 0xffffffff); /* All messages */
20 define('PEAR_LOG_NONE', 0x00000000); /* 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.
77 var $_mask = PEAR_LOG_ALL;
80 * Holds all Log_observer objects that wish to be notified of new messages.
85 var $_listeners = array();
88 * Maps canonical format keys to position arguments for use in building
89 * "line format" strings.
94 var $_formatMap = array('%{timestamp}' => '%1$s',
96 '%{priority}' => '%3$s',
97 '%{message}' => '%4$s',
100 '%{function}' => '%7$s',
101 '%{class}' => '%8$s',
105 * Utility function which wraps PHP's class_exists() function to ensure
106 * consistent behavior between PHP versions 4 and 5. Autoloading behavior
107 * is always disabled.
109 * @param string $class The name of the class whose existence should
112 * @return bool True if the class exists.
117 function _classExists($class)
119 if (version_compare(PHP_VERSION, '5.0.0', 'ge')) {
120 return class_exists($class, false);
123 return class_exists($class);
127 * Attempts to return a concrete Log instance of type $handler.
129 * @param string $handler The type of concrete Log subclass to return.
130 * Attempt to dynamically include the code for
131 * this subclass. Currently, valid values are
132 * 'console', 'syslog', 'sql', 'file', and 'mcal'.
134 * @param string $name The name of the actually log file, table, or
135 * other specific store to use. Defaults to an
136 * empty string, with which the subclass will
137 * attempt to do something intelligent.
139 * @param string $ident The identity reported to the log system.
141 * @param array $conf A hash containing any additional configuration
142 * information that a subclass might need.
144 * @param int $level Log messages up to and including this level.
146 * @return object Log The newly created concrete Log instance, or
151 function &factory($handler, $name = '', $ident = '', $conf = array(),
152 $level = PEAR_LOG_DEBUG)
154 $handler = strtolower($handler);
155 $class = 'Log_' . $handler;
156 $classfile = 'Log/' . $handler . '.php';
159 * Attempt to include our version of the named class, but don't treat
160 * a failure as fatal. The caller may have already included their own
161 * version of the named class.
163 if (!Log::_classExists($class)) {
164 include_once $classfile;
167 /* If the class exists, return a new instance of it. */
168 if (Log::_classExists($class)) {
169 $obj = &new $class($name, $ident, $conf, $level);
178 * Attempts to return a reference to a concrete Log instance of type
179 * $handler, only creating a new instance if no log instance with the same
180 * parameters currently exists.
182 * You should use this if there are multiple places you might create a
183 * logger, you don't want to create multiple loggers, and you don't want to
184 * check for the existance of one each time. The singleton pattern does all
185 * the checking work for you.
187 * <b>You MUST call this method with the $var = &Log::singleton() syntax.
188 * Without the ampersand (&) in front of the method name, you will not get
189 * a reference, you will get a copy.</b>
191 * @param string $handler The type of concrete Log subclass to return.
192 * Attempt to dynamically include the code for
193 * this subclass. Currently, valid values are
194 * 'console', 'syslog', 'sql', 'file', and 'mcal'.
196 * @param string $name The name of the actually log file, table, or
197 * other specific store to use. Defaults to an
198 * empty string, with which the subclass will
199 * attempt to do something intelligent.
201 * @param string $ident The identity reported to the log system.
203 * @param array $conf A hash containing any additional configuration
204 * information that a subclass might need.
206 * @param int $level Log messages up to and including this level.
208 * @return object Log The newly created concrete Log instance, or
213 function &singleton($handler, $name = '', $ident = '', $conf = array(),
214 $level = PEAR_LOG_DEBUG)
217 if (!isset($instances)) $instances = array();
219 $signature = serialize(array($handler, $name, $ident, $conf, $level));
220 if (!isset($instances[$signature])) {
221 $instances[$signature] = &Log::factory($handler, $name, $ident,
225 return $instances[$signature];
229 * Abstract implementation of the open() method.
238 * Abstract implementation of the close() method.
247 * Abstract implementation of the flush() method.
256 * Abstract implementation of the log() method.
259 function log($message, $priority = null)
265 * A convenience function for logging a emergency event. It will log a
266 * message at the PEAR_LOG_EMERG log level.
268 * @param mixed $message String or object containing the message
271 * @return boolean True if the message was successfully logged.
276 function emerg($message)
278 return $this->log($message, PEAR_LOG_EMERG);
282 * A convenience function for logging an alert event. It will log a
283 * message at the PEAR_LOG_ALERT log level.
285 * @param mixed $message String or object containing the message
288 * @return boolean True if the message was successfully logged.
293 function alert($message)
295 return $this->log($message, PEAR_LOG_ALERT);
299 * A convenience function for logging a critical event. It will log a
300 * message at the PEAR_LOG_CRIT log level.
302 * @param mixed $message String or object containing the message
305 * @return boolean True if the message was successfully logged.
310 function crit($message)
312 return $this->log($message, PEAR_LOG_CRIT);
316 * A convenience function for logging a error event. It will log a
317 * message at the PEAR_LOG_ERR log level.
319 * @param mixed $message String or object containing the message
322 * @return boolean True if the message was successfully logged.
327 function err($message)
329 return $this->log($message, PEAR_LOG_ERR);
333 * A convenience function for logging a warning event. It will log a
334 * message at the PEAR_LOG_WARNING log level.
336 * @param mixed $message String or object containing the message
339 * @return boolean True if the message was successfully logged.
344 function warning($message)
346 return $this->log($message, PEAR_LOG_WARNING);
350 * A convenience function for logging a notice event. It will log a
351 * message at the PEAR_LOG_NOTICE log level.
353 * @param mixed $message String or object containing the message
356 * @return boolean True if the message was successfully logged.
361 function notice($message)
363 return $this->log($message, PEAR_LOG_NOTICE);
367 * A convenience function for logging a information event. It will log a
368 * message at the PEAR_LOG_INFO log level.
370 * @param mixed $message String or object containing the message
373 * @return boolean True if the message was successfully logged.
378 function info($message)
380 return $this->log($message, PEAR_LOG_INFO);
384 * A convenience function for logging a debug event. It will log a
385 * message at the PEAR_LOG_DEBUG log level.
387 * @param mixed $message String or object containing the message
390 * @return boolean True if the message was successfully logged.
395 function debug($message)
397 return $this->log($message, PEAR_LOG_DEBUG);
401 * Returns the string representation of the message data.
403 * If $message is an object, _extractMessage() will attempt to extract
404 * the message text using a known method (such as a PEAR_Error object's
405 * getMessage() method). If a known method, cannot be found, the
406 * serialized representation of the object will be returned.
408 * If the message data is already a string, it will be returned unchanged.
410 * @param mixed $message The original message data. This may be a
411 * string or any object.
413 * @return string The string representation of the message.
417 function _extractMessage($message)
420 * If we've been given an object, attempt to extract the message using
421 * a known method. If we can't find such a method, default to the
422 * "human-readable" version of the object.
424 * We also use the human-readable format for arrays.
426 if (is_object($message)) {
427 if (method_exists($message, 'getmessage')) {
428 $message = $message->getMessage();
429 } else if (method_exists($message, 'tostring')) {
430 $message = $message->toString();
431 } else if (method_exists($message, '__tostring')) {
432 if (version_compare(PHP_VERSION, '5.0.0', 'ge')) {
433 $message = (string)$message;
435 $message = $message->__toString();
438 $message = var_export($message, true);
440 } else if (is_array($message)) {
441 if (isset($message['message'])) {
442 if (is_scalar($message['message'])) {
443 $message = $message['message'];
445 $message = var_export($message['message'], true);
448 $message = var_export($message, true);
450 } else if (is_bool($message) || $message === NULL) {
451 $message = var_export($message, true);
454 /* Otherwise, we assume the message is a string. */
459 * Using debug_backtrace(), returns the file, line, and enclosing function
460 * name of the source code context from which log() was invoked.
462 * @param int $depth The initial number of frames we should step
463 * back into the trace.
465 * @return array Array containing four strings: the filename, the line,
466 * the function name, and the class name from which log()
472 function _getBacktraceVars($depth)
474 /* Start by generating a backtrace from the current call (here). */
475 $bt = debug_backtrace();
478 * If we were ultimately invoked by the composite handler, we need to
479 * increase our depth one additional level to compensate.
481 $class = isset($bt[$depth+1]['class']) ? $bt[$depth+1]['class'] : null;
482 if ($class !== null && strcasecmp($class, 'Log_composite') == 0) {
484 $class = isset($bt[$depth + 1]) ? $bt[$depth + 1]['class'] : null;
488 * We're interested in the frame which invoked the log() function, so
489 * we need to walk back some number of frames into the backtrace. The
490 * $depth parameter tells us where to start looking. We go one step
491 * further back to find the name of the encapsulating function from
492 * which log() was called.
494 $file = isset($bt[$depth]) ? $bt[$depth]['file'] : null;
495 $line = isset($bt[$depth]) ? $bt[$depth]['line'] : 0;
496 $func = isset($bt[$depth + 1]) ? $bt[$depth + 1]['function'] : null;
499 * However, if log() was called from one of our "shortcut" functions,
500 * we're going to need to go back an additional step.
502 if (in_array($func, array('emerg', 'alert', 'crit', 'err', 'warning',
503 'notice', 'info', 'debug'))) {
504 $file = isset($bt[$depth + 1]) ? $bt[$depth + 1]['file'] : null;
505 $line = isset($bt[$depth + 1]) ? $bt[$depth + 1]['line'] : 0;
506 $func = isset($bt[$depth + 2]) ? $bt[$depth + 2]['function'] : null;
507 $class = isset($bt[$depth + 2]) ? $bt[$depth + 2]['class'] : null;
511 * If we couldn't extract a function name (perhaps because we were
512 * executed from the "main" context), provide a default value.
514 if (is_null($func)) {
518 /* Return a 4-tuple containing (file, line, function, class). */
519 return array($file, $line, $func, $class);
523 * Produces a formatted log line based on a format string and a set of
524 * variables representing the current log record and state.
526 * @return string Formatted log string.
531 function _format($format, $timestamp, $priority, $message)
534 * If the format string references any of the backtrace-driven
535 * variables (%5 %6,%7,%8), generate the backtrace and fetch them.
537 if (preg_match('/%[5678]/', $format)) {
538 list($file, $line, $func, $class) = $this->_getBacktraceVars(2);
542 * Build the formatted string. We use the sprintf() function's
543 * "argument swapping" capability to dynamically select and position
544 * the variables which will ultimately appear in the log string.
546 return sprintf($format,
549 $this->priorityToString($priority),
551 isset($file) ? $file : '',
552 isset($line) ? $line : '',
553 isset($func) ? $func : '',
554 isset($class) ? $class : '');
558 * Returns the string representation of a PEAR_LOG_* integer constant.
560 * @param int $priority A PEAR_LOG_* integer constant.
562 * @return string The string representation of $level.
567 function priorityToString($priority)
570 PEAR_LOG_EMERG => 'emergency',
571 PEAR_LOG_ALERT => 'alert',
572 PEAR_LOG_CRIT => 'critical',
573 PEAR_LOG_ERR => 'error',
574 PEAR_LOG_WARNING => 'warning',
575 PEAR_LOG_NOTICE => 'notice',
576 PEAR_LOG_INFO => 'info',
577 PEAR_LOG_DEBUG => 'debug'
580 return $levels[$priority];
584 * Returns the the PEAR_LOG_* integer constant for the given string
585 * representation of a priority name. This function performs a
586 * case-insensitive search.
588 * @param string $name String containing a priority name.
590 * @return string The PEAR_LOG_* integer contstant corresponding
591 * the the specified priority name.
596 function stringToPriority($name)
599 'emergency' => PEAR_LOG_EMERG,
600 'alert' => PEAR_LOG_ALERT,
601 'critical' => PEAR_LOG_CRIT,
602 'error' => PEAR_LOG_ERR,
603 'warning' => PEAR_LOG_WARNING,
604 'notice' => PEAR_LOG_NOTICE,
605 'info' => PEAR_LOG_INFO,
606 'debug' => PEAR_LOG_DEBUG
609 return $levels[strtolower($name)];
613 * Calculate the log mask for the given priority.
615 * This method may be called statically.
617 * @param integer $priority The priority whose mask will be calculated.
619 * @return integer The calculated log mask.
624 function MASK($priority)
626 return (1 << $priority);
630 * Calculate the log mask for all priorities up to the given priority.
632 * This method may be called statically.
634 * @param integer $priority The maximum priority covered by this mask.
636 * @return integer The resulting log mask.
641 * @deprecated deprecated since Log 1.9.4; use Log::MAX() instead
643 function UPTO($priority)
645 return Log::MAX($priority);
649 * Calculate the log mask for all priorities greater than or equal to the
650 * given priority. In other words, $priority will be the lowest priority
651 * matched by the resulting mask.
653 * This method may be called statically.
655 * @param integer $priority The minimum priority covered by this mask.
657 * @return integer The resulting log mask.
662 function MIN($priority)
664 return PEAR_LOG_ALL ^ ((1 << $priority) - 1);
668 * Calculate the log mask for all priorities less than or equal to the
669 * given priority. In other words, $priority will be the highests priority
670 * matched by the resulting mask.
672 * This method may be called statically.
674 * @param integer $priority The maximum priority covered by this mask.
676 * @return integer The resulting log mask.
681 function MAX($priority)
683 return ((1 << ($priority + 1)) - 1);
687 * Set and return the level mask for the current Log instance.
689 * @param integer $mask A bitwise mask of log levels.
691 * @return integer The current level mask.
696 function setMask($mask)
698 $this->_mask = $mask;
704 * Returns the current level mask.
706 * @return interger The current level mask.
717 * Check if the given priority is included in the current level mask.
719 * @param integer $priority The priority to check.
721 * @return boolean True if the given priority is included in the current
727 function _isMasked($priority)
729 return (Log::MASK($priority) & $this->_mask);
733 * Returns the current default priority.
735 * @return integer The current default priority.
740 function getPriority()
742 return $this->_priority;
746 * Sets the default priority to the specified value.
748 * @param integer $priority The new default priority.
753 function setPriority($priority)
755 $this->_priority = $priority;
759 * Adds a Log_observer instance to the list of observers that are listening
760 * for messages emitted by this Log instance.
762 * @param object $observer The Log_observer instance to attach as a
765 * @param boolean True if the observer is successfully attached.
770 function attach(&$observer)
772 if (!is_a($observer, 'Log_observer')) {
776 $this->_listeners[$observer->_id] = &$observer;
782 * Removes a Log_observer instance from the list of observers.
784 * @param object $observer The Log_observer instance to detach from
785 * the list of listeners.
787 * @param boolean True if the observer is successfully detached.
792 function detach($observer)
794 if (!is_a($observer, 'Log_observer') ||
795 !isset($this->_listeners[$observer->_id])) {
799 unset($this->_listeners[$observer->_id]);
805 * Informs each registered observer instance that a new message has been
808 * @param array $event A hash describing the log event.
812 function _announce($event)
814 foreach ($this->_listeners as $id => $listener) {
815 if ($event['priority'] <= $this->_listeners[$id]->_priority) {
816 $this->_listeners[$id]->notify($event);
822 * Indicates whether this is a composite class.
824 * @return boolean True if this is a composite class.
829 function isComposite()
835 * Sets this Log instance's identification string.
837 * @param string $ident The new identification string.
842 function setIdent($ident)
844 $this->_ident = $ident;
848 * Returns the current identification string.
850 * @return string The current Log instance's identification string.
857 return $this->_ident;