1 | <?php
|
---|
2 | /**
|
---|
3 | * log4php is a PHP port of the log4j java logging package.
|
---|
4 | *
|
---|
5 | * <p>This framework is based on log4j (see {@link http://jakarta.apache.org/log4j log4j} for details).</p>
|
---|
6 | * <p>Design, strategies and part of the methods documentation are developed by log4j team
|
---|
7 | * (Ceki Gülcü as log4j project founder and
|
---|
8 | * {@link http://jakarta.apache.org/log4j/docs/contributors.html contributors}).</p>
|
---|
9 | *
|
---|
10 | * <p>PHP port, extensions and modifications by VxR. All rights reserved.<br>
|
---|
11 | * For more information, please see {@link http://www.vxr.it/log4php/}.</p>
|
---|
12 | *
|
---|
13 | * <p>This software is published under the terms of the LGPL License
|
---|
14 | * a copy of which has been included with this distribution in the LICENSE file.</p>
|
---|
15 | *
|
---|
16 | * @package log4php
|
---|
17 | */
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * @ignore
|
---|
21 | */
|
---|
22 | if (!defined('LOG4PHP_DIR')) define('LOG4PHP_DIR', dirname(__FILE__));
|
---|
23 |
|
---|
24 | /**
|
---|
25 | */
|
---|
26 | require_once(LOG4PHP_DIR . '/LoggerAppender.php');
|
---|
27 | require_once(LOG4PHP_DIR . '/LoggerLog.php');
|
---|
28 | require_once(LOG4PHP_DIR . '/helpers/LoggerOptionConverter.php');
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * Abstract superclass of the other appenders in the package.
|
---|
32 | *
|
---|
33 | * This class provides the code for common functionality, such as
|
---|
34 | * support for threshold filtering and support for general filters.
|
---|
35 | *
|
---|
36 | * @author VxR <vxr@vxr.it>
|
---|
37 | * @author Sergio Strampelli <sergio@ascia.net>
|
---|
38 | * @version $Revision: 1.15 $
|
---|
39 | * @package log4php
|
---|
40 | * @abstract
|
---|
41 | */
|
---|
42 | class LoggerAppenderSkeleton extends LoggerAppender {
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * @var boolean closed appender flag
|
---|
46 | */
|
---|
47 | var $closed;
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * @var object unused
|
---|
51 | */
|
---|
52 | var $errorHandler;
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * The first filter in the filter chain
|
---|
56 | * @var LoggerFilter
|
---|
57 | */
|
---|
58 | var $headFilter = null;
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * LoggerLayout for this appender. It can be null if appender has its own layout
|
---|
62 | * @var LoggerLayout
|
---|
63 | */
|
---|
64 | var $layout = null;
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * @var string Appender name
|
---|
68 | */
|
---|
69 | var $name;
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * The last filter in the filter chain
|
---|
73 | * @var LoggerFilter
|
---|
74 | */
|
---|
75 | var $tailFilter = null;
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * @var LoggerLevel There is no level threshold filtering by default.
|
---|
79 | */
|
---|
80 | var $threshold = null;
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * @var boolean needs a layout formatting ?
|
---|
84 | */
|
---|
85 | var $requiresLayout = false;
|
---|
86 |
|
---|
87 | /* --------------------------------------------------------------------------*/
|
---|
88 | /* --------------------------------------------------------------------------*/
|
---|
89 | /* --------------------------------------------------------------------------*/
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * Constructor
|
---|
93 | *
|
---|
94 | * @param string $name appender name
|
---|
95 | */
|
---|
96 | function LoggerAppenderSkeleton($name)
|
---|
97 | {
|
---|
98 | $this->name = $name;
|
---|
99 | $this->clearFilters();
|
---|
100 | }
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * @param LoggerFilter $newFilter add a new LoggerFilter
|
---|
104 | * @see LoggerAppender::addFilter()
|
---|
105 | */
|
---|
106 | function addFilter($newFilter)
|
---|
107 | {
|
---|
108 | if($this->headFilter === null) {
|
---|
109 | $this->headFilter = $newFilter;
|
---|
110 | $this->tailFilter =& $this->headFilter;
|
---|
111 | } else {
|
---|
112 | $this->tailFilter->next = $newFilter;
|
---|
113 | $this->tailFilter =& $this->tailFilter->next;
|
---|
114 | }
|
---|
115 | }
|
---|
116 |
|
---|
117 | /**
|
---|
118 | * Derived appenders should override this method if option structure
|
---|
119 | * requires it.
|
---|
120 | */
|
---|
121 | function activateOptions()
|
---|
122 | {
|
---|
123 |
|
---|
124 | }
|
---|
125 |
|
---|
126 | /**
|
---|
127 | * Subclasses of {@link LoggerAppenderSkeleton} should implement
|
---|
128 | * this method to perform actual logging.
|
---|
129 | *
|
---|
130 | * @param LoggerLoggingEvent $event
|
---|
131 | * @see doAppend()
|
---|
132 | * @abstract
|
---|
133 | */
|
---|
134 | function append($event)
|
---|
135 | {
|
---|
136 | // override me
|
---|
137 | }
|
---|
138 |
|
---|
139 | /**
|
---|
140 | * @see LoggerAppender::clearFilters()
|
---|
141 | */
|
---|
142 | function clearFilters()
|
---|
143 | {
|
---|
144 | unset($this->headFilter);
|
---|
145 | unset($this->tailFilter);
|
---|
146 | $this->headFilter = null;
|
---|
147 | $this->tailFilter = null;
|
---|
148 | }
|
---|
149 |
|
---|
150 | /**
|
---|
151 | * @see LoggerAppender::close()
|
---|
152 | */
|
---|
153 | function close()
|
---|
154 | {
|
---|
155 | //override me
|
---|
156 | }
|
---|
157 |
|
---|
158 | /**
|
---|
159 | * Finalize this appender by calling the derived class' <i>close()</i> method.
|
---|
160 | */
|
---|
161 | function finalize()
|
---|
162 | {
|
---|
163 | // An appender might be closed then garbage collected. There is no
|
---|
164 | // point in closing twice.
|
---|
165 | if ($this->closed) return;
|
---|
166 |
|
---|
167 | LoggerLog::debug("LoggerAppenderSkeleton::finalize():name=[{$this->name}].");
|
---|
168 |
|
---|
169 | $this->close();
|
---|
170 | }
|
---|
171 |
|
---|
172 | /**
|
---|
173 | * Do not use this method.
|
---|
174 | * @see LoggerAppender::getErrorHandler()
|
---|
175 | * @return object
|
---|
176 | */
|
---|
177 | function &getErrorHandler()
|
---|
178 | {
|
---|
179 | return $this->errorHandler;
|
---|
180 | }
|
---|
181 |
|
---|
182 | /**
|
---|
183 | * @see LoggerAppender::getFilter()
|
---|
184 | * @return Filter
|
---|
185 | */
|
---|
186 | function &getFilter()
|
---|
187 | {
|
---|
188 | return $this->headFilter;
|
---|
189 | }
|
---|
190 |
|
---|
191 | /**
|
---|
192 | * Return the first filter in the filter chain for this Appender.
|
---|
193 | * The return value may be <i>null</i> if no is filter is set.
|
---|
194 | * @return Filter
|
---|
195 | */
|
---|
196 | function &getFirstFilter()
|
---|
197 | {
|
---|
198 | return $this->headFilter;
|
---|
199 | }
|
---|
200 |
|
---|
201 | /**
|
---|
202 | * @see LoggerAppender::getLayout()
|
---|
203 | * @return LoggerLayout
|
---|
204 | */
|
---|
205 | function &getLayout()
|
---|
206 | {
|
---|
207 | return $this->layout;
|
---|
208 | }
|
---|
209 |
|
---|
210 | /**
|
---|
211 | * @see LoggerAppender::getName()
|
---|
212 | * @return string
|
---|
213 | */
|
---|
214 | function getName()
|
---|
215 | {
|
---|
216 | return $this->name;
|
---|
217 | }
|
---|
218 |
|
---|
219 | /**
|
---|
220 | * Returns this appenders threshold level.
|
---|
221 | * See the {@link setThreshold()} method for the meaning of this option.
|
---|
222 | * @return LoggerLevel
|
---|
223 | */
|
---|
224 | function &getThreshold()
|
---|
225 | {
|
---|
226 | return $this->threshold;
|
---|
227 | }
|
---|
228 |
|
---|
229 | /**
|
---|
230 | * Check whether the message level is below the appender's threshold.
|
---|
231 | *
|
---|
232 | *
|
---|
233 | * If there is no threshold set, then the return value is always <i>true</i>.
|
---|
234 | * @param LoggerLevel $priority
|
---|
235 | * @return boolean true if priority is greater or equal than threshold
|
---|
236 | */
|
---|
237 | function isAsSevereAsThreshold($priority)
|
---|
238 | {
|
---|
239 | if ($this->threshold === null)
|
---|
240 | return true;
|
---|
241 |
|
---|
242 | return $priority->isGreaterOrEqual($this->getThreshold());
|
---|
243 | }
|
---|
244 |
|
---|
245 | /**
|
---|
246 | * @see LoggerAppender::doAppend()
|
---|
247 | * @param LoggerLoggingEvent $event
|
---|
248 | */
|
---|
249 | function doAppend($event)
|
---|
250 | {
|
---|
251 | LoggerLog::debug("LoggerAppenderSkeleton::doAppend()");
|
---|
252 |
|
---|
253 | if ($this->closed) {
|
---|
254 | LoggerLog::debug("LoggerAppenderSkeleton::doAppend() Attempted to append to closed appender named [{$this->name}].");
|
---|
255 | return;
|
---|
256 | }
|
---|
257 | if(!$this->isAsSevereAsThreshold($event->getLevel())) {
|
---|
258 | LoggerLog::debug("LoggerAppenderSkeleton::doAppend() event level is less severe than threshold.");
|
---|
259 | return;
|
---|
260 | }
|
---|
261 |
|
---|
262 | $f = $this->getFirstFilter();
|
---|
263 |
|
---|
264 | while($f !== null) {
|
---|
265 | switch ($f->decide($event)) {
|
---|
266 | case LOG4PHP_LOGGER_FILTER_DENY: return;
|
---|
267 | case LOG4PHP_LOGGER_FILTER_ACCEPT: return $this->append($event);
|
---|
268 | case LOG4PHP_LOGGER_FILTER_NEUTRAL: $f = $f->next;
|
---|
269 | }
|
---|
270 | }
|
---|
271 | $this->append($event);
|
---|
272 | }
|
---|
273 |
|
---|
274 |
|
---|
275 | /**
|
---|
276 | * @see LoggerAppender::requiresLayout()
|
---|
277 | * @return boolean
|
---|
278 | */
|
---|
279 | function requiresLayout()
|
---|
280 | {
|
---|
281 | return $this->requiresLayout;
|
---|
282 | }
|
---|
283 |
|
---|
284 | /**
|
---|
285 | * @see LoggerAppender::setErrorHandler()
|
---|
286 | * @param object
|
---|
287 | */
|
---|
288 | function setErrorHandler($errorHandler)
|
---|
289 | {
|
---|
290 | if($errorHandler == null) {
|
---|
291 | // We do not throw exception here since the cause is probably a
|
---|
292 | // bad config file.
|
---|
293 | LoggerLog::warn("You have tried to set a null error-handler.");
|
---|
294 | } else {
|
---|
295 | $this->errorHandler = $errorHandler;
|
---|
296 | }
|
---|
297 | }
|
---|
298 |
|
---|
299 | /**
|
---|
300 | * @see LoggerAppender::setLayout()
|
---|
301 | * @param LoggerLayout $layout
|
---|
302 | */
|
---|
303 | function setLayout($layout)
|
---|
304 | {
|
---|
305 | if ($this->requiresLayout())
|
---|
306 | $this->layout = $layout;
|
---|
307 | }
|
---|
308 |
|
---|
309 | /**
|
---|
310 | * @see LoggerAppender::setName()
|
---|
311 | * @param string $name
|
---|
312 | */
|
---|
313 | function setName($name)
|
---|
314 | {
|
---|
315 | $this->name = $name;
|
---|
316 | }
|
---|
317 |
|
---|
318 | /**
|
---|
319 | * Set the threshold level of this appender.
|
---|
320 | *
|
---|
321 | * @param mixed $threshold can be a {@link LoggerLevel} object or a string.
|
---|
322 | * @see LoggerOptionConverter::toLevel()
|
---|
323 | */
|
---|
324 | function setThreshold($threshold)
|
---|
325 | {
|
---|
326 | if (is_string($threshold)) {
|
---|
327 | $this->threshold = LoggerOptionConverter::toLevel($threshold, null);
|
---|
328 | }elseif (is_a($threshold, 'loggerlevel')) {
|
---|
329 | $this->threshold = $threshold;
|
---|
330 | }
|
---|
331 | }
|
---|
332 |
|
---|
333 | /**
|
---|
334 | * Perform actions before object serialization.
|
---|
335 | *
|
---|
336 | * Call {@link finalize()} to properly close the appender.
|
---|
337 | */
|
---|
338 | function __sleep()
|
---|
339 | {
|
---|
340 | $this->finalize();
|
---|
341 | return array_keys(get_object_vars($this));
|
---|
342 | }
|
---|
343 |
|
---|
344 | /**
|
---|
345 | * Perform actions after object deserialization.
|
---|
346 | *
|
---|
347 | * Call {@link activateOptions()} to properly setup the appender.
|
---|
348 | */
|
---|
349 | function __wakeup()
|
---|
350 | {
|
---|
351 | $this->activateOptions();
|
---|
352 | }
|
---|
353 |
|
---|
354 | }
|
---|
355 | ?> |
---|