14
14
use Psr \Log \AbstractLogger ;
15
15
use Psr \Log \InvalidArgumentException ;
16
16
use Psr \Log \LogLevel ;
17
+ use Symfony \Component \HttpFoundation \Request ;
18
+ use Symfony \Component \HttpFoundation \RequestStack ;
17
19
18
20
/**
19
21
* Minimalist PSR-3 logger designed to write in stderr or any other stream.
20
22
*
21
23
* @author Kévin Dunglas <dunglas@gmail.com>
22
24
*/
23
- class Logger extends AbstractLogger
25
+ class Logger extends AbstractLogger implements DebugLoggerInterface
24
26
{
25
27
private const LEVELS = [
26
28
LogLevel::DEBUG => 0 ,
@@ -32,17 +34,30 @@ class Logger extends AbstractLogger
32
34
LogLevel::ALERT => 6 ,
33
35
LogLevel::EMERGENCY => 7 ,
34
36
];
37
+ private const PRIORITIES = [
38
+ LogLevel::DEBUG => 100 ,
39
+ LogLevel::INFO => 200 ,
40
+ LogLevel::NOTICE => 250 ,
41
+ LogLevel::WARNING => 300 ,
42
+ LogLevel::ERROR => 400 ,
43
+ LogLevel::CRITICAL => 500 ,
44
+ LogLevel::ALERT => 550 ,
45
+ LogLevel::EMERGENCY => 600 ,
46
+ ];
35
47
36
48
private int $ minLevelIndex ;
37
49
private \Closure $ formatter ;
50
+ private bool $ debug = false ;
51
+ private array $ logs = [];
52
+ private array $ errorCount = [];
38
53
39
54
/** @var resource|null */
40
55
private $ handle ;
41
56
42
57
/**
43
58
* @param string|resource|null $output
44
59
*/
45
- public function __construct (string $ minLevel = null , $ output = null , callable $ formatter = null )
60
+ public function __construct (string $ minLevel = null , $ output = null , callable $ formatter = null , private readonly ? RequestStack $ requestStack = null )
46
61
{
47
62
if (null === $ minLevel ) {
48
63
$ minLevel = null === $ output || 'php://stdout ' === $ output || 'php://stderr ' === $ output ? LogLevel::ERROR : LogLevel::WARNING ;
@@ -69,6 +84,11 @@ public function __construct(string $minLevel = null, $output = null, callable $f
69
84
}
70
85
}
71
86
87
+ public function enableDebug (bool $ enable ): void
88
+ {
89
+ $ this ->debug = $ enable ;
90
+ }
91
+
72
92
public function log ($ level , $ message , array $ context = []): void
73
93
{
74
94
if (!isset (self ::LEVELS [$ level ])) {
@@ -85,6 +105,34 @@ public function log($level, $message, array $context = []): void
85
105
} else {
86
106
error_log ($ formatter ($ level , $ message , $ context , false ));
87
107
}
108
+
109
+ if ($ this ->debug && $ this ->requestStack ) {
110
+ $ this ->record ($ level , $ message , $ context );
111
+ }
112
+ }
113
+
114
+ public function getLogs (Request $ request = null ): array
115
+ {
116
+ if ($ request ) {
117
+ return $ this ->logs [spl_object_hash ($ request )] ?? [];
118
+ }
119
+
120
+ return array_merge (...array_values ($ this ->logs ));
121
+ }
122
+
123
+ public function countErrors (Request $ request = null ): int
124
+ {
125
+ if ($ request ) {
126
+ return $ this ->errorCount [spl_object_hash ($ request )] ?? 0 ;
127
+ }
128
+
129
+ return array_sum ($ this ->errorCount );
130
+ }
131
+
132
+ public function clear (): void
133
+ {
134
+ $ this ->logs = [];
135
+ $ this ->errorCount = [];
88
136
}
89
137
90
138
private function format (string $ level , string $ message , array $ context , bool $ prefixDate = true ): string
@@ -113,4 +161,29 @@ private function format(string $level, string $message, array $context, bool $pr
113
161
114
162
return $ log ;
115
163
}
164
+
165
+ private function record ($ level , $ message , array $ context ): void
166
+ {
167
+ $ request = $ this ->requestStack ->getCurrentRequest ();
168
+ $ hash = $ request ? spl_object_hash ($ request ) : '' ;
169
+
170
+ $ this ->logs [$ hash ][] = [
171
+ 'channel ' => null ,
172
+ 'context ' => $ context ,
173
+ 'message ' => $ message ,
174
+ 'priority ' => self ::PRIORITIES [$ level ],
175
+ 'priorityName ' => $ level ,
176
+ 'timestamp ' => time (),
177
+ 'timestamp_rfc3339 ' => date (\DATE_RFC3339_EXTENDED ),
178
+ ];
179
+
180
+ $ this ->errorCount [$ hash ] ??= 0 ;
181
+ switch ($ level ) {
182
+ case LogLevel::ERROR :
183
+ case LogLevel::CRITICAL :
184
+ case LogLevel::ALERT :
185
+ case LogLevel::EMERGENCY :
186
+ ++$ this ->errorCount [$ hash ];
187
+ }
188
+ }
116
189
}
0 commit comments