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
|
* @package log4php
|
19
|
*/
|
20
|
|
21
|
/**
|
22
|
* This class is specialized in retrieving loggers by name and also maintaining
|
23
|
* the logger hierarchy. The logger hierarchy is dealing with the several Log-Levels
|
24
|
* Logger can have. From log4j website:
|
25
|
*
|
26
|
* "A logger is said to be an ancestor of another logger if its name followed
|
27
|
* by a dot is a prefix of the descendant logger name. A logger is said to be
|
28
|
* a parent of a child logger if there are no ancestors between itself and the
|
29
|
* descendant logger."
|
30
|
*
|
31
|
* Child Loggers do inherit their Log-Levels from their Ancestors. They can
|
32
|
* increase their Log-Level compared to their Ancestors, but they cannot decrease it.
|
33
|
*
|
34
|
* <p>The casual user does not have to deal with this class directly.</p>
|
35
|
*
|
36
|
* <p>The structure of the logger hierarchy is maintained by the
|
37
|
* getLogger method. The hierarchy is such that children link
|
38
|
* to their parent but parents do not have any pointers to their
|
39
|
* children. Moreover, loggers can be instantiated in any order, in
|
40
|
* particular descendant before ancestor.</p>
|
41
|
*
|
42
|
* <p>In case a descendant is created before a particular ancestor,
|
43
|
* then it creates a provision node for the ancestor and adds itself
|
44
|
* to the provision node. Other descendants of the same ancestor add
|
45
|
* themselves to the previously created provision node.</p>
|
46
|
*
|
47
|
* @version $Revision: 1163124 $
|
48
|
* @package log4php
|
49
|
*/
|
50
|
class LoggerHierarchy {
|
51
|
|
52
|
/** Array holding all Logger instances. */
|
53
|
protected $loggers = array();
|
54
|
|
55
|
/**
|
56
|
* The root logger.
|
57
|
* @var RootLogger
|
58
|
*/
|
59
|
protected $root = null;
|
60
|
|
61
|
/**
|
62
|
* The logger renderer map.
|
63
|
* @var LoggerRendererMap
|
64
|
*/
|
65
|
protected $rendererMap;
|
66
|
|
67
|
/**
|
68
|
* Main level threshold. Events with lower level will not be logged by any
|
69
|
* logger, regardless of it's configuration.
|
70
|
* @var LoggerLevel
|
71
|
*/
|
72
|
protected $threshold;
|
73
|
|
74
|
/**
|
75
|
* Creates a new logger hierarchy.
|
76
|
* @param LoggerRoot $root The root logger.
|
77
|
*/
|
78
|
public function __construct(LoggerRoot $root) {
|
79
|
$this->root = $root;
|
80
|
$this->setThreshold(LoggerLevel::getLevelAll());
|
81
|
$this->rendererMap = new LoggerRendererMap();
|
82
|
}
|
83
|
|
84
|
/**
|
85
|
* Clears all loggers.
|
86
|
*/
|
87
|
public function clear() {
|
88
|
$this->loggers = array();
|
89
|
}
|
90
|
|
91
|
/**
|
92
|
* Check if the named logger exists in the hierarchy.
|
93
|
* @param string $name
|
94
|
* @return boolean
|
95
|
*/
|
96
|
public function exists($name) {
|
97
|
return isset($this->loggers[$name]);
|
98
|
}
|
99
|
|
100
|
/**
|
101
|
* Returns all the currently defined loggers in this hierarchy as an array.
|
102
|
* @return array
|
103
|
*/
|
104
|
public function getCurrentLoggers() {
|
105
|
return array_values($this->loggers);
|
106
|
}
|
107
|
|
108
|
/**
|
109
|
* Returns a named logger instance logger. If it doesn't exist, one is created.
|
110
|
*
|
111
|
* @param string $name Logger name
|
112
|
* @return Logger Logger instance.
|
113
|
*/
|
114
|
public function getLogger($name) {
|
115
|
if(!isset($this->loggers[$name])) {
|
116
|
$logger = new Logger($name);
|
117
|
|
118
|
$nodes = explode('.', $name);
|
119
|
$firstNode = array_shift($nodes);
|
120
|
|
121
|
// if name is not a first node but another first node is their
|
122
|
if($firstNode != $name and isset($this->loggers[$firstNode])) {
|
123
|
$logger->setParent($this->loggers[$firstNode]);
|
124
|
} else {
|
125
|
// if there is no father, set root logger as father
|
126
|
$logger->setParent($this->root);
|
127
|
}
|
128
|
|
129
|
// if there are more nodes than one
|
130
|
if(count($nodes) > 0) {
|
131
|
// find parent node
|
132
|
foreach($nodes as $node) {
|
133
|
$parentNode = "$firstNode.$node";
|
134
|
if(isset($this->loggers[$parentNode]) and $parentNode != $name) {
|
135
|
$logger->setParent($this->loggers[$parentNode]);
|
136
|
}
|
137
|
$firstNode .= ".$node";
|
138
|
}
|
139
|
}
|
140
|
|
141
|
$this->loggers[$name] = $logger;
|
142
|
}
|
143
|
|
144
|
return $this->loggers[$name];
|
145
|
}
|
146
|
|
147
|
/**
|
148
|
* Returns the logger renderer map.
|
149
|
* @return LoggerRendererMap
|
150
|
*/
|
151
|
public function getRendererMap() {
|
152
|
return $this->rendererMap;
|
153
|
}
|
154
|
|
155
|
/**
|
156
|
* Returns the root logger.
|
157
|
* @return LoggerRoot
|
158
|
*/
|
159
|
public function getRootLogger() {
|
160
|
if(!isset($this->root) or $this->root == null) {
|
161
|
$this->root = new LoggerRoot();
|
162
|
}
|
163
|
return $this->root;
|
164
|
}
|
165
|
|
166
|
/**
|
167
|
* Returns the main threshold level.
|
168
|
* @return LoggerLevel
|
169
|
*/
|
170
|
public function getThreshold() {
|
171
|
return $this->threshold;
|
172
|
}
|
173
|
|
174
|
/**
|
175
|
* Returns true if the hierarchy is disabled for given log level and false
|
176
|
* otherwise.
|
177
|
* @return boolean
|
178
|
*/
|
179
|
public function isDisabled(LoggerLevel $level) {
|
180
|
return ($this->threshold->toInt() > $level->toInt());
|
181
|
}
|
182
|
|
183
|
/**
|
184
|
* Reset all values contained in this hierarchy instance to their
|
185
|
* default.
|
186
|
*
|
187
|
* This removes all appenders from all loggers, sets
|
188
|
* the level of all non-root loggers to <i>null</i>,
|
189
|
* sets their additivity flag to <i>true</i> and sets the level
|
190
|
* of the root logger to {@link LOGGER_LEVEL_DEBUG}.
|
191
|
*
|
192
|
* <p>Existing loggers are not removed. They are just reset.
|
193
|
*
|
194
|
* <p>This method should be used sparingly and with care as it will
|
195
|
* block all logging until it is completed.</p>
|
196
|
*/
|
197
|
public function resetConfiguration() {
|
198
|
$root = $this->getRootLogger();
|
199
|
|
200
|
$root->setLevel(LoggerLevel::getLevelDebug());
|
201
|
$this->setThreshold(LoggerLevel::getLevelAll());
|
202
|
$this->shutDown();
|
203
|
|
204
|
foreach($this->loggers as $logger) {
|
205
|
$logger->setLevel(null);
|
206
|
$logger->setAdditivity(true);
|
207
|
$logger->removeAllAppenders();
|
208
|
}
|
209
|
|
210
|
$this->rendererMap->clear();
|
211
|
LoggerAppenderPool::clear();
|
212
|
}
|
213
|
|
214
|
/**
|
215
|
* Sets the main threshold level.
|
216
|
* @param LoggerLevel $l
|
217
|
*/
|
218
|
public function setThreshold(LoggerLevel $threshold) {
|
219
|
$this->threshold = $threshold;
|
220
|
}
|
221
|
|
222
|
/**
|
223
|
* Shutting down a hierarchy will <i>safely</i> close and remove
|
224
|
* all appenders in all loggers including the root logger.
|
225
|
*
|
226
|
* The shutdown method is careful to close nested
|
227
|
* appenders before closing regular appenders. This is allows
|
228
|
* configurations where a regular appender is attached to a logger
|
229
|
* and again to a nested appender.
|
230
|
*
|
231
|
* @todo Check if the last paragraph is correct.
|
232
|
*/
|
233
|
public function shutdown() {
|
234
|
$this->root->removeAllAppenders();
|
235
|
|
236
|
foreach($this->loggers as $logger) {
|
237
|
$logger->removeAllAppenders();
|
238
|
}
|
239
|
}
|
240
|
}
|