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

Revision 18220, 7.7 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 * LOG4PHP_DIR points to the log4php root directory.
21 *
22 * If not defined it will be set automatically when the first package classfile
23 * is included
24 *
25 * @var string
26 */
27if (!defined('LOG4PHP_DIR')) define('LOG4PHP_DIR', dirname(__FILE__));
28
29require_once(LOG4PHP_DIR . '/LoggerHierarchy.php');
30
31if (!defined('LOG4PHP_DEFAULT_INIT_OVERRIDE')) {
32    if (isset($_ENV['log4php.defaultInitOverride'])) {
33        /**
34         * @ignore
35         */
36        define('LOG4PHP_DEFAULT_INIT_OVERRIDE',
37            LoggerOptionConverter::toBoolean($_ENV['log4php.defaultInitOverride'], false)
38        );
39    } elseif (isset($GLOBALS['log4php.defaultInitOverride'])) {
40        /**
41         * @ignore
42         */
43        define('LOG4PHP_DEFAULT_INIT_OVERRIDE',
44            LoggerOptionConverter::toBoolean($GLOBALS['log4php.defaultInitOverride'], false)
45        );
46    } else {
47        /**
48         * Controls init execution
49         *
50         * With this constant users can skip the default init procedure that is
51         * called when this file is included.
52         *
53         * <p>If it is not user defined, log4php tries to autoconfigure using (in order):</p>
54         *
55         * - the <code>$_ENV['log4php.defaultInitOverride']</code> variable.
56         * - the <code>$GLOBALS['log4php.defaultInitOverride']</code> global variable.
57         * - defaults to <i>false</i>
58         *
59         * @var boolean
60         */
61        define('LOG4PHP_DEFAULT_INIT_OVERRIDE', false);
62    }
63}
64
65if (!defined('LOG4PHP_CONFIGURATION')) {
66    if (isset($_ENV['log4php.configuration'])) {
67        /**
68         * @ignore
69         */
70        define('LOG4PHP_CONFIGURATION', trim($_ENV['log4php.configuration']));
71    } else {
72        /**
73         * Configuration file.
74         *
75         * <p>This constant tells configurator classes where the configuration
76         * file is located.</p>
77         * <p>If not set by user, log4php tries to set it automatically using
78         * (in order):</p>
79         *
80         * - the <code>$_ENV['log4php.configuration']</code> enviroment variable.
81         * - defaults to 'log4php.properties'.
82         *
83         * @var string
84         */
85        define('LOG4PHP_CONFIGURATION', 'log4php.properties');
86    }
87}
88
89if (!defined('LOG4PHP_CONFIGURATOR_CLASS')) {
90    if ( strtolower(substr( LOG4PHP_CONFIGURATION, -4 )) == '.xml') {
91        /**
92         * @ignore
93         */
94        define('LOG4PHP_CONFIGURATOR_CLASS', LOG4PHP_DIR . '/xml/LoggerDOMConfigurator');
95    } else {
96        /**
97         * Holds the configurator class name.
98         *
99         * <p>This constant is set with the fullname (path included but non the
100         * .php extension) of the configurator class that init procedure will use.</p>
101         * <p>If not set by user, log4php tries to set it automatically.</p>
102         * <p>If {@link LOG4PHP_CONFIGURATION} has '.xml' extension set the
103         * constants to '{@link LOG4PHP_DIR}/xml/{@link LoggerDOMConfigurator}'.</p>
104         * <p>Otherwise set the constants to
105         * '{@link LOG4PHP_DIR}/{@link LoggerPropertyConfigurator}'.</p>
106         *
107         * <p><b>Security Note</b>: classfile pointed by this constant will be brutally
108         * included with a:
109         * <code>@include_once(LOG4PHP_CONFIGURATOR_CLASS . ".php");</code></p>
110         *
111         * @var string
112         */
113        define('LOG4PHP_CONFIGURATOR_CLASS', LOG4PHP_DIR . '/LoggerPropertyConfigurator');
114    }
115}
116
117if (!LOG4PHP_DEFAULT_INIT_OVERRIDE) {
118    if (!LoggerManagerDefaultInit())
119        LoggerLog::warn("LOG4PHP main() Default Init failed.");
120}
121
122/**
123 * Default init procedure.
124 *
125 * <p>This procedure tries to configure the {@link LoggerHierarchy} using the
126 * configurator class defined via {@link LOG4PHP_CONFIGURATOR_CLASS} that tries
127 * to load the configurator file defined in {@link LOG4PHP_CONFIGURATION}.
128 * If something goes wrong a warn is raised.</p>
129 * <p>Users can skip this procedure using {@link LOG4PHP_DEFAULT_INIT_OVERRIDE}
130 * constant.</p>
131 *
132 * @return boolean
133 */
134function LoggerManagerDefaultInit()
135{
136    $configuratorClass = basename(LOG4PHP_CONFIGURATOR_CLASS);
137    if (!class_exists($configuratorClass)) {
138        @include_once(LOG4PHP_CONFIGURATOR_CLASS . ".php");
139    }
140    if (class_exists($configuratorClass)) {
141       
142        return call_user_func(array($configuratorClass, 'configure'), LOG4PHP_CONFIGURATION);         
143
144    } else {
145        LoggerLog::warn("LoggerManagerDefaultInit() Configurator '{$configuratorClass}' doesnt exists");
146        return false;
147    }
148}
149
150/**
151 * Use the LoggerManager to get Logger instances.
152 *
153 * @author VxR <vxr@vxr.it>
154 * @version $Revision: 1.17 $
155 * @package log4php
156 * @see Logger
157 * @todo create a configurator selector 
158 */
159class LoggerManager {
160
161    /**
162     * check if a given logger exists.
163     *
164     * @param string $name logger name
165     * @static
166     * @return boolean
167     */
168    function exists($name)
169    {
170        $repository =& LoggerManager::getLoggerRepository();
171        return $repository->exists($name);
172    }
173
174    /**
175     * Returns an array this whole Logger instances.
176     *
177     * @static
178     * @see Logger
179     * @return array
180     */
181    function getCurrentLoggers()
182    {
183        $repository =& LoggerManager::getLoggerRepository();
184        return $repository->getCurrentLoggers();
185    }
186   
187    /**
188     * Returns the root logger.
189     *
190     * @static
191     * @return object
192     * @see LoggerRoot
193     */
194    function &getRootLogger()
195    {
196        $repository =& LoggerManager::getLoggerRepository();
197        return $repository->getRootLogger();
198    }
199   
200    /**
201     * Returns the specified Logger.
202     *
203     * @param string $name logger name
204     * @param LoggerFactory $factory a {@link LoggerFactory} instance or null
205     * @static
206     * @return Logger
207     */
208    function &getLogger($name, $factory = null)
209    {
210        $repository =& LoggerManager::getLoggerRepository();
211        return $repository->getLogger($name, $factory);
212    }
213   
214    /**
215     * Returns the LoggerHierarchy.
216     *
217     * @static
218     * @return LoggerHierarchy
219     */
220    function &getLoggerRepository()
221    {
222        return LoggerHierarchy::singleton();   
223    }
224   
225
226    /**
227     * Destroy loggers object tree.
228     *
229     * @static
230     * @return boolean
231     */
232    function resetConfiguration()
233    {
234        $repository =& LoggerManager::getLoggerRepository();   
235        return $repository->resetConfiguration();   
236    }
237   
238    /**
239     * Does nothing.
240     * @static
241     */
242    function setRepositorySelector($selector, $guard)
243    {
244        return;
245    }
246   
247    /**
248     * Safely close all appenders.
249     * @static
250     */
251    function shutdown()
252    {
253        $repository =& LoggerManager::getLoggerRepository();   
254        return $repository->shutdown();   
255    }
256}
257?>
Note: See TracBrowser for help on using the repository browser.