diff --git a/DependencyInjection/MonologExtension.php b/DependencyInjection/MonologExtension.php index 3230f44e..10533cad 100644 --- a/DependencyInjection/MonologExtension.php +++ b/DependencyInjection/MonologExtension.php @@ -24,6 +24,7 @@ use Symfony\Bridge\Monolog\Processor\TokenProcessor; use Symfony\Bridge\Monolog\Processor\WebProcessor; use Symfony\Bundle\FullStack; +use Symfony\Bundle\MonologBundle\MonologBundle; use Symfony\Component\Config\FileLocator; use Symfony\Component\DependencyInjection\Argument\BoundArgument; use Symfony\Component\DependencyInjection\ChildDefinition; @@ -50,42 +51,19 @@ class MonologExtension extends Extension private function levelToMonologConst($level, ContainerBuilder $container) { - if (null === $level || is_numeric($level)) { - return $level; - } - - if (defined('Monolog\Logger::'.strtoupper($level))) { - return constant('Monolog\Logger::' . strtoupper($level)); - } - - if (function_exists('enum_exists') && enum_exists(Level::class)) { - $factoryName = 'monolog.level.' . md5($level); - - if (!$container->has($factoryName)) { - $container->register($factoryName) - ->setClass(Level::class) - ->setFactory([Level::class, 'fromName']) + if (is_string($level) && str_contains($level, '%')) { + $serviceId = 'monolog.level.' . md5($level); + if (!$container->has($serviceId)) { + $container->register($serviceId) + ->setClass('int') + ->setFactory([MonologBundle::class, 'levelToMonologConst']) ->setArguments([$level]); } - return new Reference($factoryName); - } - - if ($container->hasParameter($level)) { - return $this->levelToMonologConst($container->getParameter($level), $container); - } - - try { - $logLevel = $container->resolveEnvPlaceholders($level, true); - } catch (ParameterNotFoundException $notFoundException) { - throw new \InvalidArgumentException(sprintf('Could not match "%s" to a log level.', $level)); - } - - if ($logLevel !== '' && $logLevel !== $level) { - return $this->levelToMonologConst($logLevel, $container); + return new Reference($serviceId); } - throw new \InvalidArgumentException(sprintf('Could not match "%s" to a log level.', $level)); + return MonologBundle::levelToMonologConst($level); } /** diff --git a/MonologBundle.php b/MonologBundle.php index 12e0cb67..371430af 100644 --- a/MonologBundle.php +++ b/MonologBundle.php @@ -14,6 +14,7 @@ use Monolog\Formatter\JsonFormatter; use Monolog\Formatter\LineFormatter; use Monolog\Handler\HandlerInterface; +use Monolog\Level; use Symfony\Bundle\MonologBundle\DependencyInjection\Compiler\AddSwiftMailerTransportPass; use Symfony\Component\HttpKernel\Bundle\Bundle; use Symfony\Component\DependencyInjection\ContainerBuilder; @@ -50,4 +51,20 @@ public static function includeStacktraces(HandlerInterface $handler) $formatter->includeStacktraces(); } } + + /** + * @internal + */ + public static function levelToMonologConst($level) + { + if (null === $level || is_numeric($level)) { + return $level; + } + + if (defined('Monolog\Logger::'.strtoupper($level))) { + return constant('Monolog\Logger::' . strtoupper($level)); + } + + throw new \InvalidArgumentException(sprintf('Could not match "%s" to a log level.', $level)); + } }