1 |
<?php |
2 |
// $Id: Log.php,v 1.2 2001/01/04 15:56:05 chagenbu Exp $ |
3 |
// $Horde: horde/lib/Log.php,v 1.15 2000/06/29 23:39:45 jon Exp $ |
4 |
|
5 |
/** |
6 |
* The Log:: class implements both an abstraction for various logging |
7 |
* mechanisms and the Subject end of a Subject-Observer pattern. |
8 |
* |
9 |
* @author Chuck Hagenbuch <chuck@horde.org> |
10 |
* @author Jon Parise <jon@csh.rit.edu> |
11 |
* @version $Revision: 1.2 $ |
12 |
* @since Horde 1.3 |
13 |
*/ |
14 |
class Log { |
15 |
|
16 |
// {{{ properties |
17 |
|
18 |
/** Boolean indicating whether or not the log connection is |
19 |
currently open. */ |
20 |
var $opened = false; |
21 |
|
22 |
/** String holding the identifier that will be stored along with |
23 |
each logged message. */ |
24 |
var $ident = ''; |
25 |
|
26 |
/** Array holding all Log_observer objects that wish to be notified |
27 |
of any messages that we handle. */ |
28 |
var $listeners = array(); |
29 |
|
30 |
// }}} |
31 |
|
32 |
// {{{ factory() |
33 |
/** |
34 |
* Attempts to return a concrete Log instance of $log_type. |
35 |
* |
36 |
* @param $log_type The type of concrete Log subclass to return. |
37 |
* Attempt to dynamically include the code for this |
38 |
* subclass. Currently, valid values are 'syslog', |
39 |
* 'sql', 'file', and 'mcal'. |
40 |
* |
41 |
* @param $log_name (optional) The name of the actually log file, |
42 |
* table, or other specific store to use. Defaults |
43 |
* to an empty string, with which the subclass will |
44 |
* attempt to do something intelligent. |
45 |
* |
46 |
* @param $ident (optional) The indentity reported to the log system. |
47 |
* |
48 |
* @param $conf (optional) A hash containing any additional |
49 |
* configuration information that a subclass might need. |
50 |
* |
51 |
* @return The newly created concrete Log instance, or an |
52 |
* false on an error. |
53 |
*/ |
54 |
function factory ($log_type, $log_name = '', $ident = '', $conf = array()) { |
55 |
$log_type = strtolower($log_type); |
56 |
$classfile = 'Log/' . $log_type . '.php'; |
57 |
if (@include_once $classfile) { |
58 |
$class = 'Log_' . $log_type; |
59 |
return new $class($log_name, $ident, $conf); |
60 |
} else { |
61 |
return false; |
62 |
} |
63 |
} |
64 |
// }}} |
65 |
|
66 |
// {{{ singleton() |
67 |
/** |
68 |
* Attempts to return a reference to a concrete Log instance of |
69 |
* $log_type, only creating a new instance if no log instance with |
70 |
* the same parameters currently exists. |
71 |
* |
72 |
* You should use this if there are multiple places you might |
73 |
* create a logger, you don't want to create multiple loggers, and |
74 |
* you don't want to check for the existance of one each time. The |
75 |
* singleton pattern does all the checking work for you. |
76 |
* |
77 |
* <b>You MUST call this method with the $var = &Log::singleton() |
78 |
* syntax. Without the ampersand (&) in front of the method name, |
79 |
* you will not get a reference, you will get a copy.</b> |
80 |
* |
81 |
* @param $log_type The type of concrete Log subclass to return. |
82 |
* Attempt to dynamically include the code for |
83 |
* this subclass. Currently, valid values are |
84 |
* 'syslog', 'sql', 'file', and 'mcal'. |
85 |
* |
86 |
* @param $log_name (optional) The name of the actually log file, |
87 |
* table, or other specific store to use. Defaults |
88 |
* to an empty string, with which the subclass will |
89 |
* attempt to do something intelligent. |
90 |
* |
91 |
* @param $ident (optional) The identity reported to the log system. |
92 |
* |
93 |
* @param $conf (optional) A hash containing any additional |
94 |
* configuration information that a subclass might need. |
95 |
* |
96 |
* @return The concrete Log reference, or false on an error. |
97 |
*/ |
98 |
function &singleton ($log_type, $log_name = '', $ident = '', $conf = array()) { |
99 |
static $instances; |
100 |
if (!isset($instances)) $instances = array(); |
101 |
|
102 |
$signature = md5($log_type . '][' . $log_name . '][' . $ident . '][' . implode('][', $conf)); |
103 |
if (!isset($instances[$signature])) { |
104 |
$instances[$signature] = Log::factory($log_type, $log_name, $ident, $conf); |
105 |
} |
106 |
return $instances[$signature]; |
107 |
} |
108 |
// }}} |
109 |
|
110 |
// {{{ priorityToString() |
111 |
/** |
112 |
* Returns the string representation of a LOG_* integer constant. |
113 |
* |
114 |
* @param $priority The LOG_* integer constant. |
115 |
* |
116 |
* @return The string representation of $priority. |
117 |
*/ |
118 |
function priorityToString ($priority) { |
119 |
$priorities = array( |
120 |
LOG_EMERG => 'emergency', |
121 |
LOG_ALERT => 'alert', |
122 |
LOG_CRIT => 'critical', |
123 |
LOG_ERR => 'error', |
124 |
LOG_WARNING => 'warning', |
125 |
LOG_NOTICE => 'notice', |
126 |
LOG_INFO => 'info', |
127 |
LOG_DEBUG => 'debug' |
128 |
); |
129 |
return $priorities[$priority]; |
130 |
} |
131 |
// }}} |
132 |
|
133 |
// {{{ attach() |
134 |
/** |
135 |
* Adds a Log_observer instance to the list of observers that are |
136 |
* be notified when a message is logged. |
137 |
* |
138 |
* @param $logObserver The Log_observer instance to be added to |
139 |
* the $listeners array. |
140 |
*/ |
141 |
function attach (&$logObserver) { |
142 |
if (!is_object($logObserver)) |
143 |
return false; |
144 |
|
145 |
$logObserver->_listenerID = uniqid(rand()); |
146 |
|
147 |
$this->listeners[$logObserver->_listenerID] = &$logObserver; |
148 |
} |
149 |
// }}} |
150 |
|
151 |
// {{{ detach() |
152 |
/** |
153 |
* Removes a Log_observer instance from the list of observers. |
154 |
* |
155 |
* @param $logObserver The Log_observer instance to be removed |
156 |
* from the $listeners array. |
157 |
*/ |
158 |
function detach ($logObserver) { |
159 |
if (isset($this->listeners[$logObserver->_listenerID])) |
160 |
unset($this->listeners[$logObserver->_listenerID]); |
161 |
} |
162 |
// }}} |
163 |
|
164 |
// {{{ notifyAll() |
165 |
/** |
166 |
* Sends any Log_observer objects listening to this Log the message |
167 |
* that was just logged. |
168 |
* |
169 |
* @param $messageOb The data structure holding all relevant log |
170 |
* information - the message, the priority, what |
171 |
* log this is, etc. |
172 |
*/ |
173 |
function notifyAll ($messageOb) { |
174 |
reset($this->listeners); |
175 |
foreach ($this->listeners as $listener) { |
176 |
if ($messageOb['priority'] <= $listener->priority) |
177 |
$listener->notify($messageOb); |
178 |
} |
179 |
} |
180 |
// }}} |
181 |
|
182 |
// {{{ isComposite() |
183 |
/** |
184 |
* @return a Boolean: true if this is a composite class, false |
185 |
* otherwise. The composite subclass overrides this to return |
186 |
* true. |
187 |
*/ |
188 |
function isComposite () { |
189 |
return false; |
190 |
} |
191 |
// }}} |
192 |
} |
193 |
|
194 |
?> |