Skip to content

Commit

Permalink
Resolve level variables at runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN committed Nov 26, 2022
1 parent 2f92e74 commit 319a920
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 31 deletions.
40 changes: 9 additions & 31 deletions DependencyInjection/MonologExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}

/**
Expand Down
17 changes: 17 additions & 0 deletions MonologBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
}

0 comments on commit 319a920

Please sign in to comment.