Automatically exported from code.google.com/p/planningalerts
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Log.php 19 KiB

il y a 18 ans
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. <?php
  2. /**
  3. * $Header: /home/ppcvs/paypal_php_sdk/Log.php,v 1.1 2006/02/19 08:22:40 dennis Exp $
  4. * $Horde: horde/lib/Log.php,v 1.15 2000/06/29 23:39:45 jon Exp $
  5. *
  6. * @version $Revision: 1.1 $
  7. * @package Log
  8. */
  9. define('PEAR_LOG_EMERG', 0); /** System is unusable */
  10. define('PEAR_LOG_ALERT', 1); /** Immediate action required */
  11. define('PEAR_LOG_CRIT', 2); /** Critical conditions */
  12. define('PEAR_LOG_ERR', 3); /** Error conditions */
  13. define('PEAR_LOG_WARNING', 4); /** Warning conditions */
  14. define('PEAR_LOG_NOTICE', 5); /** Normal but significant */
  15. define('PEAR_LOG_INFO', 6); /** Informational */
  16. define('PEAR_LOG_DEBUG', 7); /** Debug-level messages */
  17. define('PEAR_LOG_ALL', bindec('11111111')); /** All messages */
  18. define('PEAR_LOG_NONE', bindec('00000000')); /** No message */
  19. /* Log types for PHP's native error_log() function. */
  20. define('PEAR_LOG_TYPE_SYSTEM', 0); /** Use PHP's system logger */
  21. define('PEAR_LOG_TYPE_MAIL', 1); /** Use PHP's mail() function */
  22. define('PEAR_LOG_TYPE_DEBUG', 2); /** Use PHP's debugging connection */
  23. define('PEAR_LOG_TYPE_FILE', 3); /** Append to a file */
  24. /**
  25. * The Log:: class implements both an abstraction for various logging
  26. * mechanisms and the Subject end of a Subject-Observer pattern.
  27. *
  28. * @author Chuck Hagenbuch <chuck@horde.org>
  29. * @author Jon Parise <jon@php.net>
  30. * @since Horde 1.3
  31. * @package Log
  32. */
  33. class Log
  34. {
  35. /**
  36. * Indicates whether or not the log can been opened / connected.
  37. *
  38. * @var boolean
  39. * @access private
  40. */
  41. var $_opened = false;
  42. /**
  43. * Instance-specific unique identification number.
  44. *
  45. * @var integer
  46. * @access private
  47. */
  48. var $_id = 0;
  49. /**
  50. * The label that uniquely identifies this set of log messages.
  51. *
  52. * @var string
  53. * @access private
  54. */
  55. var $_ident = '';
  56. /**
  57. * The default priority to use when logging an event.
  58. *
  59. * @var integer
  60. * @access private
  61. */
  62. var $_priority = PEAR_LOG_INFO;
  63. /**
  64. * The bitmask of allowed log levels.
  65. * @var integer
  66. * @access private
  67. */
  68. var $_mask = PEAR_LOG_ALL;
  69. /**
  70. * Holds all Log_observer objects that wish to be notified of new messages.
  71. *
  72. * @var array
  73. * @access private
  74. */
  75. var $_listeners = array();
  76. /**
  77. * Attempts to return a concrete Log instance of type $handler.
  78. *
  79. * @param string $handler The type of concrete Log subclass to return.
  80. * Attempt to dynamically include the code for
  81. * this subclass. Currently, valid values are
  82. * 'console', 'syslog', 'sql', 'file', and 'mcal'.
  83. *
  84. * @param string $name The name of the actually log file, table, or
  85. * other specific store to use. Defaults to an
  86. * empty string, with which the subclass will
  87. * attempt to do something intelligent.
  88. *
  89. * @param string $ident The identity reported to the log system.
  90. *
  91. * @param array $conf A hash containing any additional configuration
  92. * information that a subclass might need.
  93. *
  94. * @param int $level Log messages up to and including this level.
  95. *
  96. * @return object Log The newly created concrete Log instance, or
  97. * null on an error.
  98. * @access public
  99. * @since Log 1.0
  100. */
  101. function &factory($handler, $name = '', $ident = '', $conf = array(),
  102. $level = PEAR_LOG_DEBUG)
  103. {
  104. $handler = strtolower($handler);
  105. $class = 'Log_' . $handler;
  106. $classfile = 'Log/' . $handler . '.php';
  107. /*
  108. * Attempt to include our version of the named class, but don't treat
  109. * a failure as fatal. The caller may have already included their own
  110. * version of the named class.
  111. */
  112. if (!class_exists($class)) {
  113. @include_once $classfile;
  114. }
  115. /* If the class exists, return a new instance of it. */
  116. if (class_exists($class)) {
  117. $obj = new $class($name, $ident, $conf, $level);
  118. return $obj;
  119. }
  120. return null;
  121. }
  122. /**
  123. * Attempts to return a reference to a concrete Log instance of type
  124. * $handler, only creating a new instance if no log instance with the same
  125. * parameters currently exists.
  126. *
  127. * You should use this if there are multiple places you might create a
  128. * logger, you don't want to create multiple loggers, and you don't want to
  129. * check for the existance of one each time. The singleton pattern does all
  130. * the checking work for you.
  131. *
  132. * <b>You MUST call this method with the $var = &Log::singleton() syntax.
  133. * Without the ampersand (&) in front of the method name, you will not get
  134. * a reference, you will get a copy.</b>
  135. *
  136. * @param string $handler The type of concrete Log subclass to return.
  137. * Attempt to dynamically include the code for
  138. * this subclass. Currently, valid values are
  139. * 'console', 'syslog', 'sql', 'file', and 'mcal'.
  140. *
  141. * @param string $name The name of the actually log file, table, or
  142. * other specific store to use. Defaults to an
  143. * empty string, with which the subclass will
  144. * attempt to do something intelligent.
  145. *
  146. * @param string $ident The identity reported to the log system.
  147. *
  148. * @param array $conf A hash containing any additional configuration
  149. * information that a subclass might need.
  150. *
  151. * @param int $level Log messages up to and including this level.
  152. *
  153. * @return object Log The newly created concrete Log instance, or
  154. * null on an error.
  155. * @access public
  156. * @since Log 1.0
  157. */
  158. function &singleton($handler, $name = '', $ident = '', $conf = array(),
  159. $level = PEAR_LOG_DEBUG)
  160. {
  161. static $instances;
  162. if (!isset($instances)) $instances = array();
  163. $signature = serialize(array($handler, $name, $ident, $conf, $level));
  164. if (!isset($instances[$signature])) {
  165. $instances[$signature] = &Log::factory($handler, $name, $ident,
  166. $conf, $level);
  167. }
  168. return $instances[$signature];
  169. }
  170. /**
  171. * Abstract implementation of the open() method.
  172. * @since Log 1.0
  173. */
  174. function open()
  175. {
  176. return false;
  177. }
  178. /**
  179. * Abstract implementation of the close() method.
  180. * @since Log 1.0
  181. */
  182. function close()
  183. {
  184. return false;
  185. }
  186. /**
  187. * Abstract implementation of the flush() method.
  188. * @since Log 1.8.2
  189. */
  190. function flush()
  191. {
  192. return false;
  193. }
  194. /**
  195. * Abstract implementation of the log() method.
  196. * @since Log 1.0
  197. */
  198. function log($message, $priority = null)
  199. {
  200. return false;
  201. }
  202. /**
  203. * A convenience function for logging a emergency event. It will log a
  204. * message at the PEAR_LOG_EMERG log level.
  205. *
  206. * @param mixed $message String or object containing the message
  207. * to log.
  208. *
  209. * @return boolean True if the message was successfully logged.
  210. *
  211. * @access public
  212. * @since Log 1.7.0
  213. */
  214. function emerg($message)
  215. {
  216. return $this->log($message, PEAR_LOG_EMERG);
  217. }
  218. /**
  219. * A convenience function for logging an alert event. It will log a
  220. * message at the PEAR_LOG_ALERT log level.
  221. *
  222. * @param mixed $message String or object containing the message
  223. * to log.
  224. *
  225. * @return boolean True if the message was successfully logged.
  226. *
  227. * @access public
  228. * @since Log 1.7.0
  229. */
  230. function alert($message)
  231. {
  232. return $this->log($message, PEAR_LOG_ALERT);
  233. }
  234. /**
  235. * A convenience function for logging a critical event. It will log a
  236. * message at the PEAR_LOG_CRIT log level.
  237. *
  238. * @param mixed $message String or object containing the message
  239. * to log.
  240. *
  241. * @return boolean True if the message was successfully logged.
  242. *
  243. * @access public
  244. * @since Log 1.7.0
  245. */
  246. function crit($message)
  247. {
  248. return $this->log($message, PEAR_LOG_CRIT);
  249. }
  250. /**
  251. * A convenience function for logging a error event. It will log a
  252. * message at the PEAR_LOG_ERR log level.
  253. *
  254. * @param mixed $message String or object containing the message
  255. * to log.
  256. *
  257. * @return boolean True if the message was successfully logged.
  258. *
  259. * @access public
  260. * @since Log 1.7.0
  261. */
  262. function err($message)
  263. {
  264. return $this->log($message, PEAR_LOG_ERR);
  265. }
  266. /**
  267. * A convenience function for logging a warning event. It will log a
  268. * message at the PEAR_LOG_WARNING log level.
  269. *
  270. * @param mixed $message String or object containing the message
  271. * to log.
  272. *
  273. * @return boolean True if the message was successfully logged.
  274. *
  275. * @access public
  276. * @since Log 1.7.0
  277. */
  278. function warning($message)
  279. {
  280. return $this->log($message, PEAR_LOG_WARNING);
  281. }
  282. /**
  283. * A convenience function for logging a notice event. It will log a
  284. * message at the PEAR_LOG_NOTICE log level.
  285. *
  286. * @param mixed $message String or object containing the message
  287. * to log.
  288. *
  289. * @return boolean True if the message was successfully logged.
  290. *
  291. * @access public
  292. * @since Log 1.7.0
  293. */
  294. function notice($message)
  295. {
  296. return $this->log($message, PEAR_LOG_NOTICE);
  297. }
  298. /**
  299. * A convenience function for logging a information event. It will log a
  300. * message at the PEAR_LOG_INFO log level.
  301. *
  302. * @param mixed $message String or object containing the message
  303. * to log.
  304. *
  305. * @return boolean True if the message was successfully logged.
  306. *
  307. * @access public
  308. * @since Log 1.7.0
  309. */
  310. function info($message)
  311. {
  312. return $this->log($message, PEAR_LOG_INFO);
  313. }
  314. /**
  315. * A convenience function for logging a debug event. It will log a
  316. * message at the PEAR_LOG_DEBUG log level.
  317. *
  318. * @param mixed $message String or object containing the message
  319. * to log.
  320. *
  321. * @return boolean True if the message was successfully logged.
  322. *
  323. * @access public
  324. * @since Log 1.7.0
  325. */
  326. function debug($message)
  327. {
  328. return $this->log($message, PEAR_LOG_DEBUG);
  329. }
  330. /**
  331. * Returns the string representation of the message data.
  332. *
  333. * If $message is an object, _extractMessage() will attempt to extract
  334. * the message text using a known method (such as a PEAR_Error object's
  335. * getMessage() method). If a known method, cannot be found, the
  336. * serialized representation of the object will be returned.
  337. *
  338. * If the message data is already a string, it will be returned unchanged.
  339. *
  340. * @param mixed $message The original message data. This may be a
  341. * string or any object.
  342. *
  343. * @return string The string representation of the message.
  344. *
  345. * @access private
  346. */
  347. function _extractMessage($message)
  348. {
  349. /*
  350. * If we've been given an object, attempt to extract the message using
  351. * a known method. If we can't find such a method, default to the
  352. * "human-readable" version of the object.
  353. *
  354. * We also use the human-readable format for arrays.
  355. */
  356. if (is_object($message)) {
  357. if (method_exists($message, 'getmessage')) {
  358. $message = $message->getMessage();
  359. } else if (method_exists($message, 'tostring')) {
  360. $message = $message->toString();
  361. } else if (method_exists($message, '__tostring')) {
  362. if (version_compare(PHP_VERSION, '5.0.0', 'ge')) {
  363. $message = (string)$message;
  364. } else {
  365. $message = $message->__toString();
  366. }
  367. } else {
  368. $message = print_r($message, true);
  369. }
  370. } else if (is_array($message)) {
  371. if (isset($message['message'])) {
  372. $message = $message['message'];
  373. } else {
  374. $message = print_r($message, true);
  375. }
  376. }
  377. /* Otherwise, we assume the message is a string. */
  378. return $message;
  379. }
  380. /**
  381. * Returns the string representation of a PEAR_LOG_* integer constant.
  382. *
  383. * @param int $priority A PEAR_LOG_* integer constant.
  384. *
  385. * @return string The string representation of $level.
  386. *
  387. * @since Log 1.0
  388. */
  389. function priorityToString($priority)
  390. {
  391. $levels = array(
  392. PEAR_LOG_EMERG => 'emergency',
  393. PEAR_LOG_ALERT => 'alert',
  394. PEAR_LOG_CRIT => 'critical',
  395. PEAR_LOG_ERR => 'error',
  396. PEAR_LOG_WARNING => 'warning',
  397. PEAR_LOG_NOTICE => 'notice',
  398. PEAR_LOG_INFO => 'info',
  399. PEAR_LOG_DEBUG => 'debug'
  400. );
  401. return $levels[$priority];
  402. }
  403. /**
  404. * Returns the the PEAR_LOG_* integer constant for the given string
  405. * representation of a priority name. This function performs a
  406. * case-insensitive search.
  407. *
  408. * @param string $name String containing a priority name.
  409. *
  410. * @return string The PEAR_LOG_* integer contstant corresponding
  411. * the the specified priority name.
  412. *
  413. * @since Log 1.9.0
  414. */
  415. function stringToPriority($name)
  416. {
  417. $levels = array(
  418. 'emergency' => PEAR_LOG_EMERG,
  419. 'alert' => PEAR_LOG_ALERT,
  420. 'critical' => PEAR_LOG_CRIT,
  421. 'error' => PEAR_LOG_ERR,
  422. 'warning' => PEAR_LOG_WARNING,
  423. 'notice' => PEAR_LOG_NOTICE,
  424. 'info' => PEAR_LOG_INFO,
  425. 'debug' => PEAR_LOG_DEBUG
  426. );
  427. return $levels[strtolower($name)];
  428. }
  429. /**
  430. * Calculate the log mask for the given priority.
  431. *
  432. * @param integer $priority The priority whose mask will be calculated.
  433. *
  434. * @return integer The calculated log mask.
  435. *
  436. * @access public
  437. * @since Log 1.7.0
  438. */
  439. function MASK($priority)
  440. {
  441. return (1 << $priority);
  442. }
  443. /**
  444. * Calculate the log mask for all priorities up to the given priority.
  445. *
  446. * @param integer $priority The maximum priority covered by this mask.
  447. *
  448. * @return integer The calculated log mask.
  449. *
  450. * @access public
  451. * @since Log 1.7.0
  452. */
  453. function UPTO($priority)
  454. {
  455. return ((1 << ($priority + 1)) - 1);
  456. }
  457. /**
  458. * Set and return the level mask for the current Log instance.
  459. *
  460. * @param integer $mask A bitwise mask of log levels.
  461. *
  462. * @return integer The current level mask.
  463. *
  464. * @access public
  465. * @since Log 1.7.0
  466. */
  467. function setMask($mask)
  468. {
  469. $this->_mask = $mask;
  470. return $this->_mask;
  471. }
  472. /**
  473. * Returns the current level mask.
  474. *
  475. * @return interger The current level mask.
  476. *
  477. * @access public
  478. * @since Log 1.7.0
  479. */
  480. function getMask()
  481. {
  482. return $this->_mask;
  483. }
  484. /**
  485. * Check if the given priority is included in the current level mask.
  486. *
  487. * @param integer $priority The priority to check.
  488. *
  489. * @return boolean True if the given priority is included in the current
  490. * log mask.
  491. *
  492. * @access private
  493. * @since Log 1.7.0
  494. */
  495. function _isMasked($priority)
  496. {
  497. return (Log::MASK($priority) & $this->_mask);
  498. }
  499. /**
  500. * Returns the current default priority.
  501. *
  502. * @return integer The current default priority.
  503. *
  504. * @access public
  505. * @since Log 1.8.4
  506. */
  507. function getPriority()
  508. {
  509. return $this->_priority;
  510. }
  511. /**
  512. * Sets the default priority to the specified value.
  513. *
  514. * @param integer $priority The new default priority.
  515. *
  516. * @access public
  517. * @since Log 1.8.4
  518. */
  519. function setPriority($priority)
  520. {
  521. $this->_priority = $priority;
  522. }
  523. /**
  524. * Adds a Log_observer instance to the list of observers that are listening
  525. * for messages emitted by this Log instance.
  526. *
  527. * @param object $observer The Log_observer instance to attach as a
  528. * listener.
  529. *
  530. * @param boolean True if the observer is successfully attached.
  531. *
  532. * @access public
  533. * @since Log 1.0
  534. */
  535. function attach(&$observer)
  536. {
  537. if (!is_a($observer, 'Log_observer')) {
  538. return false;
  539. }
  540. $this->_listeners[$observer->_id] = &$observer;
  541. return true;
  542. }
  543. /**
  544. * Removes a Log_observer instance from the list of observers.
  545. *
  546. * @param object $observer The Log_observer instance to detach from
  547. * the list of listeners.
  548. *
  549. * @param boolean True if the observer is successfully detached.
  550. *
  551. * @access public
  552. * @since Log 1.0
  553. */
  554. function detach($observer)
  555. {
  556. if (!is_a($observer, 'Log_observer') ||
  557. !isset($this->_listeners[$observer->_id])) {
  558. return false;
  559. }
  560. unset($this->_listeners[$observer->_id]);
  561. return true;
  562. }
  563. /**
  564. * Informs each registered observer instance that a new message has been
  565. * logged.
  566. *
  567. * @param array $event A hash describing the log event.
  568. *
  569. * @access private
  570. */
  571. function _announce($event)
  572. {
  573. foreach ($this->_listeners as $id => $listener) {
  574. if ($event['priority'] <= $this->_listeners[$id]->_priority) {
  575. $this->_listeners[$id]->notify($event);
  576. }
  577. }
  578. }
  579. /**
  580. * Indicates whether this is a composite class.
  581. *
  582. * @return boolean True if this is a composite class.
  583. *
  584. * @access public
  585. * @since Log 1.0
  586. */
  587. function isComposite()
  588. {
  589. return false;
  590. }
  591. /**
  592. * Sets this Log instance's identification string.
  593. *
  594. * @param string $ident The new identification string.
  595. *
  596. * @access public
  597. * @since Log 1.6.3
  598. */
  599. function setIdent($ident)
  600. {
  601. $this->_ident = $ident;
  602. }
  603. /**
  604. * Returns the current identification string.
  605. *
  606. * @return string The current Log instance's identification string.
  607. *
  608. * @access public
  609. * @since Log 1.6.3
  610. */
  611. function getIdent()
  612. {
  613. return $this->_ident;
  614. }
  615. }