diff --git a/CHANGELOG.md b/CHANGELOG.md index ce5af3de..6dbb3866 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 * Mark classes as internal when relevant diff --git a/DependencyInjection/MonologExtension.php b/DependencyInjection/MonologExtension.php index ba7c09b6..a20b558a 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; @@ -137,6 +138,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());