PageRenderTime 263ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/pacore/ext/Log.php

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