1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace DarkMatter\Components\Logger; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* This is a simple Logger implementation that other Loggers can inherit from. |
9
|
|
|
* |
10
|
|
|
* It simply delegates all log-level-specific methods to the `log` method to |
11
|
|
|
* reduce boilerplate code that a simple Logger that does the same thing with |
12
|
|
|
* messages regardless of the error level has to implement. |
13
|
|
|
*/ |
14
|
|
|
abstract class AbstractLogger implements LoggerInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var array $levels |
18
|
|
|
*/ |
19
|
|
|
protected $levels = [ |
20
|
|
|
0 => LogLevel::DEBUG, |
21
|
|
|
1 => LogLevel::INFO, |
22
|
|
|
2 => LogLevel::NOTICE, |
23
|
|
|
3 => LogLevel::WARNING, |
24
|
|
|
4 => LogLevel::ERROR, |
25
|
|
|
5 => LogLevel::CRITICAL, |
26
|
|
|
6 => LogLevel::ALERT, |
27
|
|
|
7 => LogLevel::EMERGENCY, |
28
|
|
|
]; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var int $minLevel |
32
|
|
|
*/ |
33
|
|
|
protected $minLevel = 0; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Returns log levels with numeric index. |
37
|
|
|
* |
38
|
|
|
* @return array |
39
|
|
|
*/ |
40
|
|
|
public function getLevels(): array |
41
|
|
|
{ |
42
|
|
|
return $this->levels; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Sets min. log-level. |
47
|
|
|
* |
48
|
|
|
* @param string $level |
49
|
|
|
*/ |
50
|
|
|
public function setMinLevel(string $level): void |
51
|
|
|
{ |
52
|
|
|
if ($this->levelIsValid($level) === false) { |
53
|
|
|
throw new \InvalidArgumentException('Invalid log-level provided.'); |
54
|
|
|
} |
55
|
|
|
$this->minLevel = $this->getLevelCode($level); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Returns min. log-level. |
60
|
|
|
* |
61
|
|
|
* @return string |
62
|
|
|
*/ |
63
|
|
|
public function getMinLevel(): string |
64
|
|
|
{ |
65
|
|
|
return $this->levels[$this->minLevel]; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Checks if log level is handled by logger. |
70
|
|
|
* |
71
|
|
|
* @param string $level |
72
|
|
|
* @return bool |
73
|
|
|
*/ |
74
|
|
|
public function isHandling(string $level): bool |
75
|
|
|
{ |
76
|
|
|
$levelCode = $this->getLevelCode($level); |
77
|
|
|
return $levelCode >= $this->minLevel; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Returns numeric level code for given log-level. |
82
|
|
|
* |
83
|
|
|
* @param string $level |
84
|
|
|
* @return int |
85
|
|
|
*/ |
86
|
|
|
public function getLevelCode(string $level): int |
87
|
|
|
{ |
88
|
|
|
if ($this->levelIsValid($level) === false) { |
89
|
|
|
// echo __METHOD__; |
90
|
|
|
throw new \InvalidArgumentException('Invalid log-level provided.'); |
91
|
|
|
} |
92
|
|
|
//print_r(array_search($level, $this->levels)); |
93
|
|
|
return array_search($level, $this->levels); |
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Checks if log-level is valid. |
98
|
|
|
* |
99
|
|
|
* @param string $level |
100
|
|
|
* @return bool |
101
|
|
|
*/ |
102
|
|
|
public function levelIsValid(string $level): bool |
103
|
|
|
{ |
104
|
|
|
return in_array($level, $this->levels); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* System is unusable. |
109
|
|
|
* |
110
|
|
|
* @param string $message |
111
|
|
|
* @param array $context |
112
|
|
|
* |
113
|
|
|
* @return void |
114
|
|
|
*/ |
115
|
|
|
public function emergency(string $message, array $context = []): void |
116
|
|
|
{ |
117
|
|
|
$this->log(LogLevel::EMERGENCY, $message, $context); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Action must be taken immediately. |
122
|
|
|
* |
123
|
|
|
* Example: Entire website down, database unavailable, etc. This should |
124
|
|
|
* trigger the SMS alerts and wake you up. |
125
|
|
|
* |
126
|
|
|
* @param string $message |
127
|
|
|
* @param array $context |
128
|
|
|
* |
129
|
|
|
* @return void |
130
|
|
|
*/ |
131
|
|
|
public function alert(string $message, array $context = []): void |
132
|
|
|
{ |
133
|
|
|
$this->log(LogLevel::ALERT, $message, $context); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Critical conditions. |
138
|
|
|
* |
139
|
|
|
* Example: Application component unavailable, unexpected exception. |
140
|
|
|
* |
141
|
|
|
* @param string $message |
142
|
|
|
* @param array $context |
143
|
|
|
* |
144
|
|
|
* @return void |
145
|
|
|
*/ |
146
|
|
|
public function critical(string $message, array $context = []): void |
147
|
|
|
{ |
148
|
|
|
$this->log(LogLevel::CRITICAL, $message, $context); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Runtime errors that do not require immediate action but should typically |
153
|
|
|
* be logged and monitored. |
154
|
|
|
* |
155
|
|
|
* @param string $message |
156
|
|
|
* @param array $context |
157
|
|
|
* |
158
|
|
|
* @return void |
159
|
|
|
*/ |
160
|
|
|
public function error(string $message, array $context = []): void |
161
|
|
|
{ |
162
|
|
|
$this->log(LogLevel::ERROR, $message, $context); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Exceptional occurrences that are not errors. |
167
|
|
|
* |
168
|
|
|
* Example: Use of deprecated APIs, poor use of an API, undesirable things |
169
|
|
|
* that are not necessarily wrong. |
170
|
|
|
* |
171
|
|
|
* @param string $message |
172
|
|
|
* @param array $context |
173
|
|
|
* |
174
|
|
|
* @return void |
175
|
|
|
*/ |
176
|
|
|
public function warning(string $message, array $context = []): void |
177
|
|
|
{ |
178
|
|
|
$this->log(LogLevel::WARNING, $message, $context); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Normal but significant events. |
183
|
|
|
* |
184
|
|
|
* @param string $message |
185
|
|
|
* @param array $context |
186
|
|
|
* |
187
|
|
|
* @return void |
188
|
|
|
*/ |
189
|
|
|
public function notice(string $message, array $context = []): void |
190
|
|
|
{ |
191
|
|
|
$this->log(LogLevel::NOTICE, $message, $context); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Interesting events. |
196
|
|
|
* |
197
|
|
|
* Example: User logs in, SQL logs. |
198
|
|
|
* |
199
|
|
|
* @param string $message |
200
|
|
|
* @param array $context |
201
|
|
|
* |
202
|
|
|
* @return void |
203
|
|
|
*/ |
204
|
|
|
public function info(string $message, array $context = []): void |
205
|
|
|
{ |
206
|
|
|
$this->log(LogLevel::INFO, $message, $context); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Detailed debug information. |
211
|
|
|
* |
212
|
|
|
* @param string $message |
213
|
|
|
* @param array $context |
214
|
|
|
* |
215
|
|
|
* @return void |
216
|
|
|
*/ |
217
|
|
|
public function debug(string $message, array $context = []): void |
218
|
|
|
{ |
219
|
|
|
$this->log(LogLevel::DEBUG, $message, $context); |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
|