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