Automatically exported from code.google.com/p/planningalerts
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

syslog.php 4.8 KiB

17 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * $Header: /home/ppcvs/paypal_php_sdk/Log/syslog.php,v 1.1 2006/02/19 08:22:29 dennis Exp $
  4. * $Horde: horde/lib/Log/syslog.php,v 1.6 2000/06/28 21:36:13 jon Exp $
  5. *
  6. * @version $Revision: 1.1 $
  7. * @package Log
  8. */
  9. /**
  10. * The Log_syslog class is a concrete implementation of the Log::
  11. * abstract class which sends messages to syslog on UNIX-like machines
  12. * (PHP emulates this with the Event Log on Windows machines).
  13. *
  14. * @author Chuck Hagenbuch <chuck@horde.org>
  15. * @since Horde 1.3
  16. * @since Log 1.0
  17. * @package Log
  18. *
  19. * @example syslog.php Using the syslog handler.
  20. */
  21. class Log_syslog extends Log
  22. {
  23. /**
  24. * Integer holding the log facility to use.
  25. * @var string
  26. * @access private
  27. */
  28. var $_name = LOG_SYSLOG;
  29. /**
  30. * Constructs a new syslog object.
  31. *
  32. * @param string $name The syslog facility.
  33. * @param string $ident The identity string.
  34. * @param array $conf The configuration array.
  35. * @param int $level Log messages up to and including this level.
  36. * @access public
  37. */
  38. function Log_syslog($name, $ident = '', $conf = array(),
  39. $level = PEAR_LOG_DEBUG)
  40. {
  41. /* Ensure we have a valid integer value for $name. */
  42. if (empty($name) || !is_int($name)) {
  43. $name = LOG_SYSLOG;
  44. }
  45. $this->_id = md5(microtime());
  46. $this->_name = $name;
  47. $this->_ident = $ident;
  48. $this->_mask = Log::UPTO($level);
  49. }
  50. /**
  51. * Opens a connection to the system logger, if it has not already
  52. * been opened. This is implicitly called by log(), if necessary.
  53. * @access public
  54. */
  55. function open()
  56. {
  57. if (!$this->_opened) {
  58. openlog($this->_ident, LOG_PID, $this->_name);
  59. $this->_opened = true;
  60. }
  61. return $this->_opened;
  62. }
  63. /**
  64. * Closes the connection to the system logger, if it is open.
  65. * @access public
  66. */
  67. function close()
  68. {
  69. if ($this->_opened) {
  70. closelog();
  71. $this->_opened = false;
  72. }
  73. return ($this->_opened === false);
  74. }
  75. /**
  76. * Sends $message to the currently open syslog connection. Calls
  77. * open() if necessary. Also passes the message along to any Log_observer
  78. * instances that are observing this Log.
  79. *
  80. * @param mixed $message String or object containing the message to log.
  81. * @param int $priority (optional) The priority of the message. Valid
  82. * values are: PEAR_LOG_EMERG, PEAR_LOG_ALERT,
  83. * PEAR_LOG_CRIT, PEAR_LOG_ERR, PEAR_LOG_WARNING,
  84. * PEAR_LOG_NOTICE, PEAR_LOG_INFO, and PEAR_LOG_DEBUG.
  85. * @return boolean True on success or false on failure.
  86. * @access public
  87. */
  88. function log($message, $priority = null)
  89. {
  90. /* If a priority hasn't been specified, use the default value. */
  91. if ($priority === null) {
  92. $priority = $this->_priority;
  93. }
  94. /* Abort early if the priority is above the maximum logging level. */
  95. if (!$this->_isMasked($priority)) {
  96. return false;
  97. }
  98. /* If the connection isn't open and can't be opened, return failure. */
  99. if (!$this->_opened && !$this->open()) {
  100. return false;
  101. }
  102. /* Extract the string representation of the message. */
  103. $message = $this->_extractMessage($message);
  104. if (!syslog($this->_toSyslog($priority), $message)) {
  105. return false;
  106. }
  107. $this->_announce(array('priority' => $priority, 'message' => $message));
  108. return true;
  109. }
  110. /**
  111. * Converts a PEAR_LOG_* constant into a syslog LOG_* constant.
  112. *
  113. * This function exists because, under Windows, not all of the LOG_*
  114. * constants have unique values. Instead, the PEAR_LOG_* were introduced
  115. * for global use, with the conversion to the LOG_* constants kept local to
  116. * to the syslog driver.
  117. *
  118. * @param int $priority PEAR_LOG_* value to convert to LOG_* value.
  119. *
  120. * @return The LOG_* representation of $priority.
  121. *
  122. * @access private
  123. */
  124. function _toSyslog($priority)
  125. {
  126. static $priorities = array(
  127. PEAR_LOG_EMERG => LOG_EMERG,
  128. PEAR_LOG_ALERT => LOG_ALERT,
  129. PEAR_LOG_CRIT => LOG_CRIT,
  130. PEAR_LOG_ERR => LOG_ERR,
  131. PEAR_LOG_WARNING => LOG_WARNING,
  132. PEAR_LOG_NOTICE => LOG_NOTICE,
  133. PEAR_LOG_INFO => LOG_INFO,
  134. PEAR_LOG_DEBUG => LOG_DEBUG
  135. );
  136. /* If we're passed an unknown priority, default to LOG_INFO. */
  137. if (!is_int($priority) || !in_array($priority, $priorities)) {
  138. return LOG_INFO;
  139. }
  140. return $priorities[$priority];
  141. }
  142. }