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