[ Index ] |
|
Code source de Flux CMS 1.5 |
1 <?php 2 /** 3 * $Header: /repository/pear/Log/Log.php,v 1.52 2005/10/26 05:46:55 jon Exp $ 4 * $Horde: horde/lib/Log.php,v 1.15 2000/06/29 23:39:45 jon Exp $ 5 * 6 * @version $Revision: 1.52 $ 7 * @package Log 8 */ 9 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 */ 18 19 define('PEAR_LOG_ALL', bindec('11111111')); /** All messages */ 20 define('PEAR_LOG_NONE', bindec('00000000')); /** No message */ 21 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 */ 27 28 /** 29 * The Log:: class implements both an abstraction for various logging 30 * mechanisms and the Subject end of a Subject-Observer pattern. 31 * 32 * @author Chuck Hagenbuch <chuck@horde.org> 33 * @author Jon Parise <jon@php.net> 34 * @since Horde 1.3 35 * @package Log 36 */ 37 class Log 38 { 39 /** 40 * Indicates whether or not the log can been opened / connected. 41 * 42 * @var boolean 43 * @access private 44 */ 45 var $_opened = false; 46 47 /** 48 * Instance-specific unique identification number. 49 * 50 * @var integer 51 * @access private 52 */ 53 var $_id = 0; 54 55 /** 56 * The label that uniquely identifies this set of log messages. 57 * 58 * @var string 59 * @access private 60 */ 61 var $_ident = ''; 62 63 /** 64 * The default priority to use when logging an event. 65 * 66 * @var integer 67 * @access private 68 */ 69 var $_priority = PEAR_LOG_INFO; 70 71 /** 72 * The bitmask of allowed log levels. 73 * @var integer 74 * @access private 75 */ 76 var $_mask = PEAR_LOG_ALL; 77 78 /** 79 * Holds all Log_observer objects that wish to be notified of new messages. 80 * 81 * @var array 82 * @access private 83 */ 84 var $_listeners = array(); 85 86 87 /** 88 * Attempts to return a concrete Log instance of type $handler. 89 * 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'. 94 * 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. 99 * 100 * @param string $ident The identity reported to the log system. 101 * 102 * @param array $conf A hash containing any additional configuration 103 * information that a subclass might need. 104 * 105 * @param int $level Log messages up to and including this level. 106 * 107 * @return object Log The newly created concrete Log instance, or 108 * null on an error. 109 * @access public 110 * @since Log 1.0 111 */ 112 function &factory($handler, $name = '', $ident = '', $conf = array(), 113 $level = PEAR_LOG_DEBUG) 114 { 115 $handler = strtolower($handler); 116 $class = 'Log_' . $handler; 117 $classfile = 'Log/' . $handler . '.php'; 118 119 /* 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. 123 */ 124 if (!class_exists($class)) { 125 @include_once $classfile; 126 } 127 128 /* If the class exists, return a new instance of it. */ 129 if (class_exists($class)) { 130 $obj = new $class($name, $ident, $conf, $level); 131 return $obj; 132 } 133 134 return null; 135 } 136 137 /** 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. 141 * 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 existance of one each time. The singleton pattern does all 145 * the checking work for you. 146 * 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> 150 * 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'. 155 * 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. 160 * 161 * @param string $ident The identity reported to the log system. 162 * 163 * @param array $conf A hash containing any additional configuration 164 * information that a subclass might need. 165 * 166 * @param int $level Log messages up to and including this level. 167 * 168 * @return object Log The newly created concrete Log instance, or 169 * null on an error. 170 * @access public 171 * @since Log 1.0 172 */ 173 function &singleton($handler, $name = '', $ident = '', $conf = array(), 174 $level = PEAR_LOG_DEBUG) 175 { 176 static $instances; 177 if (!isset($instances)) $instances = array(); 178 179 $signature = serialize(array($handler, $name, $ident, $conf, $level)); 180 if (!isset($instances[$signature])) { 181 $instances[$signature] = &Log::factory($handler, $name, $ident, 182 $conf, $level); 183 } 184 185 return $instances[$signature]; 186 } 187 188 /** 189 * Abstract implementation of the open() method. 190 * @since Log 1.0 191 */ 192 function open() 193 { 194 return false; 195 } 196 197 /** 198 * Abstract implementation of the close() method. 199 * @since Log 1.0 200 */ 201 function close() 202 { 203 return false; 204 } 205 206 /** 207 * Abstract implementation of the flush() method. 208 * @since Log 1.8.2 209 */ 210 function flush() 211 { 212 return false; 213 } 214 215 /** 216 * Abstract implementation of the log() method. 217 * @since Log 1.0 218 */ 219 function log($message, $priority = null) 220 { 221 return false; 222 } 223 224 /** 225 * A convenience function for logging a emergency event. It will log a 226 * message at the PEAR_LOG_EMERG log level. 227 * 228 * @param mixed $message String or object containing the message 229 * to log. 230 * 231 * @return boolean True if the message was successfully logged. 232 * 233 * @access public 234 * @since Log 1.7.0 235 */ 236 function emerg($message) 237 { 238 return $this->log($message, PEAR_LOG_EMERG); 239 } 240 241 /** 242 * A convenience function for logging an alert event. It will log a 243 * message at the PEAR_LOG_ALERT log level. 244 * 245 * @param mixed $message String or object containing the message 246 * to log. 247 * 248 * @return boolean True if the message was successfully logged. 249 * 250 * @access public 251 * @since Log 1.7.0 252 */ 253 function alert($message) 254 { 255 return $this->log($message, PEAR_LOG_ALERT); 256 } 257 258 /** 259 * A convenience function for logging a critical event. It will log a 260 * message at the PEAR_LOG_CRIT log level. 261 * 262 * @param mixed $message String or object containing the message 263 * to log. 264 * 265 * @return boolean True if the message was successfully logged. 266 * 267 * @access public 268 * @since Log 1.7.0 269 */ 270 function crit($message) 271 { 272 return $this->log($message, PEAR_LOG_CRIT); 273 } 274 275 /** 276 * A convenience function for logging a error event. It will log a 277 * message at the PEAR_LOG_ERR log level. 278 * 279 * @param mixed $message String or object containing the message 280 * to log. 281 * 282 * @return boolean True if the message was successfully logged. 283 * 284 * @access public 285 * @since Log 1.7.0 286 */ 287 function err($message) 288 { 289 return $this->log($message, PEAR_LOG_ERR); 290 } 291 292 /** 293 * A convenience function for logging a warning event. It will log a 294 * message at the PEAR_LOG_WARNING log level. 295 * 296 * @param mixed $message String or object containing the message 297 * to log. 298 * 299 * @return boolean True if the message was successfully logged. 300 * 301 * @access public 302 * @since Log 1.7.0 303 */ 304 function warning($message) 305 { 306 return $this->log($message, PEAR_LOG_WARNING); 307 } 308 309 /** 310 * A convenience function for logging a notice event. It will log a 311 * message at the PEAR_LOG_NOTICE log level. 312 * 313 * @param mixed $message String or object containing the message 314 * to log. 315 * 316 * @return boolean True if the message was successfully logged. 317 * 318 * @access public 319 * @since Log 1.7.0 320 */ 321 function notice($message) 322 { 323 return $this->log($message, PEAR_LOG_NOTICE); 324 } 325 326 /** 327 * A convenience function for logging a information event. It will log a 328 * message at the PEAR_LOG_INFO log level. 329 * 330 * @param mixed $message String or object containing the message 331 * to log. 332 * 333 * @return boolean True if the message was successfully logged. 334 * 335 * @access public 336 * @since Log 1.7.0 337 */ 338 function info($message) 339 { 340 return $this->log($message, PEAR_LOG_INFO); 341 } 342 343 /** 344 * A convenience function for logging a debug event. It will log a 345 * message at the PEAR_LOG_DEBUG log level. 346 * 347 * @param mixed $message String or object containing the message 348 * to log. 349 * 350 * @return boolean True if the message was successfully logged. 351 * 352 * @access public 353 * @since Log 1.7.0 354 */ 355 function debug($message) 356 { 357 return $this->log($message, PEAR_LOG_DEBUG); 358 } 359 360 /** 361 * Returns the string representation of the message data. 362 * 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. 367 * 368 * If the message data is already a string, it will be returned unchanged. 369 * 370 * @param mixed $message The original message data. This may be a 371 * string or any object. 372 * 373 * @return string The string representation of the message. 374 * 375 * @access private 376 */ 377 function _extractMessage($message) 378 { 379 /* 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. 383 * 384 * We also use the human-readable format for arrays. 385 */ 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; 394 } else { 395 $message = $message->__toString(); 396 } 397 } else { 398 $message = print_r($message, true); 399 } 400 } else if (is_array($message)) { 401 if (isset($message['message'])) { 402 $message = $message['message']; 403 } else { 404 $message = print_r($message, true); 405 } 406 } 407 408 /* Otherwise, we assume the message is a string. */ 409 return $message; 410 } 411 412 /** 413 * Returns the string representation of a PEAR_LOG_* integer constant. 414 * 415 * @param int $priority A PEAR_LOG_* integer constant. 416 * 417 * @return string The string representation of $level. 418 * 419 * @since Log 1.0 420 */ 421 function priorityToString($priority) 422 { 423 $levels = array( 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' 432 ); 433 434 return $levels[$priority]; 435 } 436 437 /** 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. 441 * 442 * @param string $name String containing a priority name. 443 * 444 * @return string The PEAR_LOG_* integer contstant corresponding 445 * the the specified priority name. 446 * 447 * @since Log 1.9.0 448 */ 449 function stringToPriority($name) 450 { 451 $levels = array( 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 460 ); 461 462 return $levels[strtolower($name)]; 463 } 464 465 /** 466 * Calculate the log mask for the given priority. 467 * 468 * @param integer $priority The priority whose mask will be calculated. 469 * 470 * @return integer The calculated log mask. 471 * 472 * @access public 473 * @since Log 1.7.0 474 */ 475 function MASK($priority) 476 { 477 return (1 << $priority); 478 } 479 480 /** 481 * Calculate the log mask for all priorities up to the given priority. 482 * 483 * @param integer $priority The maximum priority covered by this mask. 484 * 485 * @return integer The calculated log mask. 486 * 487 * @access public 488 * @since Log 1.7.0 489 */ 490 function UPTO($priority) 491 { 492 return ((1 << ($priority + 1)) - 1); 493 } 494 495 /** 496 * Set and return the level mask for the current Log instance. 497 * 498 * @param integer $mask A bitwise mask of log levels. 499 * 500 * @return integer The current level mask. 501 * 502 * @access public 503 * @since Log 1.7.0 504 */ 505 function setMask($mask) 506 { 507 $this->_mask = $mask; 508 509 return $this->_mask; 510 } 511 512 /** 513 * Returns the current level mask. 514 * 515 * @return interger The current level mask. 516 * 517 * @access public 518 * @since Log 1.7.0 519 */ 520 function getMask() 521 { 522 return $this->_mask; 523 } 524 525 /** 526 * Check if the given priority is included in the current level mask. 527 * 528 * @param integer $priority The priority to check. 529 * 530 * @return boolean True if the given priority is included in the current 531 * log mask. 532 * 533 * @access private 534 * @since Log 1.7.0 535 */ 536 function _isMasked($priority) 537 { 538 return (Log::MASK($priority) & $this->_mask); 539 } 540 541 /** 542 * Returns the current default priority. 543 * 544 * @return integer The current default priority. 545 * 546 * @access public 547 * @since Log 1.8.4 548 */ 549 function getPriority() 550 { 551 return $this->_priority; 552 } 553 554 /** 555 * Sets the default priority to the specified value. 556 * 557 * @param integer $priority The new default priority. 558 * 559 * @access public 560 * @since Log 1.8.4 561 */ 562 function setPriority($priority) 563 { 564 $this->_priority = $priority; 565 } 566 567 /** 568 * Adds a Log_observer instance to the list of observers that are listening 569 * for messages emitted by this Log instance. 570 * 571 * @param object $observer The Log_observer instance to attach as a 572 * listener. 573 * 574 * @param boolean True if the observer is successfully attached. 575 * 576 * @access public 577 * @since Log 1.0 578 */ 579 function attach(&$observer) 580 { 581 if (!is_a($observer, 'Log_observer')) { 582 return false; 583 } 584 585 $this->_listeners[$observer->_id] = &$observer; 586 587 return true; 588 } 589 590 /** 591 * Removes a Log_observer instance from the list of observers. 592 * 593 * @param object $observer The Log_observer instance to detach from 594 * the list of listeners. 595 * 596 * @param boolean True if the observer is successfully detached. 597 * 598 * @access public 599 * @since Log 1.0 600 */ 601 function detach($observer) 602 { 603 if (!is_a($observer, 'Log_observer') || 604 !isset($this->_listeners[$observer->_id])) { 605 return false; 606 } 607 608 unset($this->_listeners[$observer->_id]); 609 610 return true; 611 } 612 613 /** 614 * Informs each registered observer instance that a new message has been 615 * logged. 616 * 617 * @param array $event A hash describing the log event. 618 * 619 * @access private 620 */ 621 function _announce($event) 622 { 623 foreach ($this->_listeners as $id => $listener) { 624 if ($event['priority'] <= $this->_listeners[$id]->_priority) { 625 $this->_listeners[$id]->notify($event); 626 } 627 } 628 } 629 630 /** 631 * Indicates whether this is a composite class. 632 * 633 * @return boolean True if this is a composite class. 634 * 635 * @access public 636 * @since Log 1.0 637 */ 638 function isComposite() 639 { 640 return false; 641 } 642 643 /** 644 * Sets this Log instance's identification string. 645 * 646 * @param string $ident The new identification string. 647 * 648 * @access public 649 * @since Log 1.6.3 650 */ 651 function setIdent($ident) 652 { 653 $this->_ident = $ident; 654 } 655 656 /** 657 * Returns the current identification string. 658 * 659 * @return string The current Log instance's identification string. 660 * 661 * @access public 662 * @since Log 1.6.3 663 */ 664 function getIdent() 665 { 666 return $this->_ident; 667 } 668 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Wed Nov 21 13:08:55 2007 | par Balluche grâce à PHPXref 0.7 |
![]() |