Skip to content

Commit aca8fb9

Browse files
committed
feature #446 Resolve "level" env var at runtime (GromNaN)
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.
2 parents a41bbcd + db45663 commit aca8fb9

File tree

4 files changed

+40
-94
lines changed

4 files changed

+40
-94
lines changed

DependencyInjection/MonologExtension.php

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
use Symfony\Component\DependencyInjection\ChildDefinition;
2929
use Symfony\Component\DependencyInjection\ContainerBuilder;
3030
use Symfony\Component\DependencyInjection\Definition;
31-
use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException;
3231
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
3332
use Symfony\Component\DependencyInjection\Reference;
3433
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
@@ -47,33 +46,6 @@ class MonologExtension extends Extension
4746

4847
private $swiftMailerHandlers = [];
4948

50-
private function levelToMonologConst($level, ContainerBuilder $container)
51-
{
52-
if (null === $level || is_numeric($level)) {
53-
return $level;
54-
}
55-
56-
if (defined('Monolog\Logger::'.strtoupper($level))) {
57-
return constant('Monolog\Logger::' . strtoupper($level));
58-
}
59-
60-
if ($container->hasParameter($level)) {
61-
return $this->levelToMonologConst($container->getParameter($level), $container);
62-
}
63-
64-
try {
65-
$logLevel = $container->resolveEnvPlaceholders($level, true);
66-
} catch (ParameterNotFoundException $notFoundException) {
67-
throw new \InvalidArgumentException(sprintf('Could not match "%s" to a log level.', $level));
68-
}
69-
70-
if ($logLevel !== '' && $logLevel !== $level) {
71-
return $this->levelToMonologConst($logLevel, $container);
72-
}
73-
74-
throw new \InvalidArgumentException(sprintf('Could not match "%s" to a log level.', $level));
75-
}
76-
7749
/**
7850
* Loads the Monolog configuration.
7951
*
@@ -198,8 +170,6 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler
198170
$handlerClass = $this->getHandlerClassByType($handler['type']);
199171
$definition = new Definition($handlerClass);
200172

201-
$handler['level'] = $this->levelToMonologConst($handler['level'], $container);
202-
203173
if ($handler['include_stacktraces']) {
204174
$definition->setConfigurator(['Symfony\\Bundle\\MonologBundle\\MonologBundle', 'includeStacktraces']);
205175
}
@@ -433,10 +403,6 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler
433403
break;
434404

435405
case 'fingers_crossed':
436-
$handler['action_level'] = $this->levelToMonologConst($handler['action_level'], $container);
437-
if (null !== $handler['passthru_level']) {
438-
$handler['passthru_level'] = $this->levelToMonologConst($handler['passthru_level'], $container);
439-
}
440406
$nestedHandlerId = $this->getHandlerId($handler['handler']);
441407
$this->markNestedHandler($nestedHandlerId);
442408

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

484450
case 'filter':
485-
$handler['min_level'] = $this->levelToMonologConst($handler['min_level'], $container);
486-
$handler['max_level'] = $this->levelToMonologConst($handler['max_level'], $container);
487-
foreach (array_keys($handler['accepted_levels']) as $k) {
488-
$handler['accepted_levels'][$k] = $this->levelToMonologConst($handler['accepted_levels'][$k], $container);
489-
}
490-
491451
$nestedHandlerId = $this->getHandlerId($handler['handler']);
492452
$this->markNestedHandler($nestedHandlerId);
493453
$minLevelOrList = !empty($handler['accepted_levels']) ? $handler['accepted_levels'] : $handler['min_level'];

Tests/DependencyInjection/ConfigurationTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ public function testMergingInvalidChannels()
189189
$config = $this->process($configs);
190190
}
191191

192+
/** @group legacy */
192193
public function testWithSwiftMailerHandler()
193194
{
194195
if (\Monolog\Logger::API >= 3) {

Tests/DependencyInjection/FixtureMonologExtensionTest.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function testLegacyLoadWithSeveralHandlers()
2929
$this->markTestSkipped('Symfony MonologBridge < 5.2 is needed.');
3030
}
3131

32-
$this->doTestLoadWithSeveralHandlers(\Monolog\Logger::ERROR);
32+
$this->doTestLoadWithSeveralHandlers('ERROR');
3333
}
3434

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

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

4444
private function doTestLoadWithSeveralHandlers($activation)
@@ -59,15 +59,15 @@ private function doTestLoadWithSeveralHandlers($activation)
5959

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

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

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

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

80-
$this->doTestLoadWithOverwriting(\Monolog\Logger::ERROR);
80+
$this->doTestLoadWithOverwriting('ERROR');
8181
}
8282

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

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

9292
private function doTestLoadWithOverwriting($activation)
@@ -106,7 +106,7 @@ private function doTestLoadWithOverwriting($activation)
106106

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

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

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

138138
public function testLoadWithNewAndPriority()
@@ -156,15 +156,15 @@ public function testLoadWithNewAndPriority()
156156

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

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

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

170170
public function testHandlersWithChannels()
@@ -209,7 +209,7 @@ public function testServerLog()
209209

210210
$this->assertEquals([
211211
'0:9911',
212-
100,
212+
'DEBUG',
213213
true,
214214
], $container->getDefinition('monolog.handler.server_log')->getArguments());
215215
}

0 commit comments

Comments
 (0)