Skip to content

Commit

Permalink
feature #446 Resolve "level" env var at runtime (GromNaN)
Browse files Browse the repository at this point in the history
This PR was merged into the 3.x-dev branch.

Discussion
----------

Resolve "level" env var at runtime

Resolves #413, #444

Currently, the log level is read when the container built. This means we cannot change a log level at runtime using env vars.

With this change, log level can be modified for a specific command run: `LOG_LEVEL=debug bin/console ...`

~I would like to get some feedbacks before updating tests. I don't know yet what is the best strategy to check the resolution of the values.~

Commits
-------

db45663 Resolve levels at runtime using Monolog's native behavior.
  • Loading branch information
GromNaN committed Dec 5, 2022
2 parents a41bbcd + db45663 commit aca8fb9
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 94 deletions.
40 changes: 0 additions & 40 deletions DependencyInjection/MonologExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
Expand All @@ -47,33 +46,6 @@ class MonologExtension extends Extension

private $swiftMailerHandlers = [];

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 ($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);
}

throw new \InvalidArgumentException(sprintf('Could not match "%s" to a log level.', $level));
}

/**
* Loads the Monolog configuration.
*
Expand Down Expand Up @@ -198,8 +170,6 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler
$handlerClass = $this->getHandlerClassByType($handler['type']);
$definition = new Definition($handlerClass);

$handler['level'] = $this->levelToMonologConst($handler['level'], $container);

if ($handler['include_stacktraces']) {
$definition->setConfigurator(['Symfony\\Bundle\\MonologBundle\\MonologBundle', 'includeStacktraces']);
}
Expand Down Expand Up @@ -433,10 +403,6 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler
break;

case 'fingers_crossed':
$handler['action_level'] = $this->levelToMonologConst($handler['action_level'], $container);
if (null !== $handler['passthru_level']) {
$handler['passthru_level'] = $this->levelToMonologConst($handler['passthru_level'], $container);
}
$nestedHandlerId = $this->getHandlerId($handler['handler']);
$this->markNestedHandler($nestedHandlerId);

Expand Down Expand Up @@ -482,12 +448,6 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler
break;

case 'filter':
$handler['min_level'] = $this->levelToMonologConst($handler['min_level'], $container);
$handler['max_level'] = $this->levelToMonologConst($handler['max_level'], $container);
foreach (array_keys($handler['accepted_levels']) as $k) {
$handler['accepted_levels'][$k] = $this->levelToMonologConst($handler['accepted_levels'][$k], $container);
}

$nestedHandlerId = $this->getHandlerId($handler['handler']);
$this->markNestedHandler($nestedHandlerId);
$minLevelOrList = !empty($handler['accepted_levels']) ? $handler['accepted_levels'] : $handler['min_level'];
Expand Down
1 change: 1 addition & 0 deletions Tests/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ public function testMergingInvalidChannels()
$config = $this->process($configs);
}

/** @group legacy */
public function testWithSwiftMailerHandler()
{
if (\Monolog\Logger::API >= 3) {
Expand Down
26 changes: 13 additions & 13 deletions Tests/DependencyInjection/FixtureMonologExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function testLegacyLoadWithSeveralHandlers()
$this->markTestSkipped('Symfony MonologBridge < 5.2 is needed.');
}

$this->doTestLoadWithSeveralHandlers(\Monolog\Logger::ERROR);
$this->doTestLoadWithSeveralHandlers('ERROR');
}

public function testLoadWithSeveralHandlers()
Expand All @@ -38,7 +38,7 @@ public function testLoadWithSeveralHandlers()
$this->markTestSkipped('Symfony MonologBridge >= 5.2 is needed.');
}

$this->doTestLoadWithSeveralHandlers(new Definition(ErrorLevelActivationStrategy::class, [\Monolog\Logger::ERROR]));
$this->doTestLoadWithSeveralHandlers(new Definition(ErrorLevelActivationStrategy::class, ['ERROR']));
}

private function doTestLoadWithSeveralHandlers($activation)
Expand All @@ -59,15 +59,15 @@ private function doTestLoadWithSeveralHandlers($activation)

$handler = $container->getDefinition('monolog.handler.custom');
$this->assertDICDefinitionClass($handler, 'Monolog\Handler\StreamHandler');
$this->assertDICConstructorArguments($handler, ['/tmp/symfony.log', \Monolog\Logger::ERROR, false, 0666, false]);
$this->assertDICConstructorArguments($handler, ['/tmp/symfony.log', 'ERROR', false, 0666, false]);

