1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Platine Logger |
5
|
|
|
* |
6
|
|
|
* Platine Logger is the implementation of PSR 3 |
7
|
|
|
* |
8
|
|
|
* This content is released under the MIT License (MIT) |
9
|
|
|
* |
10
|
|
|
* Copyright (c) 2020 Platine Logger |
11
|
|
|
* |
12
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
13
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
14
|
|
|
* in the Software without restriction, including without limitation the rights |
15
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
16
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
17
|
|
|
* furnished to do so, subject to the following conditions: |
18
|
|
|
* |
19
|
|
|
* The above copyright notice and this permission notice shall be included in all |
20
|
|
|
* copies or substantial portions of the Software. |
21
|
|
|
* |
22
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
23
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
24
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
25
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
26
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
27
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
28
|
|
|
* SOFTWARE. |
29
|
|
|
*/ |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @file Logger.php |
33
|
|
|
* |
34
|
|
|
* The Logger class is the main class to use to handle application log. |
35
|
|
|
* |
36
|
|
|
* @package Platine\Logger |
37
|
|
|
* @author Platine Developers Team |
38
|
|
|
* @copyright Copyright (c) 2020 |
39
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
40
|
|
|
* @link http://www.iacademy.cf |
41
|
|
|
* @version 1.0.0 |
42
|
|
|
* @filesource |
43
|
|
|
*/ |
44
|
|
|
|
45
|
|
|
declare(strict_types=1); |
46
|
|
|
|
47
|
|
|
namespace Platine\Logger; |
48
|
|
|
|
49
|
|
|
use Exception; |
50
|
|
|
|
51
|
|
|
class Logger implements LoggerInterface |
52
|
|
|
{ |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Special minimum log level which will not log any log levels. |
56
|
|
|
*/ |
57
|
|
|
public const LOG_LEVEL_NONE = 'none'; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* The logger handler to use |
61
|
|
|
* @var AbstractLoggerHandler |
62
|
|
|
*/ |
63
|
|
|
protected AbstractLoggerHandler $handler; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Log level hierarchy |
67
|
|
|
* @var array<string, int> |
68
|
|
|
*/ |
69
|
|
|
protected array $levels = [ |
70
|
|
|
self::LOG_LEVEL_NONE => 999, |
71
|
|
|
LogLevel::DEBUG => 0, |
72
|
|
|
LogLevel::INFO => 1, |
73
|
|
|
LogLevel::NOTICE => 2, |
74
|
|
|
LogLevel::WARNING => 3, |
75
|
|
|
LogLevel::ERROR => 4, |
76
|
|
|
LogLevel::CRITICAL => 5, |
77
|
|
|
LogLevel::ALERT => 6, |
78
|
|
|
LogLevel::EMERGENCY => 7, |
79
|
|
|
]; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Lowest log level to log. |
83
|
|
|
* @var int |
84
|
|
|
*/ |
85
|
|
|
protected int $logLevel; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Create new logger instance |
89
|
|
|
* |
90
|
|
|
* @param AbstractLoggerHandler|null $handler the logger handler to use |
91
|
|
|
* @param string $logLevel the default log level |
92
|
|
|
*/ |
93
|
|
|
public function __construct( |
94
|
|
|
?AbstractLoggerHandler $handler = null, |
95
|
|
|
string $logLevel = LogLevel::DEBUG |
96
|
|
|
) { |
97
|
|
|
$this->handler = $handler ? $handler : new NullHandler(); |
98
|
|
|
$this->setLevel($logLevel); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Return the handler instance |
103
|
|
|
* @return AbstractLoggerHandler |
104
|
|
|
*/ |
105
|
|
|
public function getHandler(): AbstractLoggerHandler |
106
|
|
|
{ |
107
|
|
|
return $this->handler; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Set the minimum log level |
112
|
|
|
* |
113
|
|
|
* @param string $logLevel |
114
|
|
|
* |
115
|
|
|
* @throws Exception |
116
|
|
|
* |
117
|
|
|
* @return self |
118
|
|
|
*/ |
119
|
|
|
public function setLevel(string $logLevel): self |
120
|
|
|
{ |
121
|
|
|
if (!array_key_exists($logLevel, $this->levels)) { |
122
|
|
|
throw new Exception(sprintf( |
123
|
|
|
'Log level [%s] is not a valid log level. ' |
124
|
|
|
. 'Must be one of (%s)', |
125
|
|
|
$logLevel, |
126
|
|
|
implode(', ', array_keys($this->levels)) |
127
|
|
|
)); |
128
|
|
|
} |
129
|
|
|
$this->logLevel = $this->levels[$logLevel]; |
130
|
|
|
|
131
|
|
|
return $this; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* {@inheritdoc} |
136
|
|
|
* |
137
|
|
|
* @see AbstractLoggerHandler::setChannel |
138
|
|
|
* |
139
|
|
|
* @return self |
140
|
|
|
*/ |
141
|
|
|
public function setChannel(string $channel): self |
142
|
|
|
{ |
143
|
|
|
$this->handler->setChannel($channel); |
144
|
|
|
|
145
|
|
|
return $this; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* {@inheritdoc} |
150
|
|
|
* @see AbstractLoggerHandler::setOutput |
151
|
|
|
* |
152
|
|
|
* @return self |
153
|
|
|
*/ |
154
|
|
|
public function setOutput(bool $stdout): self |
155
|
|
|
{ |
156
|
|
|
$this->handler->setOutput($stdout); |
157
|
|
|
|
158
|
|
|
return $this; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* {@inheritdoc} |
163
|
|
|
*/ |
164
|
|
|
public function emergency(string $message, array $context = []): void |
165
|
|
|
{ |
166
|
|
|
if ($this->levelCanLog(LogLevel::EMERGENCY)) { |
167
|
|
|
$this->log(LogLevel::EMERGENCY, $message, $context); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* {@inheritdoc} |
173
|
|
|
*/ |
174
|
|
|
public function alert(string $message, array $context = []): void |
175
|
|
|
{ |
176
|
|
|
if ($this->levelCanLog(LogLevel::ALERT)) { |
177
|
|
|
$this->log(LogLevel::ALERT, $message, $context); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* {@inheritdoc} |
183
|
|
|
*/ |
184
|
|
|
public function critical(string $message, array $context = []): void |
185
|
|
|
{ |
186
|
|
|
if ($this->levelCanLog(LogLevel::CRITICAL)) { |
187
|
|
|
$this->log(LogLevel::CRITICAL, $message, $context); |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* {@inheritdoc} |
193
|
|
|
*/ |
194
|
|
|
public function error(string $message, array $context = []): void |
195
|
|
|
{ |
196
|
|
|
if ($this->levelCanLog(LogLevel::ERROR)) { |
197
|
|
|
$this->log(LogLevel::ERROR, $message, $context); |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* {@inheritdoc} |
203
|
|
|
*/ |
204
|
|
|
public function warning(string $message, array $context = []): void |
205
|
|
|
{ |
206
|
|
|
if ($this->levelCanLog(LogLevel::WARNING)) { |
207
|
|
|
$this->log(LogLevel::WARNING, $message, $context); |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* {@inheritdoc} |
213
|
|
|
*/ |
214
|
|
|
public function notice(string $message, array $context = []): void |
215
|
|
|
{ |
216
|
|
|
if ($this->levelCanLog(LogLevel::NOTICE)) { |
217
|
|
|
$this->log(LogLevel::NOTICE, $message, $context); |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* {@inheritdoc} |
223
|
|
|
*/ |
224
|
|
|
public function info(string $message, array $context = []): void |
225
|
|
|
{ |
226
|
|
|
if ($this->levelCanLog(LogLevel::INFO)) { |
227
|
|
|
$this->log(LogLevel::INFO, $message, $context); |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* {@inheritdoc} |
233
|
|
|
*/ |
234
|
|
|
public function debug(string $message, array $context = []): void |
235
|
|
|
{ |
236
|
|
|
if ($this->levelCanLog(LogLevel::DEBUG)) { |
237
|
|
|
$this->log(LogLevel::DEBUG, $message, $context); |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* {@inheritdoc} |
243
|
|
|
*/ |
244
|
|
|
public function log($level, string $message, array $context = []): void |
245
|
|
|
{ |
246
|
|
|
$this->handler->log($level, $message, $context); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Determine if the logger should log at a certain log level. |
251
|
|
|
* @param string $level log level to check |
252
|
|
|
* @return bool |
253
|
|
|
*/ |
254
|
|
|
protected function levelCanLog(string $level): bool |
255
|
|
|
{ |
256
|
|
|
return $this->levels[$level] >= $this->logLevel; |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
|