LoggerConfiguratorXml.php
18 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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
<?php
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
* @package log4php
* @subpackage configurators
*/
define('LOG4PHP_LOGGER_DOM_CONFIGURATOR_APPENDER_STATE', 1000);
define('LOG4PHP_LOGGER_DOM_CONFIGURATOR_LAYOUT_STATE', 1010);
define('LOG4PHP_LOGGER_DOM_CONFIGURATOR_ROOT_STATE', 1020);
define('LOG4PHP_LOGGER_DOM_CONFIGURATOR_LOGGER_STATE', 1030);
define('LOG4PHP_LOGGER_DOM_CONFIGURATOR_FILTER_STATE', 1040);
define('LOG4PHP_LOGGER_DOM_CONFIGURATOR_DEFAULT_FILENAME', './log4php.xml');
/**
* @var string the default configuration document
*/
define('LOG4PHP_LOGGER_DOM_CONFIGURATOR_DEFAULT_CONFIGURATION',
'<?xml version="1.0" ?>
<log4php:configuration threshold="all">
<appender name="A1" class="LoggerAppenderEcho">
<layout class="LoggerLayoutSimple" />
</appender>
<root>
<level value="debug" />
<appender_ref ref="A1" />
</root>
</log4php:configuration>');
/**
* @var string the elements namespace
*/
define('LOG4PHP_LOGGER_DOM_CONFIGURATOR_XMLNS', 'HTTP://LOGGING.APACHE.ORG/LOG4PHP/');
/**
* Use this class to initialize the log4php environment using expat parser.
*
* <p>Read the log4php.dtd included in the documentation directory. Note that
* php parser does not validate the document.</p>
*
* <p>Sometimes it is useful to see how log4php is reading configuration
* files. You can enable log4php internal logging by setting the <var>debug</var>
* attribute in the <var>log4php:configuration</var> element. As in
* <pre>
* <log4php:configuration <b>debug="true"</b> xmlns:log4php="http://logging.apache.org/log4php/">
* ...
* </log4php:configuration>
* </pre>
*
* <p>There are sample XML files included in the package under <b>tests/</b>
* subdirectories.</p>
*
* @version $Revision: 785337 $
* @package log4php
* @subpackage xml
* @since 0.4
*/
class LoggerConfiguratorXml implements LoggerConfigurator {
/**
* @var LoggerHierarchy
*/
var $repository;
/**
* @var array state stack
*/
var $state;
/**
* @var Logger parsed Logger
*/
var $logger;
/**
* @var LoggerAppender parsed LoggerAppender
*/
var $appender;
/**
* @var LoggerFilter parsed LoggerFilter
*/
var $filter;
/**
* @var LoggerLayout parsed LoggerLayout
*/
var $layout;
/**
* Constructor
*/
public function __construct() {
$this->state = array();
$this->logger = null;
$this->appender = null;
$this->filter = null;
$this->layout = null;
}
/**
* Configure the default repository using the resource pointed by <b>url</b>.
* <b>Url</b> is any valid resource as defined in {@link PHP_MANUAL#file} function.
* Note that the resource will be search with <i>use_include_path</i> parameter
* set to "1".
*
* @param string $url
* @static
*/
public static function configure($url = '') {
$configurator = new self();
$repository = LoggerManager::getLoggerRepository();
return $configurator->doConfigure($url, $repository);
}
/**
* Configure the given <b>repository</b> using the resource pointed by <b>url</b>.
* <b>Url</b> is any valid resurce as defined in {@link PHP_MANUAL#file} function.
* Note that the resource will be search with <i>use_include_path</i> parameter
* set to "1".
*
* @param string $url
* @param LoggerHierarchy &$repository
*/
function doConfigure($url = '', &$repository)
{
$xmlData = '';
if (!empty($url))
$xmlData = implode('', file($url, 1));
return $this->doConfigureByString($xmlData, $repository);
}
/**
* Configure the given <b>repository</b> using the configuration written in <b>xmlData</b>.
* Do not call this method directly. Use {@link doConfigure()} instead.
* @param string $xmlData
* @param LoggerHierarchy &$repository
*/
function doConfigureByString($xmlData, &$repository)
{
return $this->parse($xmlData, $repository);
}
/**
* @param LoggerHierarchy &$repository
*/
function doConfigureDefault(&$repository)
{
return $this->doConfigureByString(LOG4PHP_LOGGER_DOM_CONFIGURATOR_DEFAULT_CONFIGURATION, $repository);
}
/**
* @param string $xmlData
*/
function parse($xmlData, &$repository)
{
// LoggerManager::resetConfiguration();
$this->repository =& $repository;
$parser = xml_parser_create_ns();
xml_set_object($parser, $this);
xml_set_element_handler($parser, "tagOpen", "tagClose");
$result = xml_parse($parser, $xmlData, true);
if (!$result) {
$errorCode = xml_get_error_code($parser);
$errorStr = xml_error_string($errorCode);
$errorLine = xml_get_current_line_number($parser);
$this->repository->resetConfiguration();
} else {
xml_parser_free($parser);
}
return $result;
}
/**
* @param mixed $parser
* @param string $tag
* @param array $attribs
*
* @todo In 'LOGGER' case find a better way to detect 'getLogger()' method
*/
function tagOpen($parser, $tag, $attribs)
{
switch ($tag) {
case 'CONFIGURATION' :
case LOG4PHP_LOGGER_DOM_CONFIGURATOR_XMLNS.':CONFIGURATION':
if (isset($attribs['THRESHOLD'])) {
$this->repository->setThreshold(
LoggerOptionConverter::toLevel(
$this->subst($attribs['THRESHOLD']),
$this->repository->getThreshold()
)
);
}
/*
* TODO: Remove due to LOG4PHP-34
if (isset($attribs['DEBUG'])) {
$debug = LoggerOptionConverter::toBoolean($this->subst($attribs['DEBUG']), LoggerLog::internalDebugging());
$this->repository->debug = $debug;
LoggerLog::internalDebugging($debug);
LoggerLog::debug("LoggerDOMConfigurator::tagOpen() LOG4PHP:CONFIGURATION. Internal Debug turned ".($debug ? 'on':'off'));
}
*/
break;
case 'APPENDER' :
case LOG4PHP_LOGGER_DOM_CONFIGURATOR_XMLNS.':APPENDER':
unset($this->appender);
$this->appender = null;
$name = $this->subst(@$attribs['NAME']);
$class = $this->subst(@$attribs['CLASS']);
$this->appender = LoggerAppender::singleton($name, $class);
$this->state[] = LOG4PHP_LOGGER_DOM_CONFIGURATOR_APPENDER_STATE;
break;
case 'APPENDER_REF' :
case 'APPENDER-REF' :
case LOG4PHP_LOGGER_DOM_CONFIGURATOR_XMLNS.':APPENDER_REF':
case LOG4PHP_LOGGER_DOM_CONFIGURATOR_XMLNS.':APPENDER-REF':
if (isset($attribs['REF']) and !empty($attribs['REF'])) {
$appenderName = $this->subst($attribs['REF']);
$appender = LoggerAppender::singleton($appenderName);
if ($appender !== null) {
switch (end($this->state)) {
case LOG4PHP_LOGGER_DOM_CONFIGURATOR_LOGGER_STATE:
case LOG4PHP_LOGGER_DOM_CONFIGURATOR_ROOT_STATE:
$this->logger->addAppender($appender);
break;
}
}
}
break;
case 'FILTER' :
case LOG4PHP_LOGGER_DOM_CONFIGURATOR_XMLNS.':FILTER':
unset($this->filter);
$this->filter = null;
$filterName = basename($this->subst(@$attribs['CLASS']));
if (!empty($filterName)) {
$this->filter = new $filterName();
$this->state[] = LOG4PHP_LOGGER_DOM_CONFIGURATOR_FILTER_STATE;
}
break;
case 'LAYOUT':
case LOG4PHP_LOGGER_DOM_CONFIGURATOR_XMLNS.':LAYOUT':
$class = @$attribs['CLASS'];
$this->layout = LoggerReflectionUtils::createObject($this->subst($class));
$this->state[] = LOG4PHP_LOGGER_DOM_CONFIGURATOR_LAYOUT_STATE;
break;
case 'LOGGER':
case LOG4PHP_LOGGER_DOM_CONFIGURATOR_XMLNS.':LOGGER':
// $this->logger is assigned by reference.
// Only '$this->logger=null;' destroys referenced object
unset($this->logger);
$this->logger = null;
$loggerName = $this->subst(@$attribs['NAME']);
if (!empty($loggerName)) {
$class = $this->subst(@$attribs['CLASS']);
if (empty($class)) {
$this->logger =& $this->repository->getLogger($loggerName);
} else {
$className = basename($class);
if (!class_exists($className)) {
// TODO throw exception?
/*LoggerLog::warn(
"LoggerDOMConfigurator::tagOpen() LOGGER. ".
"cannot find '$className'."
);*/
} else {
if (in_array('getlogger', get_class_methods($className))) {
$this->logger =& call_user_func(array($className, 'getlogger'), $loggerName);
} else {
// TODO throw exception?
/*
LoggerLog::warn(
"LoggerDOMConfigurator::tagOpen() LOGGER. ".
"class '$className' doesnt implement 'getLogger()' method."
);
*/
}
}
}
if ($this->logger !== null and isset($attribs['ADDITIVITY'])) {
$additivity = LoggerOptionConverter::toBoolean($this->subst($attribs['ADDITIVITY']), true);
$this->logger->setAdditivity($additivity);
}
}
$this->state[] = LOG4PHP_LOGGER_DOM_CONFIGURATOR_LOGGER_STATE;;
break;
case 'LEVEL':
case LOG4PHP_LOGGER_DOM_CONFIGURATOR_XMLNS.':LEVEL':
case 'PRIORITY':
case LOG4PHP_LOGGER_DOM_CONFIGURATOR_XMLNS.':PRIORITY':
if (!isset($attribs['VALUE'])) {
// LoggerDOMConfigurator::tagOpen() LEVEL value not set
break;
}
if ($this->logger === null) {
// LoggerDOMConfigurator::tagOpen() LEVEL. parent logger is null
break;
}
switch (end($this->state)) {
case LOG4PHP_LOGGER_DOM_CONFIGURATOR_ROOT_STATE:
$this->logger->setLevel(
LoggerOptionConverter::toLevel(
$this->subst($attribs['VALUE']),
$this->logger->getLevel()
)
);
break;
case LOG4PHP_LOGGER_DOM_CONFIGURATOR_LOGGER_STATE:
$this->logger->setLevel(
LoggerOptionConverter::toLevel(
$this->subst($attribs['VALUE']),
$this->logger->getLevel()
)
);
break;
default:
//LoggerLog::warn("LoggerDOMConfigurator::tagOpen() LEVEL state '{$this->state}' not allowed here");
}
break;
case 'PARAM':
case LOG4PHP_LOGGER_DOM_CONFIGURATOR_XMLNS.':PARAM':
if (!isset($attribs['NAME'])) {
// LoggerDOMConfigurator::tagOpen() PARAM attribute 'name' not defined.
break;
}
if (!isset($attribs['VALUE'])) {
// LoggerDOMConfigurator::tagOpen() PARAM. attribute 'value' not defined.
break;
}
switch (end($this->state)) {
case LOG4PHP_LOGGER_DOM_CONFIGURATOR_APPENDER_STATE:
if ($this->appender !== null) {
LoggerReflectionUtils::setter($this->appender, $this->subst($attribs['NAME']), $this->subst($attribs['VALUE']));
}
break;
case LOG4PHP_LOGGER_DOM_CONFIGURATOR_LAYOUT_STATE:
if ($this->layout !== null) {
LoggerReflectionUtils::setter($this->layout, $this->subst($attribs['NAME']), $this->subst($attribs['VALUE']));
}
break;
case LOG4PHP_LOGGER_DOM_CONFIGURATOR_FILTER_STATE:
if ($this->filter !== null) {
LoggerReflectionUtils::setter($this->filter, $this->subst($attribs['NAME']), $this->subst($attribs['VALUE']));
}
break;
default:
//LoggerLog::warn("LoggerDOMConfigurator::tagOpen() PARAM state '{$this->state}' not allowed here");
}
break;
case 'RENDERER':
case LOG4PHP_LOGGER_DOM_CONFIGURATOR_XMLNS.':RENDERER':
$renderedClass = $this->subst(@$attribs['RENDEREDCLASS']);
$renderingClass = $this->subst(@$attribs['RENDERINGCLASS']);
if (!empty($renderedClass) and !empty($renderingClass)) {
$renderer = LoggerReflectionUtils::createObject($renderingClass);
if ($renderer === null) {
// LoggerDOMConfigurator::tagOpen() RENDERER cannot instantiate '$renderingClass'
} else {
$this->repository->setRenderer($renderedClass, $renderer);
}
}
break;
case 'ROOT':
case LOG4PHP_LOGGER_DOM_CONFIGURATOR_XMLNS.':ROOT':
$this->logger = LoggerManager::getRootLogger();
$this->state[] = LOG4PHP_LOGGER_DOM_CONFIGURATOR_ROOT_STATE;
break;
}
}
/**
* @param mixed $parser
* @param string $tag
*/
function tagClose($parser, $tag)
{
switch ($tag) {
case 'CONFIGURATION' :
case LOG4PHP_LOGGER_DOM_CONFIGURATOR_XMLNS.':CONFIGURATION':
break;
case 'APPENDER' :
case LOG4PHP_LOGGER_DOM_CONFIGURATOR_XMLNS.':APPENDER':
if ($this->appender !== null) {
if ($this->appender->requiresLayout() and $this->appender->getLayout() === null) {
$appenderName = $this->appender->getName();
$this->appender->setLayout(LoggerReflectionUtils::createObject('LoggerLayoutSimple'));
}
$this->appender->activateOptions();
}
array_pop($this->state);
break;
case 'FILTER' :
case LOG4PHP_LOGGER_DOM_CONFIGURATOR_XMLNS.':FILTER':
if ($this->filter !== null) {
$this->filter->activateOptions();
$this->appender->addFilter($this->filter);
$this->filter = null;
}
array_pop($this->state);
break;
case 'LAYOUT':
case LOG4PHP_LOGGER_DOM_CONFIGURATOR_XMLNS.':LAYOUT':
if ($this->appender !== null and $this->layout !== null and $this->appender->requiresLayout()) {
$this->layout->activateOptions();
$this->appender->setLayout($this->layout);
$this->layout = null;
}
array_pop($this->state);
break;
case 'LOGGER':
case LOG4PHP_LOGGER_DOM_CONFIGURATOR_XMLNS.':LOGGER':
array_pop($this->state);
break;
case 'ROOT':
case LOG4PHP_LOGGER_DOM_CONFIGURATOR_XMLNS.':ROOT':
array_pop($this->state);
break;
}
}
function subst($value)
{
return LoggerOptionConverter::substVars($value);
}
}