-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLogConfiguration.class.php
More file actions
executable file
·130 lines (119 loc) · 3.49 KB
/
LogConfiguration.class.php
File metadata and controls
executable file
·130 lines (119 loc) · 3.49 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
<?php namespace util\log;
use lang\FormatException;
use lang\IllegalArgumentException;
use lang\Throwable;
use lang\XPClass;
use util\PropertyAccess;
/**
* Log configuration from a properties object
*
* ```ini
* [default]
* uses=console|syslog|files
*
* [console]
* class=util.log.ConsoleAppender
* level=ALL
*
* [files]
* class=util.log.FileAppender
* args="/var/log/server.log"
* level=ALL
*
* [syslog]
* class=util.log.SyslogUdpAppender
* args=127.0.0.1|514|server
* level=WARN|ERROR
* ```
*
* @test xp://util.log.unittest.LogConfigurationTest
*/
class LogConfiguration {
private $categories= [];
/**
* Creates a new log configuration from a properties file
*
* @param util.PropertyAccess $properties
* @throws lang.FormatException if the property file contains errors
*/
public function __construct(PropertyAccess $properties) {
foreach ($properties->sections() as $section) {
$cat= new LogCategory($section);
foreach ($this->appendersFor($properties, $section) as $level => $appender) {
$cat->addAppender($appender, $level);
}
$this->categories[$section]= $cat;
}
}
/**
* Returns log appenders for a given property file section
*
* @param util.PropertyAccess $properties
* @param string $section
* @return iterable
* @throws lang.FormatException
*/
private function appendersFor($properties, $section) {
static $names= [
'INFO' => LogLevel::INFO,
'WARN' => LogLevel::WARN,
'ERROR' => LogLevel::ERROR,
'DEBUG' => LogLevel::DEBUG,
'ALL' => LogLevel::ALL,
'NONE' => LogLevel::NONE,
];
// Class
if ($class= $properties->readString($section, 'class', null)) {
try {
$appender= XPClass::forName($class)->newInstance(...$properties->readArray($section, 'args', []));
} catch (Throwable $e) {
throw new FormatException('Class '.$class.' in section "'.$section.'" cannot be instantiated', $e);
}
if ($levels= $properties->readArray($section, 'level', null)) {
$level= LogLevel::NONE;
foreach ($levels as $name) {
if (!isset($names[$name])) {
throw new FormatException('Level '.$name.' in section "'.$section.'" not recognized');
}
$level |= $names[$name];
}
yield $level => $appender;
} else {
yield LogLevel::ALL => $appender;
}
}
// Uses, referencing other section
if ($uses= $properties->readArray($section, 'uses', null)) {
foreach ($uses as $use) {
if (!$properties->hasSection($use)) {
throw new FormatException('Uses in section "'.$section.'" references non-existant section "'.$use.'"');
}
foreach ($this->appendersFor($properties, $use) as $level => $appender) {
yield $level => $appender;
}
}
}
}
/** @return [:util.log.LogCategory] */
public function categories() { return $this->categories; }
/**
* Test whether this configuration provides a log category by its name
*
* @param string $name
* @return bool
*/
public function provides($name) {
return isset($this->categories[$name]);
}
/**
* Return a log category by its name
*
* @param string $name
* @return util.log.LogCategory
* @throws lang.IllegalArgumentException
*/
public function category($name) {
if (isset($this->categories[$name])) return $this->categories[$name];
throw new IllegalArgumentException('No log category "'.$name.'"');
}
}