LoggerConfiguratorDefault.php
15 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
*/
/**
* Default implementation of the logger configurator.
*
* Configures log4php based on a provided configuration file or array.
*
* @package log4php
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
* @version $Revision: 1394956 $
* @since 2.2
*/
class LoggerConfiguratorDefault implements LoggerConfigurator
{
/** XML configuration file format. */
const FORMAT_XML = 'xml';
/** PHP configuration file format. */
const FORMAT_PHP = 'php';
/** INI (properties) configuration file format. */
const FORMAT_INI = 'ini';
/** Defines which adapter should be used for parsing which format. */
private $adapters = array(
self::FORMAT_XML => 'LoggerConfigurationAdapterXML',
self::FORMAT_INI => 'LoggerConfigurationAdapterINI',
self::FORMAT_PHP => 'LoggerConfigurationAdapterPHP',
);
/** Default configuration; used if no configuration file is provided. */
private static $defaultConfiguration = array(
'threshold' => 'ALL',
'rootLogger' => array(
'level' => 'DEBUG',
'appenders' => array('default'),
),
'appenders' => array(
'default' => array(
'class' => 'LoggerAppenderEcho'
),
),
);
/** Holds the appenders before they are linked to loggers. */
private $appenders = array();
/**
* Configures log4php based on the given configuration. The input can
* either be a path to the config file, or a PHP array holding the
* configuration.
*
* If no configuration is given, or if the given configuration cannot be
* parsed for whatever reason, a warning will be issued, and log4php
* will use the default configuration contained in
* {@link $defaultConfiguration}.
*
* @param LoggerHierarchy $hierarchy The hierarchy on which to perform
* the configuration.
* @param string|array $input Either path to the config file or the
* configuration as an array. If not set, default configuration
* will be used.
*/
public function configure(LoggerHierarchy $hierarchy, $input = null) {
$config = $this->parse($input);
$this->doConfigure($hierarchy, $config);
}
/**
* Parses the given configuration and returns the parsed configuration
* as a PHP array. Does not perform any configuration.
*
* If no configuration is given, or if the given configuration cannot be
* parsed for whatever reason, a warning will be issued, and the default
* configuration will be returned ({@link $defaultConfiguration}).
*
* @param string|array $input Either path to the config file or the
* configuration as an array. If not set, default configuration
* will be used.
* @return array The parsed configuration.
*/
public function parse($input) {
// No input - use default configuration
if (!isset($input)) {
$config = self::$defaultConfiguration;
}
// Array input - contains configuration within the array
else if (is_array($input)) {
$config = $input;
}
// String input - contains path to configuration file
else if (is_string($input)) {
try {
$config = $this->parseFile($input);
} catch (LoggerException $e) {
$this->warn("Configuration failed. " . $e->getMessage() . " Using default configuration.");
$config = self::$defaultConfiguration;
}
}
// Anything else is an error
else {
$this->warn("Invalid configuration param given. Reverting to default configuration.");
$config = self::$defaultConfiguration;
}
return $config;
}
/**
* Returns the default log4php configuration.
* @return array
*/
public static function getDefaultConfiguration() {
return self::$defaultConfiguration;
}
/**
* Loads the configuration file from the given URL, determines which
* adapter to use, converts the configuration to a PHP array and
* returns it.
*
* @param string $url Path to the config file.
* @return The configuration from the config file, as a PHP array.
* @throws LoggerException If the configuration file cannot be loaded, or
* if the parsing fails.
*/
private function parseFile($url) {
if (!file_exists($url)) {
throw new LoggerException("File not found at [$url].");
}
$type = $this->getConfigType($url);
$adapterClass = $this->adapters[$type];
$adapter = new $adapterClass();
return $adapter->convert($url);
}
/** Determines configuration file type based on the file extension. */
private function getConfigType($url) {
$info = pathinfo($url);
$ext = strtolower($info['extension']);
switch($ext) {
case 'xml':
return self::FORMAT_XML;
case 'ini':
case 'properties':
return self::FORMAT_INI;
case 'php':
return self::FORMAT_PHP;
default:
throw new LoggerException("Unsupported configuration file extension: $ext");
}
}
/**
* Constructs the logger hierarchy based on configuration.
*
* @param LoggerHierarchy $hierarchy
* @param array $config
*/
private function doConfigure(LoggerHierarchy $hierarchy, $config) {
if (isset($config['threshold'])) {
$threshold = LoggerLevel::toLevel($config['threshold']);
if (isset($threshold)) {
$hierarchy->setThreshold($threshold);
} else {
$this->warn("Invalid threshold value [{$config['threshold']}] specified. Ignoring threshold definition.");
}
}
// Configure appenders and add them to the appender pool
if (isset($config['appenders']) && is_array($config['appenders'])) {
foreach($config['appenders'] as $name => $appenderConfig) {
$this->configureAppender($name, $appenderConfig);
}
}
// Configure root logger
if (isset($config['rootLogger'])) {
$this->configureRootLogger($hierarchy, $config['rootLogger']);
}
// Configure loggers
if (isset($config['loggers']) && is_array($config['loggers'])) {
foreach($config['loggers'] as $loggerName => $loggerConfig) {
$this->configureOtherLogger($hierarchy, $loggerName, $loggerConfig);
}
}
// Configure renderers
if (isset($config['renderers']) && is_array($config['renderers'])) {
foreach($config['renderers'] as $rendererConfig) {
$this->configureRenderer($hierarchy, $rendererConfig);
}
}
if (isset($config['defaultRenderer'])) {
$this->configureDefaultRenderer($hierarchy, $config['defaultRenderer']);
}
}
private function configureRenderer(LoggerHierarchy $hierarchy, $config) {
if (empty($config['renderingClass'])) {
$this->warn("Rendering class not specified. Skipping renderer definition.");
return;
}
if (empty($config['renderedClass'])) {
$this->warn("Rendered class not specified. Skipping renderer definition.");
return;
}
// Error handling performed by RendererMap
$hierarchy->getRendererMap()->addRenderer($config['renderedClass'], $config['renderingClass']);
}
private function configureDefaultRenderer(LoggerHierarchy $hierarchy, $class) {
if (empty($class)) {
$this->warn("Rendering class not specified. Skipping default renderer definition.");
return;
}
// Error handling performed by RendererMap
$hierarchy->getRendererMap()->setDefaultRenderer($class);
}
/**
* Configures an appender based on given config and saves it to
* {@link $appenders} array so it can be later linked to loggers.
* @param string $name Appender name.
* @param array $config Appender configuration options.
*/
private function configureAppender($name, $config) {
// TODO: add this check to other places where it might be useful
if (!is_array($config)) {
$type = gettype($config);
$this->warn("Invalid configuration provided for appender [$name]. Expected an array, found <$type>. Skipping appender definition.");
return;
}
// Parse appender class
$class = $config['class'];
if (empty($class)) {
$this->warn("No class given for appender [$name]. Skipping appender definition.");
return;
}
if (!class_exists($class)) {
$this->warn("Invalid class [$class] given for appender [$name]. Class does not exist. Skipping appender definition.");
return;
}
// Instantiate the appender
$appender = new $class($name);
if (!($appender instanceof LoggerAppender)) {
$this->warn("Invalid class [$class] given for appender [$name]. Not a valid LoggerAppender class. Skipping appender definition.");
return;
}
// Parse the appender threshold
if (isset($config['threshold'])) {
$threshold = LoggerLevel::toLevel($config['threshold']);
if ($threshold instanceof LoggerLevel) {
$appender->setThreshold($threshold);
} else {
$this->warn("Invalid threshold value [{$config['threshold']}] specified for appender [$name]. Ignoring threshold definition.");
}
}
// Parse the appender layout
if ($appender->requiresLayout() && isset($config['layout'])) {
$this->createAppenderLayout($appender, $config['layout']);
}
// Parse filters
if (isset($config['filters']) && is_array($config['filters'])) {
foreach($config['filters'] as $filterConfig) {
$this->createAppenderFilter($appender, $filterConfig);
}
}
// Set options if any
if (isset($config['params'])) {
$this->setOptions($appender, $config['params']);
}
// Activate and save for later linking to loggers
$appender->activateOptions();
$this->appenders[$name] = $appender;
}
/**
* Parses layout config, creates the layout and links it to the appender.
* @param LoggerAppender $appender
* @param array $config Layout configuration.
*/
private function createAppenderLayout(LoggerAppender $appender, $config) {
$name = $appender->getName();
$class = $config['class'];
if (empty($class)) {
$this->warn("Layout class not specified for appender [$name]. Reverting to default layout.");
return;
}
if (!class_exists($class)) {
$this->warn("Nonexistant layout class [$class] specified for appender [$name]. Reverting to default layout.");
return;
}
$layout = new $class();
if (!($layout instanceof LoggerLayout)) {
$this->warn("Invalid layout class [$class] sepcified for appender [$name]. Reverting to default layout.");
return;
}
if (isset($config['params'])) {
$this->setOptions($layout, $config['params']);
}
$layout->activateOptions();
$appender->setLayout($layout);
}
/**
* Parses filter config, creates the filter and adds it to the appender's
* filter chain.
* @param LoggerAppender $appender
* @param array $config Filter configuration.
*/
private function createAppenderFilter(LoggerAppender $appender, $config) {
$name = $appender->getName();
$class = $config['class'];
if (!class_exists($class)) {
$this->warn("Nonexistant filter class [$class] specified on appender [$name]. Skipping filter definition.");
return;
}
$filter = new $class();
if (!($filter instanceof LoggerFilter)) {
$this->warn("Invalid filter class [$class] sepcified on appender [$name]. Skipping filter definition.");
return;
}
if (isset($config['params'])) {
$this->setOptions($filter, $config['params']);
}
$filter->activateOptions();
$appender->addFilter($filter);
}
/**
* Configures the root logger
* @see configureLogger()
*/
private function configureRootLogger(LoggerHierarchy $hierarchy, $config) {
$logger = $hierarchy->getRootLogger();
$this->configureLogger($logger, $config);
}
/**
* Configures a logger which is not root.
* @see configureLogger()
*/
private function configureOtherLogger(LoggerHierarchy $hierarchy, $name, $config) {
// Get logger from hierarchy (this creates it if it doesn't already exist)
$logger = $hierarchy->getLogger($name);
$this->configureLogger($logger, $config);
}
/**
* Configures a logger.
*
* @param Logger $logger The logger to configure
* @param array $config Logger configuration options.
*/
private function configureLogger(Logger $logger, $config) {
$loggerName = $logger->getName();
// Set logger level
if (isset($config['level'])) {
$level = LoggerLevel::toLevel($config['level']);
if (isset($level)) {
$logger->setLevel($level);
} else {
$this->warn("Invalid level value [{$config['level']}] specified for logger [$loggerName]. Ignoring level definition.");
}
}
// Link appenders to logger
if (isset($config['appenders'])) {
foreach($config['appenders'] as $appenderName) {
if (isset($this->appenders[$appenderName])) {
$logger->addAppender($this->appenders[$appenderName]);
} else {
$this->warn("Nonexistnant appender [$appenderName] linked to logger [$loggerName].");
}
}
}
// Set logger additivity
if (isset($config['additivity'])) {
try {
$additivity = LoggerOptionConverter::toBooleanEx($config['additivity'], null);
$logger->setAdditivity($additivity);
} catch (Exception $ex) {
$this->warn("Invalid additivity value [{$config['additivity']}] specified for logger [$loggerName]. Ignoring additivity setting.");
}
}
}
/**
* Helper method which applies given options to an object which has setters
* for these options (such as appenders, layouts, etc.).
*
* For example, if options are:
* <code>
* array(
* 'file' => '/tmp/myfile.log',
* 'append' => true
* )
* </code>
*
* This method will call:
* <code>
* $object->setFile('/tmp/myfile.log')
* $object->setAppend(true)
* </code>
*
* If required setters do not exist, it will produce a warning.
*
* @param mixed $object The object to configure.
* @param unknown_type $options
*/
private function setOptions($object, $options) {
foreach($options as $name => $value) {
$setter = "set$name";
if (method_exists($object, $setter)) {
$object->$setter($value);
} else {
$class = get_class($object);
$this->warn("Nonexistant option [$name] specified on [$class]. Skipping.");
}
}
}
/** Helper method to simplify error reporting. */
private function warn($message) {
trigger_error("log4php: $message", E_USER_WARNING);
}
}