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; |
12
|
|
|
|
13
|
|
|
use Codedungeon\PHPCliColors\Color; |
14
|
|
|
use Psr\Log\AbstractLogger; |
15
|
|
|
use Psr\Log\LoggerInterface; |
16
|
|
|
|
17
|
|
|
class ConsoleLogger extends AbstractLogger implements LoggerInterface |
18
|
|
|
{ |
19
|
|
|
// use LoggerTrait; |
20
|
|
|
|
21
|
|
|
public $verbose = false; |
22
|
|
|
public $debug = false; |
23
|
|
|
public $colorMode = false; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Ultralight logger. |
27
|
|
|
* |
28
|
|
|
* @inheritDoc |
29
|
|
|
*/ |
30
|
|
|
public function log($level, $message, array $context = []) |
31
|
|
|
{ |
32
|
|
|
if (is_array($message)) { |
33
|
|
|
dump($message); |
34
|
|
|
echo "Envoi de array comme message de log...\n"; |
35
|
|
|
|
36
|
|
|
return; |
37
|
|
|
} |
38
|
|
|
$message = trim($message); |
39
|
|
|
$date = date('Y-m-d H:i:s'); |
40
|
|
|
switch ($level) { |
41
|
|
|
case 'emergency': |
42
|
|
|
case 'alert': |
43
|
|
|
case 'critical': |
44
|
|
|
$this->echoColor("[$level] ".$date.' : '.$message."\n", Color::BG_RED.Color::WHITE); |
45
|
|
|
if ($context !== []) { |
46
|
|
|
dump($context); |
47
|
|
|
} |
48
|
|
|
$this->logInFile($level, $message); |
49
|
|
|
break; |
50
|
|
|
case 'error': |
51
|
|
|
case 'warning': |
52
|
|
|
$this->echoColor("[$level] ".$date.' : '.$message."\n", Color::BG_YELLOW.Color::BLACK); |
53
|
|
|
if ($context !== []) { |
54
|
|
|
dump($context); |
55
|
|
|
} |
56
|
|
|
break; |
57
|
|
|
case 'notice': |
58
|
|
|
$this->echoColor("[$level] ".$message."\n"); |
59
|
|
|
if ($context !== []) { |
60
|
|
|
dump($context); |
61
|
|
|
} |
62
|
|
|
break; |
63
|
|
|
case 'info': |
64
|
|
|
if ($this->verbose || $this->debug) { |
65
|
|
|
$this->echoColor("[$level] ".$message."\n", Color::GRAY); |
66
|
|
|
if ($context !== []) { |
67
|
|
|
dump($context); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
break; |
71
|
|
|
case 'debug': |
72
|
|
|
if ($this->debug) { |
73
|
|
|
$this->echoColor("[$level] ".$message."\n", Color::GRAY); |
74
|
|
|
if ($context !== []) { |
75
|
|
|
dump($context); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
break; |
79
|
|
|
case 'echo': |
80
|
|
|
$this->echoColor($message."\n"); |
81
|
|
|
break; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
private function logInFile($level, string $message) |
86
|
|
|
{ |
87
|
|
|
file_put_contents( |
88
|
|
|
__DIR__.'/resources/critical.log', |
89
|
|
|
date('d-m-Y H:i:s')." : $level : ".$message.PHP_EOL, |
90
|
|
|
FILE_APPEND |
91
|
|
|
); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
private function echoColor(string $text, ?string $color = null) |
95
|
|
|
{ |
96
|
|
|
if ($this->colorMode && !empty($color)) { |
97
|
|
|
echo $color.$text.Color::NORMAL; |
98
|
|
|
|
99
|
|
|
return; |
100
|
|
|
} |
101
|
|
|
echo $text; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
} |
105
|
|
|
|