source: branches/comu-ver2/data/module/log4php/php4/log4php/LoggerAppenderSkeleton.php @ 18220

Revision 18220, 9.2 KB checked in by yokkuns, 11 years ago (diff)

#149 ロガークラス作成

Line 
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 */
22if (!defined('LOG4PHP_DIR')) define('LOG4PHP_DIR', dirname(__FILE__));
23 
24/**
25 */
26require_once(LOG4PHP_DIR . '/LoggerAppender.php');
27require_once(LOG4PHP_DIR . '/LoggerLayout.php');
28require_once(LOG4PHP_DIR . '/spi/LoggerFilter.php');
29require_once(LOG4PHP_DIR . '/spi/LoggerLoggingEvent.php');
30require_once(LOG4PHP_DIR . '/helpers/LoggerOptionConverter.php');
31
32/**
33 * Abstract superclass of the other appenders in the package.
34 * 
35 * This class provides the code for common functionality, such as
36 * support for threshold filtering and support for general filters.
37 *
38 * @author  VxR <vxr@vxr.it>
39 * @author  Sergio Strampelli <sergio@ascia.net>
40 * @version $Revision: 1.12 $
41 * @package log4php
42 * @abstract
43 */
44class LoggerAppenderSkeleton extends LoggerAppender {
45
46    /**
47     * @var boolean closed appender flag
48     */
49    var $closed;
50   
51    /**
52     * @var object unused
53     */
54    var $errorHandler;
55           
56    /**
57     * The first filter in the filter chain
58     * @var LoggerFilter
59     */
60    var $headFilter = null;
61           
62    /**
63     * LoggerLayout for this appender. It can be null if appender has its own layout
64     * @var LoggerLayout
65     */
66    var $layout = null;
67           
68    /**
69     * @var string Appender name
70     */
71    var $name;
72           
73    /**
74     * The last filter in the filter chain
75     * @var LoggerFilter
76     */
77    var $tailFilter = null;
78           
79    /**
80     * @var LoggerLevel There is no level threshold filtering by default.
81     */
82    var $threshold = null;
83   
84    /**
85     * @var boolean needs a layout formatting ?
86     */
87    var $requiresLayout = false;
88   
89/* --------------------------------------------------------------------------*/
90/* --------------------------------------------------------------------------*/
91/* --------------------------------------------------------------------------*/
92   
93    /**
94     * Constructor
95     *
96     * @param string $name appender name
97     */
98    function LoggerAppenderSkeleton($name)
99    {
100        $this->name = $name;
101        $this->clearFilters();
102    }
103
104    /**
105     * @param LoggerFilter $newFilter add a new LoggerFilter
106     * @see LoggerAppender::addFilter()
107     */
108    function addFilter($newFilter)
109    {
110        if($this->headFilter == null) {
111            $this->headFilter = $newFilter;
112            $this->tailFilter =& $this->headFilter;
113        } else {
114            $this->tailFilter->next = $newFilter;
115            $this->tailFilter =& $this->tailFilter->next;
116        }
117    }
118   
119    /**
120     * Derived appenders should override this method if option structure
121     * requires it.
122     */
123    function activateOptions()
124    {
125
126    }
127   
128    /**
129     * Subclasses of {@link LoggerAppenderSkeleton} should implement
130     * this method to perform actual logging.
131     *
132     * @param LoggerLoggingEvent $event
133     * @see doAppend()
134     * @abstract
135     */
136    function append($event)
137    {
138        // override me
139    }
140 
141    /**
142     * @see LoggerAppender::clearFilters()
143     */
144    function clearFilters()
145    {
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     * If there is no threshold set, then the return value is always <i>true</i>.
233     * @param LoggerLevel $priority
234     * @return boolean
235     */
236    function isAsSevereAsThreshold($priority)
237    {
238        if ($this->threshold === null)
239            return true;
240           
241        return $priority->isGreaterOrEqual($this->getThreshold());
242    }
243   
244    /**
245     * @see LoggerAppender::doAppend()
246     * @param LoggerLoggingEvent $event
247     */
248    function doAppend($event)
249    {
250        LoggerLog::debug("LoggerAppenderSkeleton::doAppend()");
251
252        if ($this->closed) {
253            LoggerLog::debug("LoggerAppenderSkeleton::doAppend() Attempted to append to closed appender named [{$this->name}].");
254            return;
255        }
256        if(!$this->isAsSevereAsThreshold($event->getLevel())) {
257            LoggerLog::debug("LoggerAppenderSkeleton::doAppend() event level is less severe than threshold.");
258            return;
259        }
260
261        $f = $this->headFilter;
262   
263        while($f !== null) {
264            switch ($f->decide($event)) {
265                case LOG4PHP_LOGGER_FILTER_DENY: return;
266                case LOG4PHP_LOGGER_FILTER_ACCEPT: return $this->append($event);
267                case LOG4PHP_LOGGER_FILTER_NEUTRAL: $f = $f->next;
268            }
269        }
270        $this->append($event);   
271    }   
272       
273           
274    /**
275     * @see LoggerAppender::requiresLayout()
276     * @return boolean
277     */
278    function requiresLayout()
279    {
280        return $this->requiresLayout;
281    }
282           
283    /**
284     * @see LoggerAppender::setErrorHandler()
285     * @param object
286     */
287    function setErrorHandler($errorHandler)
288    {
289        if($errorHandler == null) {
290          // We do not throw exception here since the cause is probably a
291          // bad config file.
292            LoggerLog::warn("You have tried to set a null error-handler.");
293        } else {
294            $this->errorHandler = $errorHandler;
295        }
296    }
297           
298    /**
299     * @see LoggerAppender::setLayout()
300     * @param LoggerLayout $layout
301     */
302    function setLayout($layout)
303    {
304        if ($this->requiresLayout())
305            $this->layout = $layout;
306    }
307 
308    /**
309     * @see LoggerAppender::setName()
310     * @param string $name
311     */
312    function setName($name)
313    {
314        $this->name = $name;   
315    }
316   
317    /**
318     * Set the threshold level of this appender.
319     *
320     * @param mixed $threshold can be a {@link LoggerLevel} object or a string.
321     * @see LoggerOptionConverter::toLevel()
322     */
323    function setThreshold($threshold)
324    {
325        if (is_string($threshold)) {
326           $this->threshold = LoggerOptionConverter::toLevel($threshold, null);
327        }elseif (is_a($threshold, 'loggerlevel')) {
328           $this->threshold = $threshold;
329        }
330    }
331   
332    /**
333     * Perform actions before object serialization.
334     *
335     * Call {@link finalize()} to properly close the appender.
336     */
337    function __sleep()
338    {
339        $this->finalize();
340        return array_keys(get_object_vars($this));
341    }
342   
343    /**
344     * Perform actions after object deserialization.
345     *
346     * Call {@link activateOptions()} to properly setup the appender.
347     */
348    function __wakeup()
349    {
350        $this->activateOptions();
351    }
352   
353}
354?>
Note: See TracBrowser for help on using the repository browser.