1 |
<?php |
2 |
// $Id: Log.php,v 1.16 2003/01/02 04:41:38 jon Exp $ |
3 |
// $PEAR: Log.php,v 1.16 2003/01/02 04:41:38 jon Exp $ |
4 |
// $Horde: horde/lib/Log.php,v 1.15 2000/06/29 23:39:45 jon Exp $ |
5 |
|
6 |
require_once 'PEAR.php'; |
7 |
|
8 |
define('PEAR_LOG_EMERG', 0); |
9 |
define('PEAR_LOG_ALERT', 1); |
10 |
define('PEAR_LOG_CRIT', 2); |
11 |
define('PEAR_LOG_ERR', 3); |
12 |
define('PEAR_LOG_WARNING', 4); |
13 |
define('PEAR_LOG_NOTICE', 5); |
14 |
define('PEAR_LOG_INFO', 6); |
15 |
define('PEAR_LOG_DEBUG', 7); |
16 |
|
17 |
/** |
18 |
* The Log:: class implements both an abstraction for various logging |
19 |
* mechanisms and the Subject end of a Subject-Observer pattern. |
20 |
* |
21 |
* @author Chuck Hagenbuch <chuck@horde.org> |
22 |
* @author Jon Parise <jon@php.net> |
23 |
* @version $Revision: 1.16 $ |
24 |
* @since Horde 1.3 |
25 |
* @package Log |
26 |
*/ |
27 |
class Log extends PEAR { |
28 |
|
29 |
/** |
30 |
* Indicates whether or not the log can been opened / connected. |
31 |
* |
32 |
* @var boolean |
33 |
* @access private |
34 |
*/ |
35 |
var $_opened = false; |
36 |
|
37 |
/** |
38 |
* The label that uniquely identifies this set of log messages. |
39 |
* |
40 |
* @var string |
41 |
* @access private |
42 |
*/ |
43 |
var $_ident = ''; |
44 |
|
45 |
/** |
46 |
* The maximum priority level at which to log a message. |
47 |
* |
48 |
* @var int |
49 |
* @access private |
50 |
*/ |
51 |
var $_maxLevel = PEAR_LOG_DEBUG; |
52 |
|
53 |
/** |
54 |
* Holds all Log_observer objects that wish to be notified of new messages. |
55 |
* |
56 |
* @var array |
57 |
* @access private |
58 |
*/ |
59 |
var $_listeners = array(); |
60 |
|
61 |
|
62 |
/** |
63 |
* Attempts to return a concrete Log instance of $type. |
64 |
* |
65 |
* @param string $type The type of concrete Log subclass to return. |
66 |
* Attempt to dynamically include the code for |
67 |
* this subclass. Currently, valid values are |
68 |
* 'console', 'syslog', 'sql', 'file', and 'mcal'. |
69 |
* |
70 |
* @param string $name The name of the actually log file, table, or |
71 |
* other specific store to use. Defaults to an |
72 |
* empty string, with which the subclass will |
73 |
* attempt to do something intelligent. |
74 |
* |
75 |
* @param string $ident The identity reported to the log system. |
76 |
* |
77 |
* @param array $conf A hash containing any additional configuration |
78 |
* information that a subclass might need. |
79 |
* |
80 |
* @param int $maxLevel Maximum priority level at which to log. |
81 |
* |
82 |
* @return object Log The newly created concrete Log instance, or an |
83 |
* false on an error. |
84 |
* @access public |
85 |
*/ |
86 |
function &factory($type, $name = '', $ident = '', $conf = array(), |
87 |
$maxLevel = PEAR_LOG_DEBUG) |
88 |
{ |
89 |
$type = strtolower($type); |
90 |
$classfile = 'Log/' . $type . '.php'; |
91 |
if (@include_once $classfile) { |
92 |
$class = 'Log_' . $type; |
93 |
return new $class($name, $ident, $conf, $maxLevel); |
94 |
} else { |
95 |
return false; |
96 |
} |
97 |
} |
98 |
|
99 |
/** |
100 |
* Attempts to return a reference to a concrete Log instance of $type, only |
101 |
* creating a new instance if no log instance with the same parameters |
102 |
* currently exists. |
103 |
* |
104 |
* You should use this if there are multiple places you might create a |
105 |
* logger, you don't want to create multiple loggers, and you don't want to |
106 |
* check for the existance of one each time. The singleton pattern does all |
107 |
* the checking work for you. |
108 |
* |
109 |
* <b>You MUST call this method with the $var = &Log::singleton() syntax. |
110 |
* Without the ampersand (&) in front of the method name, you will not get |
111 |
* a reference, you will get a copy.</b> |
112 |
* |
113 |
* @param string $type The type of concrete Log subclass to return. |
114 |
* Attempt to dynamically include the code for |
115 |
* this subclass. Currently, valid values are |
116 |
* 'console', 'syslog', 'sql', 'file', and 'mcal'. |
117 |
* |
118 |
* @param string $name The name of the actually log file, table, or |
119 |
* other specific store to use. Defaults to an |
120 |
* empty string, with which the subclass will |
121 |
* attempt to do something intelligent. |
122 |
* |
123 |
* @param string $ident The identity reported to the log system. |
124 |
* |
125 |
* @param array $conf A hash containing any additional configuration |
126 |
* information that a subclass might need. |
127 |
* |
128 |
* @param int $maxLevel Minimum priority level at which to log. |
129 |
* |
130 |
* @return object Log The newly created concrete Log instance, or an |
131 |
* false on an error. |
132 |
* @access public |
133 |
*/ |
134 |
function &singleton($type, $name = '', $ident = '', $conf = array(), |
135 |
$maxLevel = PEAR_LOG_DEBUG) |
136 |
{ |
137 |
static $instances; |
138 |
if (!isset($instances)) $instances = array(); |
139 |
|
140 |
$signature = serialize(array($type, $name, $ident, $conf, $maxLevel)); |
141 |
if (!isset($instances[$signature])) { |
142 |
$instances[$signature] = &Log::factory($type, $name, $ident, $conf, |
143 |
$maxLevel); |
144 |
} |
145 |
|
146 |
return $instances[$signature]; |
147 |
} |
148 |
|
149 |
/** |
150 |
* Abstract implementation of the close() method. |
151 |
*/ |
152 |
function close() |
153 |
{ |
154 |
return false; |
155 |
} |
156 |
|
157 |
/** |
158 |
* Abstract implementation of the log() method. |
159 |
*/ |
160 |
function log($message, $priority = PEAR_LOG_INFO) |
161 |
{ |
162 |
return false; |
163 |
} |
164 |
|
165 |
/** |
166 |
* Returns the string representation of a PEAR_LOG_* integer constant. |
167 |
* |
168 |
* @param int $priority A PEAR_LOG_* integer constant. |
169 |
* |
170 |
* @return string The string representation of $priority. |
171 |
*/ |
172 |
function priorityToString($priority) |
173 |
{ |
174 |
$priorities = array( |
175 |
PEAR_LOG_EMERG => 'emergency', |
176 |
PEAR_LOG_ALERT => 'alert', |
177 |
PEAR_LOG_CRIT => 'critical', |
178 |
PEAR_LOG_ERR => 'error', |
179 |
PEAR_LOG_WARNING => 'warning', |
180 |
PEAR_LOG_NOTICE => 'notice', |
181 |
PEAR_LOG_INFO => 'info', |
182 |
PEAR_LOG_DEBUG => 'debug' |
183 |
); |
184 |
|
185 |
return $priorities[$priority]; |
186 |
} |
187 |
|
188 |
/** |
189 |
* Adds a Log_observer instance to the list of observers that are be |
190 |
* notified when a message is logged. |
191 |
* |
192 |
* @param object Log_observer &$logObserver The Log_observer instance to |
193 |
* be added to the $listeners |
194 |
* array. |
195 |
* @access public |
196 |
*/ |
197 |
function attach(&$logObserver) |
198 |
{ |
199 |
if (!is_object($logObserver)) { |
200 |
return false; |
201 |
} |
202 |
|
203 |
$logObserver->_listenerID = uniqid(rand()); |
204 |
|
205 |
$this->_listeners[$logObserver->_listenerID] = &$logObserver; |
206 |
} |
207 |
|
208 |
/** |
209 |
* Removes a Log_observer instance from the list of observers. |
210 |
* |
211 |
* @param object Log_observer $logObserver The Log_observer instance to |
212 |
* be removed from the $listeners |
213 |
* array. |
214 |
* @access public |
215 |
*/ |
216 |
function detach($logObserver) |
217 |
{ |
218 |
if (isset($this->_listeners[$logObserver->_listenerID])) { |
219 |
unset($this->_listeners[$logObserver->_listenerID]); |
220 |
} |
221 |
} |
222 |
|
223 |
/** |
224 |
* Sends any Log_observer objects listening to this Log the message that |
225 |
* was just logged. |
226 |
* |
227 |
* @param array $msgObj The data structure holding all relevant log |
228 |
* information - the message, the priority, what |
229 |
* log this is, etc. |
230 |
*/ |
231 |
function notifyAll($msgObj) |
232 |
{ |
233 |
reset($this->_listeners); |
234 |
foreach ($this->_listeners as $listener) { |
235 |
if ($msgObj['priority'] <= $listener->priority) { |
236 |
$listener->notify($msgObj); |
237 |
} |
238 |
} |
239 |
} |
240 |
|
241 |
/** |
242 |
* Indicates whether this is a composite class. |
243 |
* |
244 |
* @return boolean True if this is a composite class. |
245 |
*/ |
246 |
function isComposite() |
247 |
{ |
248 |
return false; |
249 |
} |
250 |
} |
251 |
|
252 |
?> |