ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
Log.php
Go to the documentation of this file.
1<?php
10define('PEAR_LOG_EMERG', 0); /* System is unusable */
11define('PEAR_LOG_ALERT', 1); /* Immediate action required */
12define('PEAR_LOG_CRIT', 2); /* Critical conditions */
13define('PEAR_LOG_ERR', 3); /* Error conditions */
14define('PEAR_LOG_WARNING', 4); /* Warning conditions */
15define('PEAR_LOG_NOTICE', 5); /* Normal but significant */
16define('PEAR_LOG_INFO', 6); /* Informational */
17define('PEAR_LOG_DEBUG', 7); /* Debug-level messages */
18
19define('PEAR_LOG_ALL', 0xffffffff); /* All messages */
20define('PEAR_LOG_NONE', 0x00000000); /* No message */
21
22/* Log types for PHP's native error_log() function. */
23define('PEAR_LOG_TYPE_SYSTEM', 0); /* Use PHP's system logger */
24define('PEAR_LOG_TYPE_MAIL', 1); /* Use PHP's mail() function */
25define('PEAR_LOG_TYPE_DEBUG', 2); /* Use PHP's debugging connection */
26define('PEAR_LOG_TYPE_FILE', 3); /* Append to a file */
27
37class Log
38{
45 var $_opened = false;
46
53 var $_id = 0;
54
61 var $_ident = '';
62
70
78
85 var $_listeners = array();
86
94 var $_formatMap = array('%{timestamp}' => '%1$s',
95 '%{ident}' => '%2$s',
96 '%{priority}' => '%3$s',
97 '%{message}' => '%4$s',
98 '%{file}' => '%5$s',
99 '%{line}' => '%6$s',
100 '%{function}' => '%7$s',
101 '%{class}' => '%8$s',
102 '%\{' => '%%{');
103
117 function _classExists($class)
118 {
119 if (version_compare(PHP_VERSION, '5.0.0', 'ge')) {
120 return class_exists($class, false);
121 }
122
123 return class_exists($class);
124 }
125
151 function &factory($handler, $name = '', $ident = '', $conf = array(),
152 $level = PEAR_LOG_DEBUG)
153 {
154 $handler = strtolower($handler);
155 $class = 'Log_' . $handler;
156 $classfile = 'Log/' . $handler . '.php';
157
158 /*
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.
162 */
163 if (!Log::_classExists($class)) {
164 include_once $classfile;
165 }
166
167 /* If the class exists, return a new instance of it. */
168 if (Log::_classExists($class)) {
169 $obj = new $class($name, $ident, $conf, $level);
170 return $obj;
171 }
172
173 $null = null;
174 return $null;
175 }
176
213 function &singleton($handler, $name = '', $ident = '', $conf = array(),
214 $level = PEAR_LOG_DEBUG)
215 {
216 static $instances;
217 if (!isset($instances)) $instances = array();
218
219 $signature = serialize(array($handler, $name, $ident, $conf, $level));
220 if (!isset($instances[$signature])) {
221 $instances[$signature] = &Log::factory($handler, $name, $ident,
222 $conf, $level);
223 }
224
225 return $instances[$signature];
226 }
227
232 function open()
233 {
234 return false;
235 }
236
241 function close()
242 {
243 return false;
244 }
245
250 function flush()
251 {
252 return false;
253 }
254
259 function log($message, $priority = null)
260 {
261 return false;
262 }
263
276 function emerg($message)
277 {
278 return $this->log($message, PEAR_LOG_EMERG);
279 }
280
293 function alert($message)
294 {
295 return $this->log($message, PEAR_LOG_ALERT);
296 }
297
310 function crit($message)
311 {
312 return $this->log($message, PEAR_LOG_CRIT);
313 }
314
327 function err($message)
328 {
329 return $this->log($message, PEAR_LOG_ERR);
330 }
331
344 function warning($message)
345 {
346 return $this->log($message, PEAR_LOG_WARNING);
347 }
348
361 function notice($message)
362 {
363 return $this->log($message, PEAR_LOG_NOTICE);
364 }
365
378 function info($message)
379 {
380 return $this->log($message, PEAR_LOG_INFO);
381 }
382
395 function debug($message)
396 {
397 return $this->log($message, PEAR_LOG_DEBUG);
398 }
399
417 function _extractMessage($message)
418 {
419 /*
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.
423 *
424 * We also use the human-readable format for arrays.
425 */
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;
434 } else {
435 $message = $message->__toString();
436 }
437 } else {
438 $message = var_export($message, true);
439 }
440 } else if (is_array($message)) {
441 if (isset($message['message'])) {
442 if (is_scalar($message['message'])) {
443 $message = $message['message'];
444 } else {
445 $message = var_export($message['message'], true);
446 }
447 } else {
448 $message = var_export($message, true);
449 }
450 } else if (is_bool($message) || $message === NULL) {
451 $message = var_export($message, true);
452 }
453
454 /* Otherwise, we assume the message is a string. */
455 return $message;
456 }
457
472 function _getBacktraceVars($depth)
473 {
474 /* Start by generating a backtrace from the current call (here). */
475 $bt = debug_backtrace();
476
477 /*
478 * If we were ultimately invoked by the composite handler, we need to
479 * increase our depth one additional level to compensate.
480 */
481 $class = isset($bt[$depth+1]['class']) ? $bt[$depth+1]['class'] : null;
482 if ($class !== null && strcasecmp($class, 'Log_composite') == 0) {
483 $depth++;
484 $class = isset($bt[$depth + 1]) ? $bt[$depth + 1]['class'] : null;
485 }
486
487 /*
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.
493 */
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;
497
498 /*
499 * However, if log() was called from one of our "shortcut" functions,
500 * we're going to need to go back an additional step.
501 */
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 }
508
509 /*
510 * If we couldn't extract a function name (perhaps because we were
511 * executed from the "main" context), provide a default value.
512 */
513 if (is_null($func)) {
514 $func = '(none)';
515 }
516
517 /* Return a 4-tuple containing (file, line, function, class). */
518 return array($file, $line, $func, $class);
519 }
520
530 function _format($format, $timestamp, $priority, $message)
531 {
532 /*
533 * If the format string references any of the backtrace-driven
534 * variables (%5 %6,%7,%8), generate the backtrace and fetch them.
535 */
536 if (preg_match('/%[5678]/', $format)) {
537 list($file, $line, $func, $class) = $this->_getBacktraceVars(2);
538 }
539
540 /*
541 * Build the formatted string. We use the sprintf() function's
542 * "argument swapping" capability to dynamically select and position
543 * the variables which will ultimately appear in the log string.
544 */
545 return sprintf($format,
547 $this->_ident,
548 $this->priorityToString($priority),
549 $message,
550 isset($file) ? $file : '',
551 isset($line) ? $line : '',
552 isset($func) ? $func : '',
553 isset($class) ? $class : '');
554 }
555
566 function priorityToString($priority)
567 {
568 $levels = array(
569 PEAR_LOG_EMERG => 'emergency',
570 PEAR_LOG_ALERT => 'alert',
571 PEAR_LOG_CRIT => 'critical',
572 PEAR_LOG_ERR => 'error',
573 PEAR_LOG_WARNING => 'warning',
574 PEAR_LOG_NOTICE => 'notice',
575 PEAR_LOG_INFO => 'info',
576 PEAR_LOG_DEBUG => 'debug'
577 );
578
579 return $levels[$priority];
580 }
581
595 function stringToPriority($name)
596 {
597 $levels = array(
598 'emergency' => PEAR_LOG_EMERG,
599 'alert' => PEAR_LOG_ALERT,
600 'critical' => PEAR_LOG_CRIT,
601 'error' => PEAR_LOG_ERR,
602 'warning' => PEAR_LOG_WARNING,
603 'notice' => PEAR_LOG_NOTICE,
604 'info' => PEAR_LOG_INFO,
605 'debug' => PEAR_LOG_DEBUG
606 );
607
608 return $levels[strtolower($name)];
609 }
610
623 function MASK($priority)
624 {
625 return (1 << $priority);
626 }
627
642 function UPTO($priority)
643 {
644 return Log::MAX($priority);
645 }
646
661 function MIN($priority)
662 {
663 return PEAR_LOG_ALL ^ ((1 << $priority) - 1);
664 }
665
680 function MAX($priority)
681 {
682 return ((1 << ($priority + 1)) - 1);
683 }
684
695 function setMask($mask)
696 {
697 $this->_mask = $mask;
698
699 return $this->_mask;
700 }
701
710 function getMask()
711 {
712 return $this->_mask;
713 }
714
726 function _isMasked($priority)
727 {
728 return (Log::MASK($priority) & $this->_mask);
729 }
730
739 function getPriority()
740 {
741 return $this->_priority;
742 }
743
752 function setPriority($priority)
753 {
754 $this->_priority = $priority;
755 }
756
769 function attach(&$observer)
770 {
771 if (!is_a($observer, 'Log_observer')) {
772 return false;
773 }
774
775 $this->_listeners[$observer->_id] = &$observer;
776
777 return true;
778 }
779
791 function detach($observer)
792 {
793 if (!is_a($observer, 'Log_observer') ||
794 !isset($this->_listeners[$observer->_id])) {
795 return false;
796 }
797
798 unset($this->_listeners[$observer->_id]);
799
800 return true;
801 }
802
811 function _announce($event)
812 {
813 foreach ($this->_listeners as $id => $listener) {
814 if ($event['priority'] <= $this->_listeners[$id]->_priority) {
815 $this->_listeners[$id]->notify($event);
816 }
817 }
818 }
819
828 function isComposite()
829 {
830 return false;
831 }
832
841 function setIdent($ident)
842 {
843 $this->_ident = $ident;
844 }
845
854 function getIdent()
855 {
856 return $this->_ident;
857 }
858}
print $file
const PEAR_LOG_ALERT
Definition: Log.php:11
const PEAR_LOG_EMERG
Definition: Log.php:10
const PEAR_LOG_ERR
Definition: Log.php:13
const PEAR_LOG_CRIT
Definition: Log.php:12
const PEAR_LOG_DEBUG
Definition: Log.php:17
const PEAR_LOG_INFO
Definition: Log.php:16
const PEAR_LOG_ALL
Definition: Log.php:19
const PEAR_LOG_WARNING
Definition: Log.php:14
const PEAR_LOG_NOTICE
Definition: Log.php:15
foreach($mandatory_scripts as $file) $timestamp
Definition: buildRTE.php:81
stringToPriority($name)
Returns the the PEAR_LOG_* integer constant for the given string representation of a priority name.
Definition: Log.php:595
debug($message)
A convenience function for logging a debug event.
Definition: Log.php:395
UPTO($priority)
Calculate the log mask for all priorities up to the given priority.
Definition: Log.php:642
$_priority
Definition: Log.php:69
getIdent()
Returns the current identification string.
Definition: Log.php:854
_isMasked($priority)
Check if the given priority is included in the current level mask.
Definition: Log.php:726
setMask($mask)
Set and return the level mask for the current Log instance.
Definition: Log.php:695
flush()
Abstract implementation of the flush() method.
Definition: Log.php:250
priorityToString($priority)
Returns the string representation of a PEAR_LOG_* integer constant.
Definition: Log.php:566
$_mask
Definition: Log.php:77
crit($message)
A convenience function for logging a critical event.
Definition: Log.php:310
MIN($priority)
Calculate the log mask for all priorities greater than or equal to the given priority.
Definition: Log.php:661
close()
Abstract implementation of the close() method.
Definition: Log.php:241
MAX($priority)
Calculate the log mask for all priorities less than or equal to the given priority.
Definition: Log.php:680
MASK($priority)
Calculate the log mask for the given priority.
Definition: Log.php:623
emerg($message)
A convenience function for logging a emergency event.
Definition: Log.php:276
_classExists($class)
Utility function which wraps PHP's class_exists() function to ensure consistent behavior between PHP ...
Definition: Log.php:117
$_opened
Definition: Log.php:45
getMask()
Returns the current level mask.
Definition: Log.php:710
$_id
Definition: Log.php:53
$_listeners
Definition: Log.php:85
& factory($handler, $name='', $ident='', $conf=array(), $level=PEAR_LOG_DEBUG)
Attempts to return a concrete Log instance of type $handler.
Definition: Log.php:151
attach(&$observer)
Adds a Log_observer instance to the list of observers that are listening for messages emitted by this...
Definition: Log.php:769
err($message)
A convenience function for logging a error event.
Definition: Log.php:327
notice($message)
A convenience function for logging a notice event.
Definition: Log.php:361
info($message)
A convenience function for logging a information event.
Definition: Log.php:378
_announce($event)
Informs each registered observer instance that a new message has been logged.
Definition: Log.php:811
_extractMessage($message)
Returns the string representation of the message data.
Definition: Log.php:417
& singleton($handler, $name='', $ident='', $conf=array(), $level=PEAR_LOG_DEBUG)
Attempts to return a reference to a concrete Log instance of type $handler, only creating a new insta...
Definition: Log.php:213
$_formatMap
Definition: Log.php:94
detach($observer)
Removes a Log_observer instance from the list of observers.
Definition: Log.php:791
_getBacktraceVars($depth)
Using debug_backtrace(), returns the file, line, and enclosing function name of the source code conte...
Definition: Log.php:472
open()
Abstract implementation of the open() method.
Definition: Log.php:232
setIdent($ident)
Sets this Log instance's identification string.
Definition: Log.php:841
isComposite()
Indicates whether this is a composite class.
Definition: Log.php:828
$_ident
Definition: Log.php:61
_format($format, $timestamp, $priority, $message)
Produces a formatted log line based on a format string and a set of variables representing the curren...
Definition: Log.php:530
setPriority($priority)
Sets the default priority to the specified value.
Definition: Log.php:752
warning($message)
A convenience function for logging a warning event.
Definition: Log.php:344
getPriority()
Returns the current default priority.
Definition: Log.php:739
alert($message)
A convenience function for logging an alert event.
Definition: Log.php:293
log($message, $priority=null)
Abstract implementation of the log() method.
Definition: Log.php:259
$mask
Definition: example_042.php:90
The Log:: class implements both an abstraction for various logging mechanisms and the Subject end of ...