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

Revision 18220, 15.7 KB checked in by yokkuns, 11 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 * @category   log4php
19 * @package log4php
20 * @author     Marco Vassura
21 * @license    http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
22 * @version    SVN: $Id: Logger.php 663206 2008-06-04 15:27:16Z carnold $
23 * @link       http://logging.apache.org/log4php
24 */
25
26/**
27 * @ignore
28 */
29if (!defined('LOG4PHP_DIR')) define('LOG4PHP_DIR', dirname(__FILE__));
30 
31/**
32 */
33require_once(LOG4PHP_DIR . '/LoggerLevel.php');
34require_once(LOG4PHP_DIR . '/spi/LoggerLoggingEvent.php');
35
36/**
37 * This class has been deprecated and replaced by the Logger subclass.
38 *
39 * @author       Marco Vassura
40 * @version      $Revision: 663206 $
41 * @package log4php
42 * @see Logger
43 */
44class Logger {
45
46    /**
47     * Additivity is set to true by default, that is children inherit the
48     * appenders of their ancestors by default.
49     * @var boolean
50     */
51        protected $additive       = true;
52   
53    /**
54     * @var string fully qualified class name
55     */ 
56    protected $fqcn           = 'LoggerCategory';
57
58    /**
59     * @var LoggerLevel The assigned level of this category.
60     */
61    var $level          = null;
62   
63    /**
64     * @var string name of this category.
65     */
66    protected $name           = '';
67   
68    /**
69     * @var Logger The parent of this category.
70     */   
71    protected $parent         = null;
72
73    /**
74     * @var LoggerHierarchy the object repository
75     */
76    var $repository     = null;
77
78    /**
79     * @var array collection of appenders
80     * @see LoggerAppender
81     */
82    var $aai            = array();
83   
84/* --------------------------------------------------------------------------*/
85/* --------------------------------------------------------------------------*/
86/* --------------------------------------------------------------------------*/
87
88    /**
89     * Constructor.
90     *
91     * @param  string  $name  Category name   
92     */
93    public function __construct($name)
94    {
95        $this->name = $name;
96    }
97   
98    /**
99     * Add a new Appender to the list of appenders of this Category instance.
100     *
101     * @param LoggerAppender $newAppender
102     */
103    public function addAppender($newAppender)
104    {
105        $appenderName = $newAppender->getName();
106        $this->aai[$appenderName] = $newAppender;
107    }
108           
109    /**
110     * If assertion parameter is false, then logs msg as an error statement.
111     *
112     * @param bool $assertion
113     * @param string $msg message to log
114     */
115    public function assertLog($assertion = true, $msg = '')
116    {
117        if ($assertion == false) {
118            $this->error($msg);
119        }
120    }
121
122    /**
123     * Call the appenders in the hierarchy starting at this.
124     *
125     * @param LoggerLoggingEvent $event
126     */
127    public function callAppenders($event)
128    {
129        if (sizeof($this->aai) > 0) {
130            foreach (array_keys($this->aai) as $appenderName) {
131                $this->aai[$appenderName]->doAppend($event);
132            }
133        }
134        if ($this->parent != null and $this->getAdditivity()) {
135            $this->parent->callAppenders($event);
136        }
137    }
138   
139    /**
140     * Log a message object with the DEBUG level including the caller.
141     *
142     * @param mixed $message message
143     * @param mixed $caller caller object or caller string id
144     */
145    public function debug($message, $caller = null)
146    {
147        $debugLevel = LoggerLevel::getLevelDebug();
148        if ($this->repository->isDisabled($debugLevel)) {
149            return;
150        }
151        if ($debugLevel->isGreaterOrEqual($this->getEffectiveLevel())) {
152            $this->forcedLog($this->fqcn, $caller, $debugLevel, $message);
153        }
154    }
155
156    /**
157     * Log a message object with the ERROR level including the caller.
158     *
159     * @param mixed $message message
160     * @param mixed $caller caller object or caller string id
161     */
162    public function error($message, $caller = null)
163    {
164        $errorLevel = LoggerLevel::getLevelError();
165        if ($this->repository->isDisabled($errorLevel)) {
166            return;
167        }
168        if ($errorLevel->isGreaterOrEqual($this->getEffectiveLevel())) {
169            $this->forcedLog($this->fqcn, $caller, $errorLevel, $message);
170        }
171    }
172 
173    /**
174     * Deprecated. Please use LoggerManager::exists() instead.
175     *
176     * @param string $name
177     * @see LoggerManager::exists()
178     * @deprecated
179     */
180    public function exists($name)
181    {
182        return LoggerManager::exists($name);
183    }
184 
185    /**
186     * Log a message object with the FATAL level including the caller.
187     *
188     * @param mixed $message message
189     * @param mixed $caller caller object or caller string id
190     */
191    public function fatal($message, $caller = null)
192    {
193        $fatalLevel = LoggerLevel::getLevelFatal();
194        if ($this->repository->isDisabled($fatalLevel)) {
195            return;
196        }
197        if ($fatalLevel->isGreaterOrEqual($this->getEffectiveLevel())) {
198            $this->forcedLog($this->fqcn, $caller, $fatalLevel, $message);
199        }
200    }
201 
202    /**
203     * This method creates a new logging event and logs the event without further checks.
204     *
205     * It should not be called directly. Use {@link info()}, {@link debug()}, {@link warn()},
206     * {@link error()} and {@link fatal()} wrappers.
207     *
208     * @param string $fqcn Fully Qualified Class Name of the Logger
209     * @param mixed $caller caller object or caller string id
210     * @param LoggerLevel $level log level     
211     * @param mixed $message message
212     * @see LoggerLoggingEvent         
213     */
214    public function forcedLog($fqcn, $caller, $level, $message)
215    {
216        // $fqcn = is_object($caller) ? get_class($caller) : (string)$caller;
217        $this->callAppenders(new LoggerLoggingEvent($fqcn, $this, $level, $message));
218    }
219
220    /**
221     * Get the additivity flag for this Category instance.
222     * @return boolean
223     */
224    public function getAdditivity()
225    {
226        return $this->additive;
227    }
228 
229    /**
230     * Get the appenders contained in this category as an array.
231     * @return array collection of appenders
232     */
233    public function getAllAppenders()
234    {
235        return array_values($this->aai);
236    }
237   
238    /**
239     * Look for the appender named as name.
240     * @return LoggerAppender
241     */
242    public function getAppender($name)
243    {
244        return $this->aai[$name];
245    }
246   
247    /**
248     * Please use the {@link getEffectiveLevel()} method instead.
249     * @deprecated
250     */
251    public function getChainedPriority()
252    {
253        return $this->getEffectiveLevel();
254    }
255 
256    /**
257     * Please use {@link LoggerManager::getCurrentLoggers()} instead.
258     * @deprecated
259     */
260    public function getCurrentCategories()
261    {
262        return LoggerManager::getCurrentLoggers();
263    }
264 
265    /**
266     * Please use {@link LoggerManager::getLoggerRepository()} instead.
267     * @deprecated
268     */
269    public function getDefaultHierarchy()
270    {
271        return LoggerManager::getLoggerRepository();
272    }
273 
274    /**
275     * @deprecated Use {@link getLoggerRepository()}
276     * @return LoggerHierarchy
277     */
278    public function getHierarchy()
279    {
280        return $this->getLoggerRepository();
281    }
282
283    /**
284     * Starting from this category, search the category hierarchy for a non-null level and return it.
285     * @see LoggerLevel
286     * @return LoggerLevel or null
287     */
288    public function getEffectiveLevel()
289    {
290        for($c = $this; $c != null; $c = $c->parent) {
291            if($c->getLevel() !== null)
292                return $c->getLevel();
293        }
294        return null;
295    }
296 
297    /**
298     * Retrieve a category with named as the name parameter.
299     * @return Logger
300     */
301    public function getInstance($name)
302    {
303        return LoggerManager::getLogger($name);
304    }
305
306    /**
307     * Returns the assigned Level, if any, for this Category.
308     * @return LoggerLevel or null
309     */
310    public function getLevel()
311    {
312        return $this->level;
313    }
314   
315    /**
316     * Get a Logger by name (Delegate to {@link LoggerManager})
317     * @param string $name logger name
318     * @param LoggerFactory $factory a {@link LoggerFactory} instance or null
319     * @return Logger
320     * @static
321     */   
322    public function getLogger($name, $factory = null)
323    {
324        return LoggerManager::getLogger($name, $factory);
325    }
326   
327    /**
328     * Return the the repository where this Category is attached.
329     * @return LoggerHierarchy
330     */
331    public function getLoggerRepository()
332    {
333        return $this->repository;
334    }
335
336    /**
337     * Return the category name.
338     * @return string
339     */
340    public function getName()
341    {
342        return $this->name;
343    }
344
345    /**
346     * Returns the parent of this category.
347     * @return Logger
348     */
349    public function getParent()
350    {
351        return $this->parent;
352    }     
353
354    /**
355     * Please use getLevel() instead.
356     * @deprecated
357     */
358    public function getPriority()
359    {
360        return $this->getLevel();
361    }
362         
363    /**
364     * Return the inherited ResourceBundle for this category.
365     */
366    public function getResourceBundle()
367    {
368        return;
369    }
370
371    /**
372     * Returns the string resource corresponding to key in this category's inherited resource bundle.
373     */
374    public function getResourceBundleString($key)
375    {
376        return;
377    }
378
379    /**
380     * Return the root of the default category hierarchy.
381     * @return LoggerRoot
382     */
383    public function getRoot()
384    {
385        return LoggerManager::getRootLogger();
386    }
387
388    /**
389     * get the Root Logger (Delegate to {@link LoggerManager})
390     * @return LoggerRoot
391     * @static
392     */   
393    public function getRootLogger()
394    {
395        return LoggerManager::getRootLogger();   
396    }
397
398    /**
399     * Log a message object with the INFO Level.
400     *
401     * @param mixed $message message
402     * @param mixed $caller caller object or caller string id
403     */
404    public function info($message, $caller = null)
405    {
406        $infoLevel = LoggerLevel::getLevelInfo();
407        if ($this->repository->isDisabled($infoLevel)) {
408            return;
409        }
410        if ($infoLevel->isGreaterOrEqual($this->getEffectiveLevel())) {
411            $this->forcedLog($this->fqcn, $caller, $infoLevel, $message);
412        }
413    }
414     
415    /**
416     * Is the appender passed as parameter attached to this category?
417     *
418     * @param LoggerAppender $appender
419     */
420    public function isAttached($appender)
421    {
422        return isset($this->aai[$appender->getName()]);
423    }
424           
425    /**
426     * Check whether this category is enabled for the DEBUG Level.
427     * @return boolean
428     */
429    public function isDebugEnabled()
430    {
431        $debugLevel = LoggerLevel::getLevelDebug();
432        if ($this->repository->isDisabled($debugLevel)) {
433            return false;
434        }
435        return ($debugLevel->isGreaterOrEqual($this->getEffectiveLevel()));
436    }       
437
438    /**
439     * Check whether this category is enabled for a given Level passed as parameter.
440     *
441     * @param LoggerLevel level
442     * @return boolean
443     */
444    public function isEnabledFor($level)
445    {
446        if ($this->repository->isDisabled($level)) {
447            return false;
448        }
449        return (bool)($level->isGreaterOrEqual($this->getEffectiveLevel()));
450    }
451
452    /**
453     * Check whether this category is enabled for the info Level.
454     * @return boolean
455     * @see LoggerLevel
456     */
457    public function isInfoEnabled()
458    {
459        $infoLevel = LoggerLevel::getLevelInfo();
460        if ($this->repository->isDisabled($infoLevel)) {
461            return false;
462        }
463        return ($infoLevel->isGreaterOrEqual($this->getEffectiveLevel()));
464    }
465
466    /**
467     * Log a localized and parameterized message.
468     */
469    public function l7dlog($priority, $key, $params, $t)
470    {
471        return;
472    }
473
474    /**
475     * This generic form is intended to be used by wrappers.
476     *
477     * @param LoggerLevel $priority a valid level
478     * @param mixed $message message
479     * @param mixed $caller caller object or caller string id
480     */
481    public function log($priority, $message, $caller = null)
482    {
483        if ($this->repository->isDisabled($priority)) {
484            return;
485        }
486        if ($priority->isGreaterOrEqual($this->getEffectiveLevel())) {
487            $this->forcedLog($this->fqcn, $caller, $priority, $message);
488        }
489    }
490
491    /**
492     * Remove all previously added appenders from this Category instance.
493     */
494    public function removeAllAppenders()
495    {
496        $appenderNames = array_keys($this->aai);
497        $enumAppenders = sizeof($appenderNames);
498        for ($i = 0; $i < $enumAppenders; $i++) {
499            $this->removeAppender($appenderNames[$i]);
500        }
501    }
502           
503    /**
504     * Remove the appender passed as parameter form the list of appenders.
505     *
506     * @param mixed $appender can be an appender name or a {@link LoggerAppender} object
507     */
508    public function removeAppender($appender)
509    {
510        if ($appender instanceof LoggerAppender) {
511            $appender->close();
512            unset($this->aai[$appender->getName()]);
513        } elseif (is_string($appender) and isset($this->aai[$appender])) {
514            $this->aai[$appender]->close();
515            unset($this->aai[$appender]);
516        }
517    }
518
519    /**
520     * Set the additivity flag for this Category instance.
521     *
522     * @param boolean $additive
523     */
524    public function setAdditivity($additive)
525    {
526        $this->additive = (bool)$additive;
527    }
528   
529    /**
530     * @deprecated Please use {@link setLevel()} instead.
531     * @see setLevel()
532     */
533    public function setPriority($priority)
534    {
535        $this->setLevel($priority);
536    }
537
538    /**
539     * Only the Hierarchy class can set the hierarchy of a
540     * category.
541     *
542     * @param LoggerHierarchy $repository
543     */
544    public function setHierarchy($repository)
545    {
546        $this->repository = $repository;
547    }
548
549    /**
550     * Set the level of this Category.
551     *
552     * @param LoggerLevel $level a level string or a level constant
553     */
554    public function setLevel($level)
555    {
556        $this->level = $level;
557    }
558   
559    public function setParent($logger) {
560        if ($logger instanceof Logger) {
561                $this->parent = $logger;
562        }
563    }
564
565    /**
566     * Set the resource bundle to be used with localized logging methods
567     */
568    public function setResourceBundle($bundle)
569    {
570        return;
571    }
572           
573    /**
574     * @deprecated use {@link LoggerManager::shutdown()} instead.
575     * @see LoggerManager::shutdown()
576     */
577    public function shutdown()
578    {
579        LoggerManager::shutdown();
580    }
581 
582    /**
583     * Log a message with the WARN level.
584     *
585     * @param mixed $message message
586     * @param mixed $caller caller object or caller string id
587     */
588    public function warn($message, $caller = null)
589    {
590        $warnLevel = LoggerLevel::getLevelWarn();
591        if ($this->repository->isDisabled($warnLevel)) {
592            return;
593        }
594        if ($warnLevel->isGreaterOrEqual($this->getEffectiveLevel())) {
595            $this->forcedLog($this->fqcn, $caller, $warnLevel, $message);
596        }
597    }
598
599}
Note: See TracBrowser for help on using the repository browser.