34
34
class ErrorListener implements EventSubscriberInterface
35
35
{
36
36
/**
37
- * @param array<class-string, array{log_level: string|null, status_code: int<100,599>|null}> $exceptionsMapping
37
+ * @param array<class-string, array{log_level: string|null, status_code: int<100,599>|null, log_channel: string|null }> $exceptionsMapping
38
38
*/
39
39
public function __construct (
40
40
protected string |object |array |null $ controller ,
41
41
protected ?LoggerInterface $ logger = null ,
42
42
protected bool $ debug = false ,
43
43
protected array $ exceptionsMapping = [],
44
+ protected array $ loggers = [],
44
45
) {
45
46
}
46
47
47
48
public function logKernelException (ExceptionEvent $ event ): void
48
49
{
49
50
$ throwable = $ event ->getThrowable ();
50
51
$ logLevel = $ this ->resolveLogLevel ($ throwable );
52
+ $ logChannel = $ this ->resolveLogChannel ($ throwable );
51
53
52
54
foreach ($ this ->exceptionsMapping as $ class => $ config ) {
53
55
if (!$ throwable instanceof $ class || !$ config ['status_code ' ]) {
@@ -69,7 +71,7 @@ public function logKernelException(ExceptionEvent $event): void
69
71
70
72
$ e = FlattenException::createFromThrowable ($ throwable );
71
73
72
- $ this ->logException ($ throwable , \sprintf ('Uncaught PHP Exception %s: "%s" at %s line %s ' , $ e ->getClass (), $ e ->getMessage (), basename ($ e ->getFile ()), $ e ->getLine ()), $ logLevel );
74
+ $ this ->logException ($ throwable , \sprintf ('Uncaught PHP Exception %s: "%s" at %s line %s ' , $ e ->getClass (), $ e ->getMessage (), basename ($ e ->getFile ()), $ e ->getLine ()), $ logLevel, $ logChannel );
73
75
}
74
76
75
77
public function onKernelException (ExceptionEvent $ event ): void
@@ -159,16 +161,20 @@ public static function getSubscribedEvents(): array
159
161
160
162
/**
161
163
* Logs an exception.
164
+ *
165
+ * @param ?string $logChannel
162
166
*/
163
- protected function logException (\Throwable $ exception , string $ message , ?string $ logLevel = null ): void
167
+ protected function logException (\Throwable $ exception , string $ message , ?string $ logLevel = null , /* ?string $logChannel = null */ ): void
164
168
{
165
- if (null === $ this ->logger ) {
169
+ $ logChannel = (3 < \func_num_args () ? \func_get_arg (3 ) : null ) ?? $ this ->resolveLogChannel ($ exception );
170
+
171
+ $ logLevel ??= $ this ->resolveLogLevel ($ exception );
172
+
173
+ if (!$ logger = $ this ->getLogger ($ logChannel )) {
166
174
return ;
167
175
}
168
176
169
- $ logLevel ??= $ this ->resolveLogLevel ($ exception );
170
-
171
- $ this ->logger ->log ($ logLevel , $ message , ['exception ' => $ exception ]);
177
+ $ logger ->log ($ logLevel , $ message , ['exception ' => $ exception ]);
172
178
}
173
179
174
180
/**
@@ -193,6 +199,17 @@ private function resolveLogLevel(\Throwable $throwable): string
193
199
return LogLevel::ERROR ;
194
200
}
195
201
202
+ private function resolveLogChannel (\Throwable $ throwable ): ?string
203
+ {
204
+ foreach ($ this ->exceptionsMapping as $ class => $ config ) {
205
+ if ($ throwable instanceof $ class && isset ($ config ['log_channel ' ])) {
206
+ return $ config ['log_channel ' ];
207
+ }
208
+ }
209
+
210
+ return null ;
211
+ }
212
+
196
213
/**
197
214
* Clones the request for the exception.
198
215
*/
@@ -201,7 +218,7 @@ protected function duplicateRequest(\Throwable $exception, Request $request): Re
201
218
$ attributes = [
202
219
'_controller ' => $ this ->controller ,
203
220
'exception ' => $ exception ,
204
- 'logger ' => DebugLoggerConfigurator::getDebugLogger ($ this ->logger ),
221
+ 'logger ' => DebugLoggerConfigurator::getDebugLogger ($ this ->getLogger ( $ exception ) ),
205
222
];
206
223
$ request = $ request ->duplicate (null , null , $ attributes );
207
224
$ request ->setMethod ('GET ' );
@@ -249,4 +266,9 @@ private function getInheritedAttribute(string $class, string $attribute): ?objec
249
266
250
267
return $ attributeReflector ?->newInstance();
251
268
}
269
+
270
+ private function getLogger (?string $ logChannel ): ?LoggerInterface
271
+ {
272
+ return $ logChannel ? $ this ->loggers [$ logChannel ] ?? $ this ->logger : $ this ->logger ;
273
+ }
252
274
}
0 commit comments