|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
use Psr\Log\LoggerInterface; |
|
5
|
|
|
|
|
6
|
|
|
class ConsoleLogger implements LoggerInterface |
|
7
|
|
|
{ |
|
8
|
|
|
/** |
|
9
|
|
|
* System is unusable. |
|
10
|
|
|
* |
|
11
|
|
|
* @param string $message |
|
12
|
|
|
* @param array<mixed> $context |
|
13
|
|
|
* |
|
14
|
|
|
* @return void |
|
15
|
|
|
*/ |
|
16
|
|
|
public function emergency($message, array $context = []): void |
|
17
|
|
|
{ |
|
18
|
|
|
$this->log('EMERGENCY', $message, $context); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Action must be taken immediately. |
|
23
|
|
|
* |
|
24
|
|
|
* Example: Entire website down, database unavailable, etc. This should |
|
25
|
|
|
* trigger the SMS alerts and wake you up. |
|
26
|
|
|
* |
|
27
|
|
|
* @param string $message |
|
28
|
|
|
* @param array<mixed> $context |
|
29
|
|
|
* |
|
30
|
|
|
* @return void |
|
31
|
|
|
*/ |
|
32
|
|
|
public function alert($message, array $context = []): void |
|
33
|
|
|
{ |
|
34
|
|
|
$this->log('ALERT', $message, $context); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Critical conditions. |
|
39
|
|
|
* |
|
40
|
|
|
* Example: Application component unavailable, unexpected exception. |
|
41
|
|
|
* |
|
42
|
|
|
* @param string $message |
|
43
|
|
|
* @param array<mixed> $context |
|
44
|
|
|
* |
|
45
|
|
|
* @return void |
|
46
|
|
|
*/ |
|
47
|
|
|
public function critical($message, array $context = []): void |
|
48
|
|
|
{ |
|
49
|
|
|
$this->log('CRITICAL', $message, $context); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Runtime errors that do not require immediate action but should typically |
|
54
|
|
|
* be logged and monitored. |
|
55
|
|
|
* |
|
56
|
|
|
* @param string $message |
|
57
|
|
|
* @param array<mixed> $context |
|
58
|
|
|
* |
|
59
|
|
|
* @return void |
|
60
|
|
|
*/ |
|
61
|
|
|
public function error($message, array $context = []): void |
|
62
|
|
|
{ |
|
63
|
|
|
$this->log('ERROR', $message, $context); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Exceptional occurrences that are not errors. |
|
68
|
|
|
* |
|
69
|
|
|
* Example: Use of deprecated APIs, poor use of an API, undesirable things |
|
70
|
|
|
* that are not necessarily wrong. |
|
71
|
|
|
* |
|
72
|
|
|
* @param string $message |
|
73
|
|
|
* @param array<mixed> $context |
|
74
|
|
|
* |
|
75
|
|
|
* @return void |
|
76
|
|
|
*/ |
|
77
|
|
|
public function warning($message, array $context = []): void |
|
78
|
|
|
{ |
|
79
|
|
|
$this->log('WARNING', $message, $context); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Normal but significant events. |
|
84
|
|
|
* |
|
85
|
|
|
* @param string $message |
|
86
|
|
|
* @param array<mixed> $context |
|
87
|
|
|
* |
|
88
|
|
|
* @return void |
|
89
|
|
|
*/ |
|
90
|
|
|
public function notice($message, array $context = []): void |
|
91
|
|
|
{ |
|
92
|
|
|
$this->log('NOTICE', $message, $context); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Interesting events. |
|
97
|
|
|
* |
|
98
|
|
|
* Example: User logs in, SQL logs. |
|
99
|
|
|
* |
|
100
|
|
|
* @param string $message |
|
101
|
|
|
* @param array<mixed> $context |
|
102
|
|
|
* |
|
103
|
|
|
* @return void |
|
104
|
|
|
*/ |
|
105
|
|
|
public function info($message, array $context = []): void |
|
106
|
|
|
{ |
|
107
|
|
|
$this->log('INFO', $message, $context); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Detailed debug information. |
|
112
|
|
|
* |
|
113
|
|
|
* @param string $message |
|
114
|
|
|
* @param array<mixed> $context |
|
115
|
|
|
* |
|
116
|
|
|
* @return void |
|
117
|
|
|
*/ |
|
118
|
|
|
public function debug($message, array $context = []): void |
|
119
|
|
|
{ |
|
120
|
|
|
$this->log('DEBUG', $message, $context); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Logs with an arbitrary level. |
|
125
|
|
|
* |
|
126
|
|
|
* @param mixed $level |
|
127
|
|
|
* @param string $message |
|
128
|
|
|
* @param array<mixed> $context |
|
129
|
|
|
* |
|
130
|
|
|
* @return void |
|
131
|
|
|
*/ |
|
132
|
|
|
public function log($level, $message, array $context = []): void |
|
133
|
|
|
{ |
|
134
|
|
|
echo $level . ' - ' . $message . PHP_EOL; |
|
135
|
|
|
print_r($context); |
|
136
|
|
|
} |
|
137
|
|
|
} |