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