PageRenderTime 40ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/php/Log.php

https://bitbucket.org/adarshj/convenient_website
PHP | 635 lines | 198 code | 52 blank | 385 comment | 17 complexity | fc7a1d17c5e1f79819f0872b6b353f6d MD5 | raw file
Possible License(s): Apache-2.0, MPL-2.0-no-copyleft-exception, LGPL-2.1, BSD-2-Clause, GPL-2.0, LGPL-3.0
  1. <?php
  2. /**
  3. * $Header: /repository/pear/Log/Log.php,v 1.46 2004/09/09 02:42:22 jon Exp $
  4. * $Horde: horde/lib/Log.php,v 1.15 2000/06/29 23:39:45 jon Exp $
  5. *
  6. * @version $Revision: 1.46 $
  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 an
  97. * false 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. @include_once $classfile;
  113. /* If the class exists, return a new instance of it. */
  114. if (class_exists($class)) {
  115. return new $class($name, $ident, $conf, $level);
  116. }
  117. return false;
  118. }
  119. /**
  120. * Attempts to return a reference to a concrete Log instance of type
  121. * $handler, only creating a new instance if no log instance with the same
  122. * parameters currently exists.
  123. *
  124. * You should use this if there are multiple places you might create a
  125. * logger, you don't want to create multiple loggers, and you don't want to
  126. * check for the existance of one each time. The singleton pattern does all
  127. * the checking work for you.
  128. *
  129. * <b>You MUST call this method with the $var = &Log::singleton() syntax.
  130. * Without the ampersand (&) in front of the method name, you will not get
  131. * a reference, you will get a copy.</b>
  132. *
  133. * @param string $handler The type of concrete Log subclass to return.
  134. * Attempt to dynamically include the code for
  135. * this subclass. Currently, valid values are
  136. * 'console', 'syslog', 'sql', 'file', and 'mcal'.
  137. *
  138. * @param string $name The name of the actually log file, table, or
  139. * other specific store to use. Defaults to an
  140. * empty string, with which the subclass will
  141. * attempt to do something intelligent.
  142. *
  143. * @param string $ident The identity reported to the log system.
  144. *
  145. * @param array $conf A hash containing any additional configuration
  146. * information that a subclass might need.
  147. *
  148. * @param int $level Log messages up to and including this level.
  149. *
  150. * @return object Log The newly created concrete Log instance, or an
  151. * false on an error.
  152. * @access public
  153. * @since Log 1.0
  154. */
  155. function &singleton($handler, $name = '', $ident = '', $conf = array(),
  156. $level = PEAR_LOG_DEBUG)
  157. {
  158. static $instances;
  159. if (!isset($instances)) $instances = array();
  160. $signature = serialize(array($handler, $name, $ident, $conf, $level));
  161. if (!isset($instances[$signature])) {
  162. $instances[$signature] = &Log::factory($handler, $name, $ident,
  163. $conf, $level);
  164. }
  165. return $instances[$signature];
  166. }
  167. /**
  168. * Abstract implementation of the open() method.
  169. * @since Log 1.0
  170. */
  171. function open()
  172. {
  173. return false;
  174. }
  175. /**
  176. * Abstract implementation of the close() method.
  177. * @since Log 1.0
  178. */
  179. function close()
  180. {
  181. return false;
  182. }
  183. /**
  184. * Abstract implementation of the flush() method.
  185. * @since Log 1.8.2
  186. */
  187. function flush()
  188. {
  189. return false;
  190. }
  191. /**
  192. * Abstract implementation of the log() method.
  193. * @since Log 1.0
  194. */
  195. function log($message, $priority = null)
  196. {
  197. return false;
  198. }
  199. /**
  200. * A convenience function for logging a emergency event. It will log a
  201. * message at the PEAR_LOG_EMERG log level.
  202. *
  203. * @param mixed $message String or object containing the message
  204. * to log.
  205. *
  206. * @return boolean True if the message was successfully logged.
  207. *
  208. * @access public
  209. * @since Log 1.7.0
  210. */
  211. function emerg($message)
  212. {
  213. return $this->log($message, PEAR_LOG_EMERG);
  214. }
  215. /**
  216. * A convenience function for logging an alert event. It will log a
  217. * message at the PEAR_LOG_ALERT log level.
  218. *
  219. * @param mixed $message String or object containing the message
  220. * to log.
  221. *
  222. * @return boolean True if the message was successfully logged.
  223. *
  224. * @access public
  225. * @since Log 1.7.0
  226. */
  227. function alert($message)
  228. {
  229. return $this->log($message, PEAR_LOG_ALERT);
  230. }
  231. /**
  232. * A convenience function for logging a critical event. It will log a
  233. * message at the PEAR_LOG_CRIT log level.
  234. *
  235. * @param mixed $message String or object containing the message
  236. * to log.
  237. *
  238. * @return boolean True if the message was successfully logged.
  239. *
  240. * @access public
  241. * @since Log 1.7.0
  242. */
  243. function crit($message)
  244. {
  245. return $this->log($message, PEAR_LOG_CRIT);
  246. }
  247. /**
  248. * A convenience function for logging a error event. It will log a
  249. * message at the PEAR_LOG_ERR log level.
  250. *
  251. * @param mixed $message String or object containing the message
  252. * to log.
  253. *
  254. * @return boolean True if the message was successfully logged.
  255. *
  256. * @access public
  257. * @since Log 1.7.0
  258. */
  259. function err($message)
  260. {
  261. return $this->log($message, PEAR_LOG_ERR);
  262. }
  263. /**
  264. * A convenience function for logging a warning event. It will log a
  265. * message at the PEAR_LOG_WARNING log level.
  266. *
  267. * @param mixed $message String or object containing the message
  268. * to log.
  269. *
  270. * @return boolean True if the message was successfully logged.
  271. *
  272. * @access public
  273. * @since Log 1.7.0
  274. */
  275. function warning($message)
  276. {
  277. return $this->log($message, PEAR_LOG_WARNING);
  278. }
  279. /**
  280. * A convenience function for logging a notice event. It will log a
  281. * message at the PEAR_LOG_NOTICE log level.
  282. *
  283. * @param mixed $message String or object containing the message
  284. * to log.
  285. *
  286. * @return boolean True if the message was successfully logged.
  287. *
  288. * @access public
  289. * @since Log 1.7.0
  290. */
  291. function notice($message)
  292. {
  293. return $this->log($message, PEAR_LOG_NOTICE);
  294. }
  295. /**
  296. * A convenience function for logging a information event. It will log a
  297. * message at the PEAR_LOG_INFO log level.
  298. *
  299. * @param mixed $message String or object containing the message
  300. * to log.
  301. *
  302. * @return boolean True if the message was successfully logged.
  303. *
  304. * @access public
  305. * @since Log 1.7.0
  306. */
  307. function info($message)
  308. {
  309. return $this->log($message, PEAR_LOG_INFO);
  310. }
  311. /**
  312. * A convenience function for logging a debug event. It will log a
  313. * message at the PEAR_LOG_DEBUG log level.
  314. *
  315. * @param mixed $message String or object containing the message
  316. * to log.
  317. *
  318. * @return boolean True if the message was successfully logged.
  319. *
  320. * @access public
  321. * @since Log 1.7.0
  322. */
  323. function debug($message)
  324. {
  325. return $this->log($message, PEAR_LOG_DEBUG);
  326. }
  327. /**
  328. * Returns the string representation of the message data.
  329. *
  330. * If $message is an object, _extractMessage() will attempt to extract
  331. * the message text using a known method (such as a PEAR_Error object's
  332. * getMessage() method). If a known method, cannot be found, the
  333. * serialized representation of the object will be returned.
  334. *
  335. * If the message data is already a string, it will be returned unchanged.
  336. *
  337. * @param mixed $message The original message data. This may be a
  338. * string or any object.
  339. *
  340. * @return string The string representation of the message.
  341. *
  342. * @access private
  343. */
  344. function _extractMessage($message)
  345. {
  346. /*
  347. * If we've been given an object, attempt to extract the message using
  348. * a known method. If we can't find such a method, default to the
  349. * "human-readable" version of the object.
  350. *
  351. * We also use the human-readable format for arrays.
  352. */
  353. if (is_object($message)) {
  354. if (method_exists($message, 'getmessage')) {
  355. $message = $message->getMessage();
  356. } else if (method_exists($message, 'tostring')) {
  357. $message = $message->toString();
  358. } else if (method_exists($message, '__tostring')) {
  359. $message = (string)$message;
  360. } else {
  361. $message = print_r($message, true);
  362. }
  363. } else if (is_array($message)) {
  364. if (isset($message['message'])) {
  365. $message = $message['message'];
  366. } else {
  367. $message = print_r($message, true);
  368. }
  369. }
  370. /* Otherwise, we assume the message is a string. */
  371. return $message;
  372. }
  373. /**
  374. * Returns the string representation of a PEAR_LOG_* integer constant.
  375. *
  376. * @param int $priority A PEAR_LOG_* integer constant.
  377. *
  378. * @return string The string representation of $level.
  379. *
  380. * @since Log 1.0
  381. */
  382. function priorityToString($priority)
  383. {
  384. $levels = array(
  385. PEAR_LOG_EMERG => 'emergency',
  386. PEAR_LOG_ALERT => 'alert',
  387. PEAR_LOG_CRIT => 'critical',
  388. PEAR_LOG_ERR => 'error',
  389. PEAR_LOG_WARNING => 'warning',
  390. PEAR_LOG_NOTICE => 'notice',
  391. PEAR_LOG_INFO => 'info',
  392. PEAR_LOG_DEBUG => 'debug'
  393. );
  394. return $levels[$priority];
  395. }
  396. /**
  397. * Calculate the log mask for the given priority.
  398. *
  399. * @param integer $priority The priority whose mask will be calculated.
  400. *
  401. * @return integer The calculated log mask.
  402. *
  403. * @access public
  404. * @since Log 1.7.0
  405. */
  406. function MASK($priority)
  407. {
  408. return (1 << $priority);
  409. }
  410. /**
  411. * Calculate the log mask for all priorities up to the given priority.
  412. *
  413. * @param integer $priority The maximum priority covered by this mask.
  414. *
  415. * @return integer The calculated log mask.
  416. *
  417. * @access public
  418. * @since Log 1.7.0
  419. */
  420. function UPTO($priority)
  421. {
  422. return ((1 << ($priority + 1)) - 1);
  423. }
  424. /**
  425. * Set and return the level mask for the current Log instance.
  426. *
  427. * @param integer $mask A bitwise mask of log levels.
  428. *
  429. * @return integer The current level mask.
  430. *
  431. * @access public
  432. * @since Log 1.7.0
  433. */
  434. function setMask($mask)
  435. {
  436. $this->_mask = $mask;
  437. return $this->_mask;
  438. }
  439. /**
  440. * Returns the current level mask.
  441. *
  442. * @return interger The current level mask.
  443. *
  444. * @access public
  445. * @since Log 1.7.0
  446. */
  447. function getMask()
  448. {
  449. return $this->_mask;
  450. }
  451. /**
  452. * Check if the given priority is included in the current level mask.
  453. *
  454. * @param integer $priority The priority to check.
  455. *
  456. * @return boolean True if the given priority is included in the current
  457. * log mask.
  458. *
  459. * @access private
  460. * @since Log 1.7.0
  461. */
  462. function _isMasked($priority)
  463. {
  464. return (Log::MASK($priority) & $this->_mask);
  465. }
  466. /**
  467. * Returns the current default priority.
  468. *
  469. * @return integer The current default priority.
  470. *
  471. * @access public
  472. * @since Log 1.8.4
  473. */
  474. function getPriority()
  475. {
  476. return $this->_priority;
  477. }
  478. /**
  479. * Sets the default priority to the specified value.
  480. *
  481. * @param integer $priority The new default priority.
  482. *
  483. * @access public
  484. * @since Log 1.8.4
  485. */
  486. function setPriority($priority)
  487. {
  488. $this->_priority = $priority;
  489. }
  490. /**
  491. * Adds a Log_observer instance to the list of observers that are listening
  492. * for messages emitted by this Log instance.
  493. *
  494. * @param object $observer The Log_observer instance to attach as a
  495. * listener.
  496. *
  497. * @param boolean True if the observer is successfully attached.
  498. *
  499. * @access public
  500. * @since Log 1.0
  501. */
  502. function attach(&$observer)
  503. {
  504. if (!is_a($observer, 'Log_observer')) {
  505. return false;
  506. }
  507. $this->_listeners[$observer->_id] = &$observer;
  508. return true;
  509. }
  510. /**
  511. * Removes a Log_observer instance from the list of observers.
  512. *
  513. * @param object $observer The Log_observer instance to detach from
  514. * the list of listeners.
  515. *
  516. * @param boolean True if the observer is successfully detached.
  517. *
  518. * @access public
  519. * @since Log 1.0
  520. */
  521. function detach($observer)
  522. {
  523. if (!is_a($observer, 'Log_observer') ||
  524. !isset($this->_listeners[$observer->_id])) {
  525. return false;
  526. }
  527. unset($this->_listeners[$observer->_id]);
  528. return true;
  529. }
  530. /**
  531. * Informs each registered observer instance that a new message has been
  532. * logged.
  533. *
  534. * @param array $event A hash describing the log event.
  535. *
  536. * @access private
  537. */
  538. function _announce($event)
  539. {
  540. foreach ($this->_listeners as $id => $listener) {
  541. if ($event['priority'] <= $this->_listeners[$id]->_priority) {
  542. $this->_listeners[$id]->notify($event);
  543. }
  544. }
  545. }
  546. /**
  547. * Indicates whether this is a composite class.
  548. *
  549. * @return boolean True if this is a composite class.
  550. *
  551. * @access public
  552. * @since Log 1.0
  553. */
  554. function isComposite()
  555. {
  556. return false;
  557. }
  558. /**
  559. * Sets this Log instance's identification string.
  560. *
  561. * @param string $ident The new identification string.
  562. *
  563. * @access public
  564. * @since Log 1.6.3
  565. */
  566. function setIdent($ident)
  567. {
  568. $this->_ident = $ident;
  569. }
  570. /**
  571. * Returns the current identification string.
  572. *
  573. * @return string The current Log instance's identification string.
  574. *
  575. * @access public
  576. * @since Log 1.6.3
  577. */
  578. function getIdent()
  579. {
  580. return $this->_ident;
  581. }
  582. }
  583. ?>