|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of dispositif/wikibot application (@github) |
|
4
|
|
|
* 2019/2020 © Philippe/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 Logger 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 (!empty($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 (!empty($context)) { |
|
54
|
|
|
dump($context); |
|
55
|
|
|
} |
|
56
|
|
|
break; |
|
57
|
|
|
case 'notice': |
|
58
|
|
|
$this->echoColor("[$level] ".$message."\n"); |
|
59
|
|
|
if (!empty($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 (!empty($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 (!empty($context)) { |
|
75
|
|
|
dump($context); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
break; |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
private function logInFile($level, string $message) |
|
83
|
|
|
{ |
|
84
|
|
|
file_put_contents( |
|
85
|
|
|
__DIR__.'/resources/critical.log', |
|
86
|
|
|
date('d-m-Y H:i:s')." : $level : ".$message.PHP_EOL, |
|
87
|
|
|
FILE_APPEND |
|
88
|
|
|
); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
private function echoColor(string $text, ?string $color = null) |
|
92
|
|
|
{ |
|
93
|
|
|
if ($this->colorMode && !empty($color)) { |
|
94
|
|
|
echo $color.$text.Color::NORMAL; |
|
95
|
|
|
|
|
96
|
|
|
return; |
|
97
|
|
|
} |
|
98
|
|
|
echo $text; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
} |
|
102
|
|
|
|