4 * $Horde: horde/lib/Log/syslog.php,v 1.6 2000/06/28 21:36:13 jon Exp $
6 * @version $Revision: 308379 $
11 * The Log_syslog class is a concrete implementation of the Log::
12 * abstract class which sends messages to syslog on UNIX-like machines
13 * (PHP emulates this with the Event Log on Windows machines).
15 * @author Chuck Hagenbuch <chuck@horde.org>
16 * @author Jon Parise <jon@php.net>
21 * @example syslog.php Using the syslog handler.
23 class Log_syslog extends Log
26 * Integer holding the log facility to use.
30 var $_name = LOG_SYSLOG;
33 * Should we inherit the current syslog connection for this process, or
34 * should we call openlog() to start a new syslog connection?
38 var $_inherit = false;
41 * Should we re-open the syslog connection for each log event?
48 * Maximum message length that will be sent to syslog(). If the handler
49 * receives a message longer than this length limit, it will be split into
50 * multiple syslog() calls.
54 var $_maxLength = 500;
57 * String containing the format of a message.
61 var $_lineFormat = '%4$s';
64 * String containing the timestamp format. It will be passed directly to
65 * strftime(). Note that the timestamp string will generated using the
70 var $_timeFormat = '%b %d %H:%M:%S';
73 * Constructs a new syslog object.
75 * @param string $name The syslog facility.
76 * @param string $ident The identity string.
77 * @param array $conf The configuration array.
78 * @param int $level Log messages up to and including this level.
81 function Log_syslog($name, $ident = '', $conf = array(),
82 $level = PEAR_LOG_DEBUG)
84 /* Ensure we have a valid integer value for $name. */
85 if (empty($name) || !is_int($name)) {
89 if (isset($conf['inherit'])) {
90 $this->_inherit = $conf['inherit'];
91 $this->_opened = $this->_inherit;
93 if (isset($conf['reopen'])) {
94 $this->_reopen = $conf['reopen'];
96 if (isset($conf['maxLength'])) {
97 $this->_maxLength = $conf['maxLength'];
99 if (!empty($conf['lineFormat'])) {
100 $this->_lineFormat = str_replace(array_keys($this->_formatMap),
101 array_values($this->_formatMap),
102 $conf['lineFormat']);
104 if (!empty($conf['timeFormat'])) {
105 $this->_timeFormat = $conf['timeFormat'];
108 $this->_id = md5(microtime());
109 $this->_name = $name;
110 $this->_ident = $ident;
111 $this->_mask = Log::UPTO($level);
115 * Opens a connection to the system logger, if it has not already
116 * been opened. This is implicitly called by log(), if necessary.
121 if (!$this->_opened || $this->_reopen) {
122 $this->_opened = openlog($this->_ident, LOG_PID, $this->_name);
125 return $this->_opened;
129 * Closes the connection to the system logger, if it is open.
134 if ($this->_opened && !$this->_inherit) {
136 $this->_opened = false;
143 * Sends $message to the currently open syslog connection. Calls
144 * open() if necessary. Also passes the message along to any Log_observer
145 * instances that are observing this Log.
147 * @param mixed $message String or object containing the message to log.
148 * @param int $priority (optional) The priority of the message. Valid
149 * values are: PEAR_LOG_EMERG, PEAR_LOG_ALERT,
150 * PEAR_LOG_CRIT, PEAR_LOG_ERR, PEAR_LOG_WARNING,
151 * PEAR_LOG_NOTICE, PEAR_LOG_INFO, and PEAR_LOG_DEBUG.
152 * @return boolean True on success or false on failure.
155 function log($message, $priority = null)
157 /* If a priority hasn't been specified, use the default value. */
158 if ($priority === null) {
159 $priority = $this->_priority;
162 /* Abort early if the priority is above the maximum logging level. */
163 if (!$this->_isMasked($priority)) {
167 /* If we need to (re)open the connection and open() fails, abort. */
168 if ((!$this->_opened || $this->_reopen) && !$this->open()) {
172 /* Extract the string representation of the message. */
173 $message = $this->_extractMessage($message);
175 /* Build a syslog priority value based on our current configuration. */
176 $priority = $this->_toSyslog($priority);
177 if ($this->_inherit) {
178 $priority |= $this->_name;
181 /* Apply the configured line format to the message string. */
182 $message = $this->_format($this->_lineFormat,
183 strftime($this->_timeFormat),
184 $priority, $message);
186 /* Split the string into parts based on our maximum length setting. */
187 $parts = str_split($message, $this->_maxLength);
188 if ($parts === false) {
192 foreach ($parts as $part) {
193 if (!syslog($priority, $part)) {
198 $this->_announce(array('priority' => $priority, 'message' => $message));
204 * Converts a PEAR_LOG_* constant into a syslog LOG_* constant.
206 * This function exists because, under Windows, not all of the LOG_*
207 * constants have unique values. Instead, the PEAR_LOG_* were introduced
208 * for global use, with the conversion to the LOG_* constants kept local to
209 * to the syslog driver.
211 * @param int $priority PEAR_LOG_* value to convert to LOG_* value.
213 * @return The LOG_* representation of $priority.
217 function _toSyslog($priority)
219 static $priorities = array(
220 PEAR_LOG_EMERG => LOG_EMERG,
221 PEAR_LOG_ALERT => LOG_ALERT,
222 PEAR_LOG_CRIT => LOG_CRIT,
223 PEAR_LOG_ERR => LOG_ERR,
224 PEAR_LOG_WARNING => LOG_WARNING,
225 PEAR_LOG_NOTICE => LOG_NOTICE,
226 PEAR_LOG_INFO => LOG_INFO,
227 PEAR_LOG_DEBUG => LOG_DEBUG
230 /* If we're passed an unknown priority, default to LOG_INFO. */
231 if (!is_int($priority) || !in_array($priority, $priorities)) {
235 return $priorities[$priority];