|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of dispositif/wikibot application (@github) |
|
4
|
|
|
* 2019-2023 © Philippe M./Irønie <[email protected]> |
|
5
|
|
|
* For the full copyright and MIT license information, view the license file. |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
declare(strict_types=1); |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
namespace App\Infrastructure\Monitor; |
|
12
|
|
|
|
|
13
|
|
|
use Codedungeon\PHPCliColors\Color; |
|
14
|
|
|
use Exception; |
|
15
|
|
|
use Psr\Log\AbstractLogger; |
|
16
|
|
|
use Psr\Log\LoggerInterface; |
|
17
|
|
|
|
|
18
|
|
|
// todo move /Monitor |
|
19
|
|
|
class ConsoleLogger extends AbstractLogger implements LoggerInterface |
|
20
|
|
|
{ |
|
21
|
|
|
// use LoggerTrait; |
|
22
|
|
|
|
|
23
|
|
|
public bool $verbose = false; |
|
24
|
|
|
public bool $debug = false; |
|
25
|
|
|
public bool $colorMode = false; |
|
26
|
|
|
|
|
27
|
|
|
public function __construct(public Stats|NullStats $stats = new Stats()) |
|
28
|
|
|
{ |
|
29
|
|
|
try { |
|
30
|
|
|
$this->stats->increment('test.consolelogger'); |
|
31
|
|
|
} catch (Exception $e) { |
|
32
|
|
|
$this->stats = new NullStats(); |
|
33
|
|
|
} |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function __call(string $method, array $args): void |
|
37
|
|
|
{ |
|
38
|
|
|
$this->notice('Call to undefined method ConsoleLogger:' . $method . '()'); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Ultralight logger. |
|
43
|
|
|
* @inheritDoc |
|
44
|
|
|
*/ |
|
45
|
|
|
public function log($level, $message, array $context = []): void |
|
46
|
|
|
{ |
|
47
|
|
|
if (is_array($message)) { |
|
48
|
|
|
$this->error('Array comme message de log...', ['message' => $message]); |
|
49
|
|
|
|
|
50
|
|
|
return; |
|
51
|
|
|
} |
|
52
|
|
|
$message = trim($message); |
|
53
|
|
|
$date = date('Y-m-d H:i:s'); |
|
54
|
|
|
|
|
55
|
|
|
$this->incrementStatsFromContext($context); |
|
56
|
|
|
if (isset($context['stats'])) { |
|
57
|
|
|
unset($context['stats']); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
switch ($level) { |
|
61
|
|
|
case 'emergency': |
|
62
|
|
|
case 'alert': |
|
63
|
|
|
case 'critical': |
|
64
|
|
|
$this->echoColor("[$level] " . $date . ' : ' . $message . "\n", Color::BG_RED . Color::WHITE); |
|
65
|
|
|
if ($context !== []) { |
|
66
|
|
|
dump($context); |
|
67
|
|
|
} |
|
68
|
|
|
$this->logInFile($level, $message); |
|
69
|
|
|
break; |
|
70
|
|
|
case 'error': |
|
71
|
|
|
case 'warning': |
|
72
|
|
|
$this->echoColor("[$level] " . $date . ' : ' . $message . "\n", Color::BG_YELLOW . Color::BLACK); |
|
73
|
|
|
if ($context !== []) { |
|
74
|
|
|
dump($context); |
|
75
|
|
|
} |
|
76
|
|
|
break; |
|
77
|
|
|
case 'notice': |
|
78
|
|
|
$this->echoColor("[$level] " . $message . "\n"); |
|
79
|
|
|
if ($context !== []) { |
|
80
|
|
|
dump($context); |
|
81
|
|
|
} |
|
82
|
|
|
break; |
|
83
|
|
|
case 'info': |
|
84
|
|
|
if ($this->verbose || $this->debug) { |
|
85
|
|
|
$this->echoColor("[$level] " . $message . "\n", Color::GRAY); |
|
86
|
|
|
if ($context !== []) { |
|
87
|
|
|
dump($context); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
break; |
|
91
|
|
|
case 'debug': |
|
92
|
|
|
if ($this->debug) { |
|
93
|
|
|
$this->echoColor("[$level] " . $message . "\n", Color::GRAY); |
|
94
|
|
|
if ($context !== []) { |
|
95
|
|
|
dump($context); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
break; |
|
99
|
|
|
case 'echo': |
|
100
|
|
|
$this->echoColor($message . "\n"); |
|
101
|
|
|
break; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
protected function incrementStatsFromContext(array $context): void |
|
106
|
|
|
{ |
|
107
|
|
|
if (!isset($context['stats'])) { |
|
108
|
|
|
return; |
|
109
|
|
|
} |
|
110
|
|
|
if (is_string($context['stats']) && $context['stats'] !== '') { |
|
111
|
|
|
$this->stats->increment($context['stats']); |
|
112
|
|
|
} |
|
113
|
|
|
if (is_array($context['stats'])) { |
|
114
|
|
|
foreach ($context['stats'] as $tag) { |
|
115
|
|
|
$this->stats->increment($tag); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
private function echoColor(string $text, ?string $color = null): void |
|
121
|
|
|
{ |
|
122
|
|
|
if ($this->colorMode && !empty($color)) { |
|
123
|
|
|
echo $color . $text . Color::NORMAL; |
|
124
|
|
|
|
|
125
|
|
|
return; |
|
126
|
|
|
} |
|
127
|
|
|
echo $text; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
private function logInFile($level, string $message): void |
|
131
|
|
|
{ |
|
132
|
|
|
file_put_contents( |
|
133
|
|
|
__DIR__ . '/resources/critical.log', |
|
134
|
|
|
date('d-m-Y H:i:s') . " : $level : " . $message . PHP_EOL, |
|
135
|
|
|
FILE_APPEND |
|
136
|
|
|
); |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|