Merge branch 'master' into wip_leon_T8050_add_shenzen_SZ_to_ISIN
[pear] / Log / syslog.php
1 <?php
2 /**
3  * $Header$
4  * $Horde: horde/lib/Log/syslog.php,v 1.6 2000/06/28 21:36:13 jon Exp $
5  *
6  * @version $Revision: 308379 $
7  * @package Log
8  */
9
10 /**
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).
14  *
15  * @author  Chuck Hagenbuch <chuck@horde.org>
16  * @author  Jon Parise <jon@php.net>
17  * @since   Horde 1.3
18  * @since   Log 1.0
19  * @package Log
20  *
21  * @example syslog.php      Using the syslog handler.
22  */
23 class Log_syslog extends Log
24 {
25     /**
26      * Integer holding the log facility to use.
27      * @var integer
28      * @access private
29      */
30     var $_name = LOG_SYSLOG;
31
32     /**
33      * Should we inherit the current syslog connection for this process, or
34      * should we call openlog() to start a new syslog connection?
35      * @var boolean
36      * @access private
37      */
38     var $_inherit = false;
39
40     /**
41      * Should we re-open the syslog connection for each log event?
42      * @var boolean
43      * @access private
44      */
45     var $_reopen = false;
46
47     /**
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.
51      * @var integer
52      * @access private
53      */
54     var $_maxLength = 500;
55
56     /**
57      * String containing the format of a message.
58      * @var string
59      * @access private
60      */
61     var $_lineFormat = '%4$s';
62
63     /**
64      * String containing the timestamp format.  It will be passed directly to
65      * strftime().  Note that the timestamp string will generated using the
66      * current locale.
67      * @var string
68      * @access private
69      */
70     var $_timeFormat = '%b %d %H:%M:%S';
71
72     /**
73      * Constructs a new syslog object.
74      *
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.
79      * @access public
80      */
81     function Log_syslog($name, $ident = '', $conf = array(),
82                         $level = PEAR_LOG_DEBUG)
83     {
84         /* Ensure we have a valid integer value for $name. */
85         if (empty($name) || !is_int($name)) {
86             $name = LOG_SYSLOG;
87         }
88
89         if (isset($conf['inherit'])) {
90             $this->_inherit = $conf['inherit'];
91             $this->_opened = $this->_inherit;
92         }
93         if (isset($conf['reopen'])) {
94             $this->_reopen = $conf['reopen'];
95         }
96         if (isset($conf['maxLength'])) {
97             $this->_maxLength = $conf['maxLength'];
98         }
99         if (!empty($conf['lineFormat'])) {
100             $this->_lineFormat = str_replace(array_keys($this->_formatMap),
101                                              array_values($this->_formatMap),
102                                              $conf['lineFormat']);
103         }
104         if (!empty($conf['timeFormat'])) {
105             $this->_timeFormat = $conf['timeFormat'];
106         }
107
108         $this->_id = md5(microtime());
109         $this->_name = $name;
110         $this->_ident = $ident;
111         $this->_mask = Log::UPTO($level);
112     }
113
114     /**
115      * Opens a connection to the system logger, if it has not already
116      * been opened.  This is implicitly called by log(), if necessary.
117      * @access public
118      */
119     function open()
120     {
121         if (!$this->_opened || $this->_reopen) {
122             $this->_opened = openlog($this->_ident, LOG_PID, $this->_name);
123         }
124
125         return $this->_opened;
126     }
127
128     /**
129      * Closes the connection to the system logger, if it is open.
130      * @access public
131      */
132     function close()
133     {
134         if ($this->_opened && !$this->_inherit) {
135             closelog();
136             $this->_opened = false;
137         }
138
139         return true;
140     }
141
142     /**
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.
146      *
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.
153      * @access public
154      */
155     function log($message, $priority = null)
156     {
157         /* If a priority hasn't been specified, use the default value. */
158         if ($priority === null) {
159             $priority = $this->_priority;
160         }
161
162         /* Abort early if the priority is above the maximum logging level. */
163         if (!$this->_isMasked($priority)) {
164             return false;
165         }
166
167         /* If we need to (re)open the connection and open() fails, abort. */
168         if ((!$this->_opened || $this->_reopen) && !$this->open()) {
169             return false;
170         }
171
172         /* Extract the string representation of the message. */
173         $message = $this->_extractMessage($message);
174
175         /* Build a syslog priority value based on our current configuration. */
176         $priority = $this->_toSyslog($priority);
177         if ($this->_inherit) {
178             $priority |= $this->_name;
179         }
180
181         /* Apply the configured line format to the message string. */
182         $message = $this->_format($this->_lineFormat,
183                                   strftime($this->_timeFormat),
184                                   $priority, $message);
185
186         /* Split the string into parts based on our maximum length setting. */
187         $parts = str_split($message, $this->_maxLength);
188         if ($parts === false) {
189             return false;
190         }
191
192         foreach ($parts as $part) {
193             if (!syslog($priority, $part)) {
194                 return false;
195             }
196         }
197
198         $this->_announce(array('priority' => $priority, 'message' => $message));
199
200         return true;
201     }
202
203     /**
204      * Converts a PEAR_LOG_* constant into a syslog LOG_* constant.
205      *
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.
210      *
211      * @param int $priority     PEAR_LOG_* value to convert to LOG_* value.
212      *
213      * @return  The LOG_* representation of $priority.
214      *
215      * @access private
216      */
217     function _toSyslog($priority)
218     {
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
228         );
229
230         /* If we're passed an unknown priority, default to LOG_INFO. */
231         if (!is_int($priority) || !in_array($priority, $priorities)) {
232             return LOG_INFO;
233         }
234
235         return $priorities[$priority];
236     }
237
238 }