-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogger.php
More file actions
347 lines (299 loc) · 10.6 KB
/
Logger.php
File metadata and controls
347 lines (299 loc) · 10.6 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
<?php
/**
* This file is part of Blitz PHP framework.
*
* (c) 2022 Dimitri Sitchet Tomkeu <devcode.dst@gmail.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace BlitzPHP\Debug;
use InvalidArgumentException;
use Monolog\Formatter\HtmlFormatter;
use Monolog\Formatter\JsonFormatter;
use Monolog\Formatter\LineFormatter;
use Monolog\Formatter\NormalizerFormatter;
use Monolog\Formatter\ScalarFormatter;
use Monolog\Handler\BrowserConsoleHandler;
use Monolog\Handler\ChromePHPHandler;
use Monolog\Handler\ErrorLogHandler;
use Monolog\Handler\FirePHPHandler;
use Monolog\Handler\NativeMailerHandler;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Handler\StreamHandler;
use Monolog\Handler\TelegramBotHandler;
use Monolog\Logger as MonologLogger;
use Monolog\Processor\HostnameProcessor;
use Monolog\Processor\IntrospectionProcessor;
use Monolog\Processor\MemoryUsageProcessor;
use Monolog\Processor\ProcessIdProcessor;
use Monolog\Processor\PsrLogMessageProcessor;
use Monolog\Processor\UidProcessor;
use Monolog\Processor\WebProcessor;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
use stdClass;
use Stringable;
class Logger implements LoggerInterface
{
/**
* Options de configuration provenant de app/Config/log.php
*
* @var object
*/
private readonly stdClass $config;
/**
* Met en cache les appels de journalisation pour la barre de débogage.
*/
public array $logCache = [];
/**
* Devrions-nous mettre en cache nos éléments enregistrés ?
*
* @var bool
*/
protected $cacheLogs = false;
/**
* Instance monolog
*/
private readonly MonologLogger $monolog;
public function __construct(bool $debug = BLITZ_DEBUG)
{
$this->config = (object) config('log');
$this->monolog = new MonologLogger(str_replace(' ', '-', $this->config->name ?? 'application'));
foreach (($this->config->handlers ?? []) as $handler => $options) {
$this->pushHandler($handler, (object) $options);
}
foreach (($this->config->processors ?? []) as $processor) {
$this->pushProcessor($processor);
}
$this->cacheLogs = $debug;
if ($this->cacheLogs) {
$this->logCache = [];
}
}
/**
* {@inheritDoc}
*/
public function emergency(string|Stringable $message, array $context = []): void
{
$this->log(LogLevel::EMERGENCY, $message, $context);
}
/**
* {@inheritDoc}
*/
public function alert(string|Stringable $message, array $context = []): void
{
$this->log(LogLevel::ALERT, $message, $context);
}
/**
* {@inheritDoc}
*/
public function critical(string|Stringable $message, array $context = []): void
{
$this->log(LogLevel::CRITICAL, $message, $context);
}
/**
* {@inheritDoc}
*/
public function error(string|Stringable $message, array $context = []): void
{
$this->log(LogLevel::ERROR, $message, $context);
}
/**
* {@inheritDoc}
*/
public function warning(string|Stringable $message, array $context = []): void
{
$this->log(LogLevel::WARNING, $message, $context);
}
/**
* {@inheritDoc}
*/
public function notice(string|Stringable $message, array $context = []): void
{
$this->log(LogLevel::NOTICE, $message, $context);
}
/**
* {@inheritDoc}
*/
public function info(string|Stringable $message, array $context = []): void
{
$this->log(LogLevel::INFO, $message, $context);
}
/**
* {@inheritDoc}
*/
public function debug(string|Stringable $message, array $context = []): void
{
$this->log(LogLevel::DEBUG, $message, $context);
}
/**
* {@inheritDoc}
*/
public function log($level, string|Stringable $message, array $context = []): void
{
$this->monolog->log($level, $message, $context);
if ($this->cacheLogs) {
$this->logCache[] = [
'level' => $level,
'msg' => $message,
];
}
}
/**
* Ajoute les differents gestionnaires prise en charge par la configuration /app/Config/log.php
*/
private function pushHandler(string $handler, stdClass $options)
{
match ($handler) {
'error' => $this->pushErrorHandler($options),
'email' => $this->pushEmailHandler($options),
'telegram' => $this->pushTelegramHandler($options),
'chrome' => $this->pushChromeHandler($options),
'firebug' => $this->pushFirebugHandler($options),
'browser' => $this->pushBrowserHandler($options),
default => $this->pushFileHandler($options),
};
}
/**
* Ajoute un gestionnaire de log de type Fichier
*
* Enregistre les problèmes dans des fichiers de journalisation
*/
private function pushFileHandler(stdClass $options): void
{
$directory = rtrim($options->path ?: LOG_PATH, DS) . DS;
$filename = strtolower($this->config->name ?: 'application');
$extension = $options->extension ?: '.log';
if (($options->dayly_rotation ?: true) === true) {
$handler = new RotatingFileHandler($directory . $filename . $extension, $options->max_files ?: 0, $options->level ?: LogLevel::DEBUG, true, $options->permissions ?: 644);
} else {
$handler = new StreamHandler($directory . $filename . $extension, $options->level ?: LogLevel::DEBUG, true, $options->permissions ?: 644);
}
$this->monolog->pushHandler(
$this->setFormatter($handler, ['json', 'line', 'scalar', 'normalizer'], $options->format)
);
}
/**
* Ajoute un gestionnaire de log de type PHP error_log
*
* Enregistre les problèmes dans la fonction PHP error_log().
*/
private function pushErrorHandler(stdClass $options): void
{
$handler = new ErrorLogHandler($options->type ?: ErrorLogHandler::OPERATING_SYSTEM, $options->level ?: LogLevel::DEBUG);
$this->monolog->pushHandler(
$this->setFormatter($handler, ['json', 'line'], $options->format)
);
}
/**
* Ajoute un gestionnaire de log de type Email
*
* Envoi un email à l'administrateur en cas de problème
*/
private function pushEmailHandler(stdClass $options): void
{
$handler = new NativeMailerHandler($options->to, $options->subject, $options->from, $options->level ?: LogLevel::ERROR);
$this->monolog->pushHandler(
$this->setFormatter($handler, ['html', 'json', 'line'], $options->format)
);
}
private function pushTelegramHandler(stdClass $options): void
{
$handler = new TelegramBotHandler($options->api_key, $options->channel, $options->level ?: LogLevel::DEBUG);
$this->monolog->pushHandler(
$this->setFormatter($handler, [], $options->format)
);
}
/**
* Ajoute un gestionnaire de log pour chrome
*
* Affichera les log dans la console de chrome
*/
private function pushChromeHandler(stdClass $options): void
{
$handler = new ChromePHPHandler($options->level ?: LogLevel::DEBUG);
$this->monolog->pushHandler(
$this->setFormatter($handler, [], $options->format)
);
}
/**
* Ajoute un gestionnaire de log pour firebug
*
* Affichera les log dans la console firebug
*/
private function pushFirebugHandler(stdClass $options): void
{
$handler = new FirePHPHandler();
$this->monolog->pushHandler(
$this->setFormatter($handler, [], $options->format)
);
}
/**
* Ajoute un gestionnaire de log pour les navigateurs
*
* Affichera les log dans la console des navigateurs
*/
private function pushBrowserHandler(stdClass $options): void
{
$handler = new BrowserConsoleHandler();
$this->monolog->pushHandler(
$this->setFormatter($handler, [], $options->format)
);
}
/**
* Ajoute les processeur au gestionnaire de log
*
* Un processor permet d'ajouter des méta données aux log générés
*/
private function pushProcessor(string $processor)
{
match ($processor) {
'web' => $this->monolog->pushProcessor(new WebProcessor()),
'introspection' => $this->monolog->pushProcessor(new IntrospectionProcessor()),
'hostname' => $this->monolog->pushProcessor(new HostnameProcessor()),
'process_id' => $this->monolog->pushProcessor(new ProcessIdProcessor()),
'uid' => $this->monolog->pushProcessor(new UidProcessor()),
'memory_usage' => $this->monolog->pushProcessor(new MemoryUsageProcessor()),
'psr' => $this->monolog->pushProcessor(new PsrLogMessageProcessor()),
default => throw new InvalidArgumentException('Invalid formatter for log processor. Accepts values: web/introspection/hostname/process_id/uid/memory_usage/psr'),
};
}
/**
* Definit le formateur des gestionnaire
*
* @param array $allowed Formats autorisés
*/
private function setFormatter(object $handler, array $allowed, ?string $format = 'json'): object
{
if (! method_exists($handler, 'setFormatter')) {
return $handler;
}
if ($format === null || $format === '') {
$format = 'json';
}
if ($allowed !== [] && ! in_array($format, $allowed, true)) {
throw new InvalidArgumentException('Invalid formatter for log file handler. Accepts values: ' . implode('/', $allowed));
}
switch ($format) {
case 'json':
$handler->setFormatter(new JsonFormatter());
break;
case 'line':
$handler->setFormatter(new LineFormatter(null, $this->config->date_format ?? 'Y-m-d H:i:s'));
break;
case 'normalizer':
$handler->setFormatter(new NormalizerFormatter($this->config->date_format ?? 'Y-m-d H:i:s'));
break;
case 'scalar':
$handler->setFormatter(new ScalarFormatter($this->config->date_format ?? 'Y-m-d H:i:s'));
break;
case 'html':
$handler->setFormatter(new HtmlFormatter($this->config->date_format ?? 'Y-m-d H:i:s'));
break;
default:
break;
}
return $handler;
}
}