diff --git a/src/DependencyInjection/Compiler/LoggerChannelPass.php b/src/DependencyInjection/Compiler/LoggerChannelPass.php index 7172aaf4..c144ac97 100644 --- a/src/DependencyInjection/Compiler/LoggerChannelPass.php +++ b/src/DependencyInjection/Compiler/LoggerChannelPass.php @@ -28,15 +28,15 @@ */ class LoggerChannelPass implements CompilerPassInterface { - /** @var list */ - protected array $channels = ['app']; - public function process(ContainerBuilder $container): void { if (!$container->hasDefinition('monolog.logger')) { return; } + /** @var list $createdLoggers */ + $createdLoggers = ['app']; + // create channels necessary for the handlers foreach ($container->findTaggedServiceIds('monolog.logger') as $id => $tags) { foreach ($tags as $tag) { @@ -48,7 +48,7 @@ public function process(ContainerBuilder $container): void $definition = $container->getDefinition($id); $loggerId = \sprintf('monolog.logger.%s', $resolvedChannel); - $this->createLogger($resolvedChannel, $loggerId, $container); + $this->createLogger($resolvedChannel, $loggerId, $container, $createdLoggers); foreach ($definition->getArguments() as $index => $argument) { if ($argument instanceof Reference && 'logger' === (string) $argument) { @@ -86,7 +86,7 @@ public function process(ContainerBuilder $container): void continue; } $loggerId = \sprintf('monolog.logger.%s', $chan); - $this->createLogger($chan, $loggerId, $container); + $this->createLogger($chan, $loggerId, $container, $createdLoggers); $container->getDefinition($loggerId)->setPublic(true); } $container->getParameterBag()->remove('monolog.additional_channels'); @@ -94,7 +94,7 @@ public function process(ContainerBuilder $container): void // wire handlers to channels $handlersToChannels = $container->getParameter('monolog.handlers_to_channels'); foreach ($handlersToChannels as $handler => $channels) { - foreach ($this->processChannels($channels) as $channel) { + foreach ($this->processChannels($channels, $createdLoggers) as $channel) { try { $logger = $container->getDefinition('app' === $channel ? 'monolog.logger' : 'monolog.logger.'.$channel); } catch (InvalidArgumentException $e) { @@ -105,37 +105,29 @@ public function process(ContainerBuilder $container): void } } - /** - * @return list - */ - public function getChannels(): array - { - return $this->channels; - } - - protected function processChannels(?array $configuration): array + protected function processChannels(?array $configuration, array $createdLoggers): array { if (null === $configuration) { - return $this->channels; + return $createdLoggers; } if ('inclusive' === $configuration['type']) { - return $configuration['elements'] ?: $this->channels; + return $configuration['elements'] ?: $createdLoggers; } - return array_diff($this->channels, $configuration['elements']); + return array_diff($createdLoggers, $configuration['elements']); } /** * Create new logger from the monolog.logger_prototype. */ - protected function createLogger(string $channel, string $loggerId, ContainerBuilder $container): void + protected function createLogger(string $channel, string $loggerId, ContainerBuilder $container, array &$createdLoggers): void { - if (!\in_array($channel, $this->channels, true)) { + if (!\in_array($channel, $createdLoggers, true)) { $logger = new ChildDefinition('monolog.logger_prototype'); $logger->replaceArgument(0, $channel); $container->setDefinition($loggerId, $logger); - $this->channels[] = $channel; + $createdLoggers[] = $channel; } $container->registerAliasForArgument($loggerId, LoggerInterface::class, $channel.'.logger');