Skip to content

Commit

Permalink
Postpone exception with multiple found LoggerInterfaces to autowire
Browse files Browse the repository at this point in the history
  • Loading branch information
finwe committed Feb 20, 2024
1 parent dd1d4eb commit 241e901
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/DI/Pass/LoggerPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Contributte\Messenger\Logger\MessengerLogger;
use Nette\DI\Definitions\ServiceDefinition;
use Nette\DI\Definitions\Statement;
use Nette\DI\ServiceCreationException;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Symfony\Component\Console\Logger\ConsoleLogger;
Expand Down Expand Up @@ -34,13 +35,21 @@ public function beforePassCompile(): void
$builder = $this->getContainerBuilder();
$config = $this->getConfig();

$logger = $builder->getByType(LoggerInterface::class);
$multiple = null;
try {
$logger = $builder->getByType(LoggerInterface::class, false);
} catch (ServiceCreationException $e) {
// multiple loggers
$multiple = $e;
}

// Register or resolve http logger
if ($config->logger->httpLogger !== null) {
$httpLogger = $builder->addDefinition($this->prefix('logger.httpLogger'))
->setFactory($config->logger->httpLogger)
->setAutowired(false);
} elseif ($multiple !== null) {
throw $e;
} elseif ($logger !== null) {
$httpLogger = $builder->addDefinition($this->prefix('logger.httpLogger'))
->setFactory('@' . $logger)
Expand All @@ -56,6 +65,8 @@ public function beforePassCompile(): void
$consoleLogger = $builder->addDefinition($this->prefix('logger.consoleLogger'))
->setFactory($config->logger->consoleLogger)
->setAutowired(false);
} elseif ($multiple !== null) {
throw $e;
} elseif ($logger !== null) {
$consoleLogger = $builder->addDefinition($this->prefix('logger.consoleLogger'))
->setFactory('@' . $logger)
Expand Down

0 comments on commit 241e901

Please sign in to comment.