diff --git a/CHANGELOG.md b/CHANGELOG.md index 96efe9d0..e9228f66 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,9 @@ ## 3.5.0 (xxxx-xx-xx) +* Added support for Monolog 2.0 * Added `sentry` type to use sentry 2.0 client * Added `insightops` handler -* Added support for Monolog 2.0 +* Added possibility for auto-wire monolog channel according to the type-hinted aliases, introduced in the Symfony 4.2 ## 3.4.0 (2019-06-20) diff --git a/DependencyInjection/Compiler/LoggerChannelPass.php b/DependencyInjection/Compiler/LoggerChannelPass.php index 1ed68aed..d10d41a2 100644 --- a/DependencyInjection/Compiler/LoggerChannelPass.php +++ b/DependencyInjection/Compiler/LoggerChannelPass.php @@ -11,13 +11,13 @@ namespace Symfony\Bundle\MonologBundle\DependencyInjection\Compiler; +use Psr\Log\LoggerInterface; use Symfony\Component\DependencyInjection\Argument\BoundArgument; -use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\DependencyInjection\ChildDefinition; -use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; -use Symfony\Component\DependencyInjection\DefinitionDecorator; +use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; +use Symfony\Component\DependencyInjection\Reference; /** * Replaces the default logger by another one with its own channel for tagged services. @@ -28,6 +28,9 @@ class LoggerChannelPass implements CompilerPassInterface { protected $channels = ['app']; + /** + * {@inheritDoc} + */ public function process(ContainerBuilder $container) { if (!$container->hasDefinition('monolog.logger')) { @@ -105,11 +108,19 @@ public function process(ContainerBuilder $container) } } + /** + * @return array + */ public function getChannels() { return $this->channels; } + /** + * @param array $configuration + * + * @return array + */ protected function processChannels($configuration) { if (null === $configuration) { @@ -123,6 +134,13 @@ protected function processChannels($configuration) return array_diff($this->channels, $configuration['elements']); } + /** + * Create new logger from the monolog.logger_prototype + * + * @param string $channel + * @param string $loggerId + * @param ContainerBuilder $container + */ protected function createLogger($channel, $loggerId, ContainerBuilder $container) { if (!in_array($channel, $this->channels)) { @@ -131,6 +149,13 @@ protected function createLogger($channel, $loggerId, ContainerBuilder $container $container->setDefinition($loggerId, $logger); $this->channels[] = $channel; } + + // Allows only for Symfony 4.2+ + if (\method_exists($container, 'registerAliasForArgument')) { + $parameterName = $channel . 'Logger'; + + $container->registerAliasForArgument($loggerId, LoggerInterface::class, $parameterName); + } } /** diff --git a/Tests/DependencyInjection/Compiler/LoggerChannelPassTest.php b/Tests/DependencyInjection/Compiler/LoggerChannelPassTest.php index eeaedc8c..7cc7323c 100644 --- a/Tests/DependencyInjection/Compiler/LoggerChannelPassTest.php +++ b/Tests/DependencyInjection/Compiler/LoggerChannelPassTest.php @@ -13,12 +13,12 @@ use PHPUnit\Framework\TestCase; use Psr\Log\LoggerInterface; -use Symfony\Component\DependencyInjection\Reference; -use Symfony\Component\DependencyInjection\Definition; -use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Bundle\MonologBundle\DependencyInjection\Compiler\LoggerChannelPass; use Symfony\Component\Config\FileLocator; +use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Definition; use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; +use Symfony\Component\DependencyInjection\Reference; class LoggerChannelPassTest extends TestCase { @@ -51,7 +51,22 @@ public function testProcess() } } - $this->assertNotNull($container->getDefinition('monolog.logger.manualchan')); + $this->assertNotNull($container->getDefinition('monolog.logger.additional')); + } + + public function testTypeHintedAliasesExistForEachChannel() + { + if (!\method_exists(ContainerBuilder::class, 'registerAliasForArgument')) { + $this->markTestSkipped('Need DependencyInjection 4.2+ to register type-hinted aliases for channels.'); + } + + $container = $this->getContainer(); + $expectedChannels = ['test', 'foo', 'bar', 'additional']; + + foreach ($expectedChannels as $channelName) { + $aliasName = LoggerInterface::class.' $' .$channelName.'Logger'; + $this->assertTrue($container->hasAlias($aliasName), 'type-hinted alias should be exists for each logger channel'); + } } public function testProcessSetters() @@ -166,7 +181,7 @@ private function getContainer() $container->setDefinition($name, $service); } - $container->setParameter('monolog.additional_channels', ['manualchan']); + $container->setParameter('monolog.additional_channels', ['additional']); $container->setParameter('monolog.handlers_to_channels', [ 'monolog.handler.a' => [ 'type' => 'inclusive', @@ -202,7 +217,7 @@ private function getContainerWithSetter() $service->addMethodCall('setLogger', [new Reference('logger')]); $container->setDefinition('foo', $service); - $container->setParameter('monolog.additional_channels', ['manualchan']); + $container->setParameter('monolog.additional_channels', ['additional']); $container->setParameter('monolog.handlers_to_channels', []); $container->getCompilerPassConfig()->setOptimizationPasses([]);