source: branches/comu-ver2/data/module/log4php/php5/log4php/LoggerHierarchy.php @ 18220

Revision 18220, 10.5 KB checked in by yokkuns, 15 years ago (diff)

#149 ロガークラス作成

Line 
1<?php
2/**
3 * Licensed to the Apache Software Foundation (ASF) under one or more
4 * contributor license agreements.  See the NOTICE file distributed with
5 * this work for additional information regarding copyright ownership.
6 * The ASF licenses this file to You under the Apache License, Version 2.0
7 * (the "License"); you may not use this file except in compliance with
8 * the License.  You may obtain a copy of the License at
9 *
10 *     http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 *
19 * @package log4php
20 */
21
22/**
23 * @ignore
24 */
25if (!defined('LOG4PHP_DIR')) define('LOG4PHP_DIR', dirname(__FILE__));
26
27/**
28 */
29require_once(LOG4PHP_DIR . '/LoggerLog.php');
30require_once(LOG4PHP_DIR . '/LoggerLevel.php');
31require_once(LOG4PHP_DIR . '/LoggerRoot.php');
32require_once(LOG4PHP_DIR . '/or/LoggerRendererMap.php');
33require_once(LOG4PHP_DIR . '/LoggerDefaultCategoryFactory.php');
34
35/**
36 * This class is specialized in retrieving loggers by name and also maintaining
37 * the logger hierarchy.
38 *
39 * <p>The casual user does not have to deal with this class directly.</p>
40 *
41 * <p>The structure of the logger hierarchy is maintained by the
42 * getLogger method. The hierarchy is such that children link
43 * to their parent but parents do not have any pointers to their
44 * children. Moreover, loggers can be instantiated in any order, in
45 * particular descendant before ancestor.</p>
46 *
47 * <p>In case a descendant is created before a particular ancestor,
48 * then it creates a provision node for the ancestor and adds itself
49 * to the provision node. Other descendants of the same ancestor add
50 * themselves to the previously created provision node.</p>
51 *
52 * @author  Marco Vassura
53 * @version $Revision: 635069 $
54 * @package log4php
55 */
56class LoggerHierarchy {
57
58    /**
59     * @var object currently unused
60     */
61    protected $defaultFactory;
62   
63    /**
64     * @var boolean activate internal logging
65     * @see LoggerLog
66     */
67    public $debug = false;
68
69    /**
70     * @var array hierarchy tree. saves here all loggers
71     */
72    protected $ht = array();
73   
74    /**
75     * @var LoggerRoot
76     */
77    protected $root = null;
78   
79    /**
80     * @var LoggerRendererMap
81     */
82    protected $rendererMap;
83
84    /**
85     * @var LoggerLevel main level threshold
86     */
87    protected $threshold;
88   
89    /**
90     * @var boolean currently unused
91     */
92    protected $emittedNoAppenderWarning       = false;
93
94    /**
95     * @var boolean currently unused
96     */
97    protected $emittedNoResourceBundleWarning = false;
98   
99    public static function singleton()
100    {
101        static $instance;
102       
103        if (!isset($instance))
104            $instance = new LoggerHierarchy(new LoggerRoot());
105        return $instance;
106    }
107   
108    /**
109     * Create a new logger hierarchy.
110     * @param object $root the root logger
111     */
112    protected function __construct($root)
113    {
114        $this->root = $root;
115        // Enable all level levels by default.
116        $this->setThreshold(LoggerLevel::getLevelAll());
117        $this->root->setHierarchy($this);
118        $this->rendererMap = new LoggerRendererMap();
119        $this->defaultFactory = new LoggerDefaultCategoryFactory();       
120    }
121     
122    /**
123     * Add a HierarchyEventListener event to the repository.
124     * Not Yet Impl.
125     */
126    public function addHierarchyEventListener($listener)
127    {
128        return;
129    }
130     
131    /**
132     * Add an object renderer for a specific class.
133     * @param string $classToRender
134     * @param LoggerObjectRenderer $or
135     */
136    public function addRenderer($classToRender, $or)
137    {
138        $this->rendererMap->put($classToRender, $or);
139    }
140   
141    /**
142     * This call will clear all logger definitions from the internal hashtable.
143     */
144    public function clear()
145    {
146        $this->ht = array();
147    }
148     
149    public function emitNoAppenderWarning($cat)
150    {
151        return;
152    }
153   
154    /**
155     * Check if the named logger exists in the hierarchy.
156     * @param string $name
157     * @return boolean
158     */
159    public function exists($name)
160    {
161        return isset($this->ht[$name]);
162    }
163
164        /**
165         * Not Implemented.
166         * @param Logger $logger
167         * @param LoggerAppender $appender
168         */
169    public function fireAddAppenderEvent($logger, $appender)
170    {
171        return;
172    }
173   
174    /**
175     * @deprecated Please use {@link getCurrentLoggers()} instead.
176     */
177    public function getCurrentCategories()
178    {
179        return $this->getCurrentLoggers();
180    }
181   
182    /**
183     * Returns all the currently defined categories in this hierarchy as an array.
184     * @return array
185     */ 
186    public function getCurrentLoggers()
187    {
188        return array_values($this->ht);
189    }
190   
191    /**
192     * Return a new logger instance named as the first parameter using the default factory.
193     *
194     * @param string $name logger name
195     * @param LoggerFactory $factory a {@link LoggerFactory} instance or null     
196     * @return Logger
197     */
198    public function getLogger($name, $factory = null)
199    {
200        if ($factory === null) {
201            return $this->getLoggerByFactory($name, $this->defaultFactory);
202        } else {
203            return $this->getLoggerByFactory($name, $factory);
204        }
205    }
206   
207    /**
208     * Return a new logger instance named as the first parameter using the default factory.
209     *
210     * @param string $name logger name
211     * @return Logger
212     * @todo merge with {@link getLogger()}
213     */
214    public function getLoggerByFactory($name, $factory)
215    {
216        if (!isset($this->ht[$name])) {
217            $this->ht[$name] = $factory->makeNewLoggerInstance($name);
218            $this->ht[$name]->setHierarchy($this);
219            $nodes = explode('.', $name);
220            $firstNode = array_shift($nodes);
221            if ( $firstNode != $name and isset($this->ht[$firstNode])) {
222                $this->ht[$name]->setParent($this->ht[$firstNode]);
223            } else {
224                $this->ht[$name]->setParent($this->root);
225            }
226            if (sizeof($nodes) > 0) {
227                // find parent node
228                foreach ($nodes as $node) {
229                    $parentNode = "$firstNode.$node";
230                    if (isset($this->ht[$parentNode]) and $parentNode != $name) {
231                        $this->ht[$name]->setParent($this->ht[$parentNode]);
232                    }
233                    $firstNode .= ".$node";
234                }
235            }
236        }           
237        return $this->ht[$name];
238    }
239   
240    /**
241     * @return LoggerRendererMap Get the renderer map for this hierarchy.
242     */
243    public function getRendererMap()
244    {
245        return $this->rendererMap;
246    }
247   
248    /**
249     * @return LoggerRoot Get the root of this hierarchy.
250     */
251    public function getRootLogger()
252    {
253        if (!isset($this->root) or $this->root == null)
254            $this->root = new LoggerRoot();
255        return $this->root;
256    }
257     
258    /**
259     * @return LoggerLevel Returns the threshold Level.
260     */
261    public function getThreshold()
262    {
263        return $this->threshold;
264    }
265
266    /**
267     * This method will return true if this repository is disabled
268     * for level object passed as parameter and false otherwise.
269     * @return boolean
270     */
271    public function isDisabled($level)
272    {
273        return ($this->threshold->level > $level->level);
274    }
275   
276    /**
277     * @deprecated Deprecated with no replacement.
278     */
279    public function overrideAsNeeded($override)
280    {
281        return;
282    }
283   
284    /**
285     * Reset all values contained in this hierarchy instance to their
286     * default.
287     *
288     * This removes all appenders from all categories, sets
289     * the level of all non-root categories to <i>null</i>,
290     * sets their additivity flag to <i>true</i> and sets the level
291     * of the root logger to {@link LOGGER_LEVEL_DEBUG}.  Moreover,
292     * message disabling is set its default "off" value.
293     *
294     * <p>Existing categories are not removed. They are just reset.
295     *
296     * <p>This method should be used sparingly and with care as it will
297     * block all logging until it is completed.</p>
298     */
299    public function resetConfiguration()
300    {
301        $root = $this->getRootLogger();
302       
303        $root->setLevel(LoggerLevel::getLevelDebug());
304        $this->setThreshold(LoggerLevel::getLevelAll());
305        $this->shutDown();
306        $loggers = $this->getCurrentLoggers();
307        $enumLoggers = sizeof($loggers);
308        for ($i = 0; $i < $enumLoggers; $i++) {
309            $loggers[$i]->setLevel(null);
310                $loggers[$i]->setAdditivity(true);
311                $loggers[$i]->setResourceBundle(null);
312                $loggers[$i]->removeAllAppenders();
313        }
314        $this->rendererMap->clear();
315    }
316     
317    /**
318     * @deprecated Deprecated with no replacement.
319     */
320    public function setDisableOverride($override)
321    {
322        return;
323    }
324   
325    /**
326     * Used by subclasses to add a renderer to the hierarchy passed as parameter.
327     * @param string $renderedClass a LoggerRenderer class name
328     * @param LoggerRenderer $renderer
329     *
330     */
331    public function setRenderer($renderedClass, $renderer)
332    {
333        $this->rendererMap->put($renderedClass, $renderer);
334    }
335   
336    /**
337     * set a new threshold level
338     *
339     * @param LoggerLevel $l
340     */
341    public function setThreshold($l)
342    {
343        if ($l !== null)
344            $this->threshold = $l;
345    }
346   
347    /**
348     * Shutting down a hierarchy will <i>safely</i> close and remove
349     * all appenders in all categories including the root logger.
350     *
351     * <p>Some appenders such as {@link LoggerSocketAppender}
352     * need to be closed before the
353     * application exists. Otherwise, pending logging events might be
354     * lost.
355     *
356     * <p>The shutdown method is careful to close nested
357     * appenders before closing regular appenders. This is allows
358     * configurations where a regular appender is attached to a logger
359     * and again to a nested appender.
360     */
361    public function shutdown()
362    {
363        $this->root->removeAllAppenders();
364        $cats = $this->getCurrentLoggers();
365        $enumCats = sizeof($cats);       
366        if ($enumCats > 0) {
367            for ($i = 0; $i < $enumCats; $i++) {
368                $cats[$i]->removeAllAppenders();
369            }
370        }
371    } 
372}
Note: See TracBrowser for help on using the repository browser.