$handler = $container->getDefinition('monolog.handler.main');
$this->assertDICDefinitionClass($handler, 'Monolog\Handler\FingersCrossedHandler');
$this->assertDICConstructorArguments($handler, [new Reference('monolog.handler.nested'), $activation, 0, true, true, \Monolog\Logger::NOTICE]);
$this->assertDICConstructorArguments($handler, [new Reference('monolog.handler.nested'), $activation, 0, true, true, 'NOTICE']);

$handler = $container->getDefinition('monolog.handler.filtered');
$this->assertDICDefinitionClass($handler, 'Monolog\Handler\FilterHandler');
$this->assertDICConstructorArguments($handler, [new Reference('monolog.handler.nested2'), [\Monolog\Logger::WARNING, \Monolog\Logger::ERROR], \Monolog\Logger::EMERGENCY, true]);
$this->assertDICConstructorArguments($handler, [new Reference('monolog.handler.nested2'), ['WARNING', 'ERROR'], 'EMERGENCY', true]);
}

/** @group legacy */
Expand All @@ -77,7 +77,7 @@ public function testLegacyLoadWithOverwriting()
$this->markTestSkipped('Symfony MonologBridge < 5.2 is needed.');
}

$this->doTestLoadWithOverwriting(\Monolog\Logger::ERROR);
$this->doTestLoadWithOverwriting('ERROR');
}

public function testLoadWithOverwriting()
Expand All @@ -86,7 +86,7 @@ public function testLoadWithOverwriting()
$this->markTestSkipped('Symfony MonologBridge >= 5.2 is needed.');
}

$this->doTestLoadWithOverwriting(new Definition(ErrorLevelActivationStrategy::class, [\Monolog\Logger::ERROR]));
$this->doTestLoadWithOverwriting(new Definition(ErrorLevelActivationStrategy::class, ['ERROR']));
}

private function doTestLoadWithOverwriting($activation)
Expand All @@ -106,7 +106,7 @@ private function doTestLoadWithOverwriting($activation)

$handler = $container->getDefinition('monolog.handler.custom');
$this->assertDICDefinitionClass($handler, 'Monolog\Handler\StreamHandler');
$this->assertDICConstructorArguments($handler, ['/tmp/symfony.log', \Monolog\Logger::WARNING, true, null, false]);
$this->assertDICConstructorArguments($handler, ['/tmp/symfony.log', 'WARNING', true, null, false]);

$handler = $container->getDefinition('monolog.handler.main');
$this->assertDICDefinitionClass($handler, 'Monolog\Handler\FingersCrossedHandler');
Expand All @@ -132,7 +132,7 @@ public function testLoadWithNewAtEnd()

$handler = $container->getDefinition('monolog.handler.new');
$this->assertDICDefinitionClass($handler, 'Monolog\Handler\StreamHandler');
$this->assertDICConstructorArguments($handler, ['/tmp/monolog.log', \Monolog\Logger::ERROR, true, null, false]);
$this->assertDICConstructorArguments($handler, ['/tmp/monolog.log', 'ERROR', true, null, false]);
}

public function testLoadWithNewAndPriority()
Expand All @@ -156,15 +156,15 @@ public function testLoadWithNewAndPriority()

$handler = $container->getDefinition('monolog.handler.main');
$this->assertDICDefinitionClass($handler, 'Monolog\Handler\BufferHandler');
$this->assertDICConstructorArguments($handler, [new Reference('monolog.handler.nested'), 0, \Monolog\Logger::INFO, true, false]);
$this->assertDICConstructorArguments($handler, [new Reference('monolog.handler.nested'), 0, 'INFO', true, false]);

$handler = $container->getDefinition('monolog.handler.first');
$this->assertDICDefinitionClass($handler, 'Monolog\Handler\RotatingFileHandler');
$this->assertDICConstructorArguments($handler, ['/tmp/monolog.log', 0, \Monolog\Logger::ERROR, true, null, false]);
$this->assertDICConstructorArguments($handler, ['/tmp/monolog.log', 0, 'ERROR', true, null, false]);

$handler = $container->getDefinition('monolog.handler.last');
$this->assertDICDefinitionClass($handler, 'Monolog\Handler\StreamHandler');
$this->assertDICConstructorArguments($handler, ['/tmp/last.log', \Monolog\Logger::ERROR, true, null, false]);
$this->assertDICConstructorArguments($handler, ['/tmp/last.log', 'ERROR', true, null, false]);
}

public function testHandlersWithChannels()
Expand Down Expand Up @@ -209,7 +209,7 @@ public function testServerLog()

$this->assertEquals([
'0:9911',
100,
'DEBUG',
true,
], $container->getDefinition('monolog.handler.server_log')->getArguments());
}
Expand Down
Loading

0 comments on commit aca8fb9

Please sign in to comment.