Skip to content

Commit 98c2313

Browse files
Arkalo2nicolas-grekas
authored andcommitted
[FrameworkBundle][HttpKernel] Allow configuring the logging channel per type of exceptions
1 parent fd38187 commit 98c2313

File tree

5 files changed

+24
-1
lines changed

5 files changed

+24
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ CHANGELOG
1717
* Auto-exclude DI extensions, test cases, entities and messenger messages
1818
* Add DI alias from `ServicesResetterInterface` to `services_resetter`
1919
* Add `methods` argument in `#[IsCsrfTokenValid]` attribute
20+
* Allow configuring the logging channel per type of exceptions
2021

2122
7.2
2223
---

DependencyInjection/Configuration.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1458,6 +1458,10 @@ private function addExceptionsSection(ArrayNodeDefinition $rootNode): void
14581458
->end()
14591459
->defaultNull()
14601460
->end()
1461+
->scalarNode('log_channel')
1462+
->info('The channel of log message. Null to let Symfony decide.')
1463+
->defaultNull()
1464+
->end()
14611465
->end()
14621466
->end()
14631467
->end()

DependencyInjection/FrameworkExtension.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,20 @@ public function load(array $configs, ContainerBuilder $container): void
417417
$this->registerPropertyAccessConfiguration($config['property_access'], $container, $loader);
418418
$this->registerSecretsConfiguration($config['secrets'], $container, $loader, $config['secret'] ?? null);
419419

420-
$container->getDefinition('exception_listener')->replaceArgument(3, $config['exceptions']);
420+
$exceptionListener = $container->getDefinition('exception_listener');
421+
422+
$loggers = [];
423+
foreach ($config['exceptions'] as $exception) {
424+
if (!isset($exception['log_channel'])) {
425+
continue;
426+
}
427+
$loggers[$exception['log_channel']] = new Reference('monolog.logger.'.$exception['log_channel'], ContainerInterface::NULL_ON_INVALID_REFERENCE);
428+
}
429+
430+
$exceptionListener
431+
->replaceArgument(3, $config['exceptions'])
432+
->setArgument(4, $loggers)
433+
;
421434

422435
if ($this->readConfigEnabled('serializer', $container, $config['serializer'])) {
423436
if (!class_exists(Serializer::class)) {

Resources/config/web.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@
138138
service('logger')->nullOnInvalid(),
139139
param('kernel.debug'),
140140
abstract_arg('an exceptions to log & status code mapping'),
141+
abstract_arg('list of loggers by log_channel'),
141142
])
142143
->tag('kernel.event_subscriber')
143144
->tag('monolog.logger', ['channel' => 'request'])

Tests/DependencyInjection/FrameworkExtensionTestCase.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,21 +615,25 @@ public function testExceptionsConfig()
615615
], array_keys($configuration));
616616

617617
$this->assertEqualsCanonicalizing([
618+
'log_channel' => null,
618619
'log_level' => 'info',
619620
'status_code' => 422,
620621
], $configuration[\Symfony\Component\HttpKernel\Exception\BadRequestHttpException::class]);
621622

622623
$this->assertEqualsCanonicalizing([
624+
'log_channel' => null,
623625
'log_level' => 'info',
624626
'status_code' => null,
625627
], $configuration[\Symfony\Component\HttpKernel\Exception\NotFoundHttpException::class]);
626628

627629
$this->assertEqualsCanonicalizing([
630+
'log_channel' => null,
628631
'log_level' => 'info',
629632
'status_code' => null,
630633
], $configuration[\Symfony\Component\HttpKernel\Exception\ConflictHttpException::class]);
631634

632635
$this->assertEqualsCanonicalizing([
636+
'log_channel' => null,
633637
'log_level' => null,
634638
'status_code' => 500,
635639
], $configuration[\Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException::class]);

0 commit comments

Comments
 (0)