From 066974536627c40612b6a777920498552fb1d501 Mon Sep 17 00:00:00 2001 From: Christophe Coevoet Date: Tue, 24 Oct 2023 11:33:35 +0200 Subject: [PATCH] Add support for the WithMonologChannel attribute of Monolog 3.5 --- CHANGELOG.md | 1 + DependencyInjection/MonologExtension.php | 4 ++++ .../Fixtures/ServiceWithChannel.php | 19 +++++++++++++++ .../MonologExtensionTest.php | 23 ++++++++++++++++++- 4 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 Tests/DependencyInjection/Fixtures/ServiceWithChannel.php diff --git a/CHANGELOG.md b/CHANGELOG.md index bc313647..9ebbe76d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ ## Unreleased +* Add support for the `WithMonologChannel` attribute of Monolog 3.5.0 to autoconfigure the `monolog.logger` tag * Add support for Symfony 7 * Remove support for Symfony 4 * Add support for env placeholders in the `level` option of handlers diff --git a/DependencyInjection/MonologExtension.php b/DependencyInjection/MonologExtension.php index 5d14c430..db000b52 100644 --- a/DependencyInjection/MonologExtension.php +++ b/DependencyInjection/MonologExtension.php @@ -12,6 +12,7 @@ namespace Symfony\Bundle\MonologBundle\DependencyInjection; use Monolog\Attribute\AsMonologProcessor; +use Monolog\Attribute\WithMonologChannel; use Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy; use Monolog\Handler\HandlerInterface; use Monolog\Logger; @@ -135,6 +136,9 @@ public function load(array $configs, ContainerBuilder $container) $definition->addTag('monolog.processor', $tagAttributes); }); + $container->registerAttributeForAutoconfiguration(WithMonologChannel::class, static function (ChildDefinition $definition, WithMonologChannel $attribute): void { + $definition->addTag('monolog.logger', ['channel' => $attribute->channel]); + }); } } diff --git a/Tests/DependencyInjection/Fixtures/ServiceWithChannel.php b/Tests/DependencyInjection/Fixtures/ServiceWithChannel.php new file mode 100644 index 00000000..b57eae80 --- /dev/null +++ b/Tests/DependencyInjection/Fixtures/ServiceWithChannel.php @@ -0,0 +1,19 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bundle\MonologBundle\Tests\DependencyInjection\Fixtures; + +use Monolog\Attribute\WithMonologChannel; + +#[WithMonologChannel('fixture')] +class ServiceWithChannel +{ +} diff --git a/Tests/DependencyInjection/MonologExtensionTest.php b/Tests/DependencyInjection/MonologExtensionTest.php index 29ef6a15..2888c686 100644 --- a/Tests/DependencyInjection/MonologExtensionTest.php +++ b/Tests/DependencyInjection/MonologExtensionTest.php @@ -13,16 +13,17 @@ use InvalidArgumentException; use Monolog\Attribute\AsMonologProcessor; +use Monolog\Attribute\WithMonologChannel; use Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy; use Monolog\Handler\RollbarHandler; use Monolog\Logger; use Monolog\Processor\UidProcessor; -use Symfony\Bridge\Monolog\Processor\SwitchUserTokenProcessor; use Symfony\Bundle\MonologBundle\DependencyInjection\MonologExtension; use Symfony\Bundle\MonologBundle\DependencyInjection\Compiler\LoggerChannelPass; use Symfony\Bundle\MonologBundle\Tests\DependencyInjection\Fixtures\AsMonologProcessor\FooProcessor; use Symfony\Bundle\MonologBundle\Tests\DependencyInjection\Fixtures\AsMonologProcessor\FooProcessorWithPriority; use Symfony\Bundle\MonologBundle\Tests\DependencyInjection\Fixtures\AsMonologProcessor\RedeclareMethodProcessor; +use Symfony\Bundle\MonologBundle\Tests\DependencyInjection\Fixtures\ServiceWithChannel; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Definition; use Symfony\Component\DependencyInjection\ParameterBag\EnvPlaceholderParameterBag; @@ -837,6 +838,26 @@ public function testAsMonologProcessorAutoconfigurationWithPriority(): void ], $container->getDefinition(FooProcessorWithPriority::class)->getTag('monolog.processor')); } + /** + * @requires PHP 8.0 + */ + public function testWithLoggerChannelAutoconfiguration(): void + { + if (!class_exists(WithMonologChannel::class)) { + $this->markTestSkipped('Monolog >= 3.5.0 is needed.'); + } + + $container = $this->getContainer([], [ + ServiceWithChannel::class => (new Definition(ServiceWithChannel::class))->setAutoconfigured(true), + ]); + + $this->assertSame([ + [ + 'channel' => 'fixture', + ], + ], $container->getDefinition(ServiceWithChannel::class)->getTag('monolog.logger')); + } + protected function getContainer(array $config = [], array $thirdPartyDefinitions = []): ContainerBuilder { $container = new ContainerBuilder(new EnvPlaceholderParameterBag());