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
|
|
|
protected const CRITICAL_LOG_PATH = __DIR__ . '/../../../log/critical.log'; |
24
|
|
|
public bool $verbose = false; |
25
|
|
|
public bool $debug = false; |
26
|
|
|
public bool $colorMode = false; |
27
|
|
|
|
28
|
|
|
public function __construct(public StatsInterface $stats = new NullStats()) |
29
|
|
|
{ |
30
|
|
|
try { |
31
|
|
|
$this->stats->increment('test.consolelogger'); |
32
|
|
|
} catch (Exception $e) { |
33
|
|
|
$this->stats = new NullStats(); |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function __call(string $method, array $args): void |
38
|
|
|
{ |
39
|
|
|
$this->notice('Call to undefined method ConsoleLogger:' . $method . '()'); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Ultralight logger. |
44
|
|
|
* @inheritDoc |
45
|
|
|
*/ |
46
|
|
|
public function log($level, $message, array $context = []): void |
47
|
|
|
{ |
48
|
|
|
if (is_array($message)) { |
49
|
|
|
$this->error('Array comme message de log...', ['message' => $message]); |
50
|
|
|
|
51
|
|
|
return; |
52
|
|
|
} |
53
|
|
|
$message = trim($message); |
54
|
|
|
$date = date('Y-m-d H:i:s'); |
55
|
|
|
|
56
|
|
|
$this->incrementStatsFromContext($context); |
57
|
|
|
if (isset($context['stats'])) { |
58
|
|
|
unset($context['stats']); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
switch ($level) { |
62
|
|
|
case 'emergency': |
63
|
|
|
case 'alert': |
64
|
|
|
case 'critical': |
65
|
|
|
$this->echoColor("[$level] " . $date . ' : ' . $message . "\n", Color::BG_RED . Color::WHITE); |
66
|
|
|
if ($context !== []) { |
67
|
|
|
dump($context); |
68
|
|
|
} |
69
|
|
|
$this->logInFile($level, $message); |
70
|
|
|
break; |
71
|
|
|
case 'error': |
72
|
|
|
case 'warning': |
73
|
|
|
$this->echoColor("[$level] " . $date . ' : ' . $message . "\n", Color::BG_YELLOW . Color::BLACK); |
74
|
|
|
if ($context !== []) { |
75
|
|
|
dump($context); |
76
|
|
|
} |
77
|
|
|
break; |
78
|
|
|
case 'notice': |
79
|
|
|
$this->echoColor("[$level] " . $message . "\n"); |
80
|
|
|
if ($context !== []) { |
81
|
|
|
dump($context); |
82
|
|
|
} |
83
|
|
|
break; |
84
|
|
|
case 'info': |
85
|
|
|
if ($this->verbose || $this->debug) { |
86
|
|
|
$this->echoColor("[$level] " . $message . "\n", Color::GRAY); |
87
|
|
|
if ($context !== []) { |
88
|
|
|
dump($context); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
break; |
92
|
|
|
case 'debug': |
93
|
|
|
if ($this->debug) { |
94
|
|
|
$this->echoColor("[$level] " . $message . "\n", Color::GRAY); |
95
|
|
|
if ($context !== []) { |
96
|
|
|
dump($context); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
break; |
100
|
|
|
case 'echo': |
101
|
|
|
$this->echoColor($message . "\n"); |
102
|
|
|
break; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
protected function incrementStatsFromContext(array $context): void |
107
|
|
|
{ |
108
|
|
|
if (!isset($context['stats'])) { |
109
|
|
|
return; |
110
|
|
|
} |
111
|
|
|
if (is_string($context['stats']) && $context['stats'] !== '') { |
112
|
|
|
$this->stats->increment($context['stats']); |
113
|
|
|
} |
114
|
|
|
if (is_array($context['stats'])) { |
115
|
|
|
foreach ($context['stats'] as $tag) { |
116
|
|
|
$this->stats->increment($tag); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
private function echoColor(string $text, ?string $color = null): void |
122
|
|
|
{ |
123
|
|
|
if ($this->colorMode && !empty($color)) { |
124
|
|
|
echo $color . $text . Color::NORMAL; |
125
|
|
|
|
126
|
|
|
return; |
127
|
|
|
} |
128
|
|
|
echo $text; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
private function logInFile($level, string $message): void |
132
|
|
|
{ |
133
|
|
|
file_put_contents( |
134
|
|
|
self::CRITICAL_LOG_PATH, |
135
|
|
|
date('d-m-Y H:i:s') . " : $level : " . $message . PHP_EOL, |
136
|
|
|
FILE_APPEND |
137
|
|
|
); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|