Repository: symfony/monolog-bundle (php)

Issue:
Setting log_level via environment variable requires cache clear
Hi, I'm trying to use the log_level from env vars implemented in #357 but I can't seem to get it to work without clearing the cache between command executions.

I'm using v3.7 of this bundle:

```
root@9e633aa0c868:/app# composer show symfony/monolog-bundle
name     : symfony/monolog-bundle
descrip. : Symfony MonologBundle
keywords : log, logging
versions : * v3.7.0
```

I have this configuration:

```yaml
# config/packages/probe/monolog.yaml
parameters:
    log_level: "%env(APP_LOG_LEVEL)%"

monolog:
    handlers:
        main:
            type: stream
            path: php://stderr
            level: '%log_level%'
```

And I'm testing with this command:

```php
<?php
declare(strict_types=1);

namespace App\Command;

use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

class TestCommand extends Command
{
    protected static $defaultName = 'app:test';
    private ContainerInterface $container;
    private LoggerInterface $logger;

    public function __construct(ContainerInterface $container, LoggerInterface $logger, string $name = null)
    {
        parent::__construct($name);
        $this->container = $container;
        $this->logger = $logger;
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $output->writeln('log_level: ' . $this->container->getParameter('log_level'));

        $this->logger->debug('debug');
        $this->logger->info('info');
        $this->logger->notice('notice');
        $this->logger->warning('warning');
        $this->logger->error('error');
        $this->logger->critical('critical');
        $this->logger->alert('alert');
        $this->logger->emergency('emergency');

        return Command::SUCCESS;
    }
}
```

I expected the APP_LOG_LEVEL to be able to change between subsequent runs of the command w/o clearing the cache, but that doesn't seem to work. As soon as I start the command with a valid value for log_level, I can never change it until I clear the cache.

This is also annoying when I already have a .env/.env.local file since it always seems to take that value.

In this example, I have APP_LOG_LEVEL=warning in the .env file.

```shell
root@9e633aa0c868:/app# APP_ENV=probe php bin/console app:test                  
log_level: warning
[2021-07-05T16:27:13.541340+00:00] app.WARNING: warning [] []
[2021-07-05T16:27:13.545269+00:00] app.ERROR: error [] []
[2021-07-05T16:27:13.547210+00:00] app.CRITICAL: critical [] []
[2021-07-05T16:27:13.548159+00:00] app.ALERT: alert [] []
[2021-07-05T16:27:13.549459+00:00] app.EMERGENCY: emergency [] []
root@9e633aa0c868:/app# APP_LOG_LEVEL=debug APP_ENV=probe php bin/console app:test
log_level: debug
[2021-07-05T16:27:17.861489+00:00] app.WARNING: warning [] []
[2021-07-05T16:27:17.864648+00:00] app.ERROR: error [] []
[2021-07-05T16:27:17.866875+00:00] app.CRITICAL: critical [] []
[2021-07-05T16:27:17.868456+00:00] app.ALERT: alert [] []
[2021-07-05T16:27:17.869008+00:00] app.EMERGENCY: emergency [] []
```

If I remove APP_LOG_LEVEL from .env *or* remove the cache and then restart my command passing APP_LOG_LEVEL=debug it'll use that value for all subsequent runs. It won't change until I clear:cache or remove the cache.

```shell
root@9e633aa0c868:/app# rm -rf var/cache/probe/*
root@9e633aa0c868:/app# APP_LOG_LEVEL=debug APP_ENV=probe php bin/console app:test                    
log_level: debug
[2021-07-05T16:28:58.107361+00:00] app.DEBUG: debug [] []
[2021-07-05T16:28:58.107665+00:00] app.INFO: info [] []
[2021-07-05T16:28:58.107821+00:00] app.NOTICE: notice [] []
[2021-07-05T16:28:58.107951+00:00] app.WARNING: warning [] []
[2021-07-05T16:28:58.108081+00:00] app.ERROR: error [] []
[2021-07-05T16:28:58.108239+00:00] app.CRITICAL: critical [] []
[2021-07-05T16:28:58.109731+00:00] app.ALERT: alert [] []
[2021-07-05T16:28:58.110578+00:00] app.EMERGENCY: emergency [] []
```

Am I doing something wrong or is this intended behavior?

Pull request:
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.~

Patch:
diff --git a/DependencyInjection/MonologExtension.php b/DependencyInjection/MonologExtension.php
index 7d76671..0c5bee2 100644
--- a/DependencyInjection/MonologExtension.php
+++ b/DependencyInjection/MonologExtension.php
@@ -28,7 +28,6 @@ use Symfony\Component\DependencyInjection\Argument\BoundArgument;
 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;
@@ -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.
      *
@@ -198,8 +170,6 @@ class MonologExtension extends Extension
         $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']);
         }
@@ -433,10 +403,6 @@ class MonologExtension extends Extension
             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);
 
@@ -482,12 +448,6 @@ class MonologExtension extends Extension
             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'];


Test patch:
diff --git a/Tests/DependencyInjection/ConfigurationTest.php b/Tests/DependencyInjection/ConfigurationTest.php
index c9b6a23..3884736 100644
--- a/Tests/DependencyInjection/ConfigurationTest.php
+++ b/Tests/DependencyInjection/ConfigurationTest.php
@@ -189,6 +189,7 @@ class ConfigurationTest extends TestCase
         $config = $this->process($configs);
     }
 
+    /** @group legacy */
     public function testWithSwiftMailerHandler()
     {
         if (\Monolog\Logger::API >= 3) {
diff --git a/Tests/DependencyInjection/FixtureMonologExtensionTest.php b/Tests/DependencyInjection/FixtureMonologExtensionTest.php
index 3cbb192..3384113 100644
--- a/Tests/DependencyInjection/FixtureMonologExtensionTest.php
+++ b/Tests/DependencyInjection/FixtureMonologExtensionTest.php
@@ -29,7 +29,7 @@ abstract class FixtureMonologExtensionTest extends DependencyInjectionTest
             $this->markTestSkipped('Symfony MonologBridge < 5.2 is needed.');
         }
 
-        $this->doTestLoadWithSeveralHandlers(\Monolog\Logger::ERROR);
+        $this->doTestLoadWithSeveralHandlers('ERROR');
     }
 
     public function testLoadWithSeveralHandlers()
@@ -38,7 +38,7 @@ abstract class FixtureMonologExtensionTest extends DependencyInjectionTest
             $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)
@@ -59,15 +59,15 @@ abstract class FixtureMonologExtensionTest extends DependencyInjectionTest
 
         $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 */
@@ -77,7 +77,7 @@ abstract class FixtureMonologExtensionTest extends DependencyInjectionTest
             $this->markTestSkipped('Symfony MonologBridge < 5.2 is needed.');
         }
 
-        $this->doTestLoadWithOverwriting(\Monolog\Logger::ERROR);
+        $this->doTestLoadWithOverwriting('ERROR');
     }
 
     public function testLoadWithOverwriting()
@@ -86,7 +86,7 @@ abstract class FixtureMonologExtensionTest extends DependencyInjectionTest
             $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)
@@ -106,7 +106,7 @@ abstract class FixtureMonologExtensionTest extends DependencyInjectionTest
 
         $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');
@@ -132,7 +132,7 @@ abstract class FixtureMonologExtensionTest extends DependencyInjectionTest
 
         $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()
@@ -156,15 +156,15 @@ abstract class FixtureMonologExtensionTest extends DependencyInjectionTest
 
         $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()
@@ -209,7 +209,7 @@ abstract class FixtureMonologExtensionTest extends DependencyInjectionTest
 
         $this->assertEquals([
             '0:9911',
-            100,
+            'DEBUG',
             true,
         ], $container->getDefinition('monolog.handler.server_log')->getArguments());
     }
diff --git a/Tests/DependencyInjection/MonologExtensionTest.php b/Tests/DependencyInjection/MonologExtensionTest.php
index 52307b9..4ff3cf9 100644
--- a/Tests/DependencyInjection/MonologExtensionTest.php
+++ b/Tests/DependencyInjection/MonologExtensionTest.php
@@ -44,7 +44,7 @@ class MonologExtensionTest extends DependencyInjectionTest
 
         $handler = $container->getDefinition('monolog.handler.main');
         $this->assertDICDefinitionClass($handler, 'Monolog\Handler\StreamHandler');
-        $this->assertDICConstructorArguments($handler, ['%kernel.logs_dir%/%kernel.environment%.log', \Monolog\Logger::DEBUG, true, null, false]);
+        $this->assertDICConstructorArguments($handler, ['%kernel.logs_dir%/%kernel.environment%.log', 'DEBUG', true, null, false]);
         $this->assertDICDefinitionMethodCallAt(0, $handler, 'pushProcessor', [new Reference('monolog.processor.psr_log_message')]);
     }
 
@@ -62,16 +62,7 @@ class MonologExtensionTest extends DependencyInjectionTest
 
         $handler = $container->getDefinition('monolog.handler.custom');
         $this->assertDICDefinitionClass($handler, 'Monolog\Handler\StreamHandler');
-        $this->assertDICConstructorArguments($handler, ['/tmp/symfony.log', \Monolog\Logger::ERROR, false, 0666, true]);
-    }
-
-    public function testLoadWithUnknownLevel()
-    {
-        $this->expectException(InvalidArgumentException::class);
-        $this->expectExceptionMessage('Could not match "warn" to a log level.');
-        $container = $this->getContainer([['handlers' => [
-            'custom' => ['type' => 'stream', 'path' => '/tmp/symfony.log', 'bubble' => false, 'level' => 'warn', 'file_permission' => '0666', 'use_locking' => true]
-        ]]]);
+        $this->assertDICConstructorArguments($handler, ['/tmp/symfony.log', 'ERROR', false, 0666, true]);
     }
 
     public function testLoadWithNestedHandler()
@@ -91,7 +82,7 @@ class MonologExtensionTest extends DependencyInjectionTest
 
         $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]);
     }
 
     public function testLoadWithServiceHandler()
@@ -228,7 +219,7 @@ class MonologExtensionTest extends DependencyInjectionTest
 
         $handler = $container->getDefinition('monolog.handler.main');
         $this->assertDICDefinitionClass($handler, 'Monolog\Handler\SyslogHandler');
-        $this->assertDICConstructorArguments($handler, [false, 'user', \Monolog\Logger::DEBUG, true, LOG_CONS]);
+        $this->assertDICConstructorArguments($handler, [false, 'user', 'DEBUG', true, LOG_CONS]);
     }
 
     public function testRollbarHandlerCreatesNotifier()
@@ -244,7 +235,7 @@ class MonologExtensionTest extends DependencyInjectionTest
 
         $handler = $container->getDefinition('monolog.handler.main');
         $this->assertDICDefinitionClass($handler, 'Monolog\Handler\RollbarHandler');
-        $this->assertDICConstructorArguments($handler, [new Reference('monolog.rollbar.notifier.1c8e6a67728dff6a209f828427128dd8b3c2b746'), \Monolog\Logger::DEBUG, true]);
+        $this->assertDICConstructorArguments($handler, [new Reference('monolog.rollbar.notifier.1c8e6a67728dff6a209f828427128dd8b3c2b746'), 'DEBUG', true]);
     }
 
     public function testRollbarHandlerReusesNotifier()
@@ -263,7 +254,7 @@ class MonologExtensionTest extends DependencyInjectionTest
 
         $handler = $container->getDefinition('monolog.handler.main');
         $this->assertDICDefinitionClass($handler, 'Monolog\Handler\RollbarHandler');
-        $this->assertDICConstructorArguments($handler, [new Reference('my_rollbar_id'), \Monolog\Logger::DEBUG, true]);
+        $this->assertDICConstructorArguments($handler, [new Reference('my_rollbar_id'), 'DEBUG', true]);
     }
 
     public function testSocketHandler()
@@ -288,7 +279,7 @@ class MonologExtensionTest extends DependencyInjectionTest
 
         $handler = $container->getDefinition('monolog.handler.socket');
         $this->assertDICDefinitionClass($handler, 'Monolog\Handler\SocketHandler');
-        $this->assertDICConstructorArguments($handler, ['localhost:50505', \Monolog\Logger::DEBUG, true]);
+        $this->assertDICConstructorArguments($handler, ['localhost:50505', 'DEBUG', true]);
         $this->assertDICDefinitionMethodCallAt(0, $handler, 'pushProcessor', [new Reference('monolog.processor.psr_log_message')]);
         $this->assertDICDefinitionMethodCallAt(1, $handler, 'setTimeout', ['1']);
         $this->assertDICDefinitionMethodCallAt(2, $handler, 'setConnectionTimeout', ['0.6']);
@@ -354,7 +345,7 @@ class MonologExtensionTest extends DependencyInjectionTest
         $this->assertDICDefinitionMethodCallAt(1, $logger, 'pushHandler', [new Reference('monolog.handler.raven')]);
 
         $handler = $container->getDefinition('monolog.handler.raven');
-        $this->assertDICConstructorArguments($handler, [new Reference('raven.client'), 100, true]);
+        $this->assertDICConstructorArguments($handler, [new Reference('raven.client'), 'DEBUG', true]);
     }
 
     public function testRavenHandlerWhenAClientIsSpecified()
@@ -374,7 +365,7 @@ class MonologExtensionTest extends DependencyInjectionTest
         $this->assertDICDefinitionMethodCallAt(1, $logger, 'pushHandler', [new Reference('monolog.handler.raven')]);
 
         $handler = $container->getDefinition('monolog.handler.raven');
-        $this->assertDICConstructorArguments($handler, [new Reference('raven.client'), 100, true]);
+        $this->assertDICConstructorArguments($handler, [new Reference('raven.client'), 'DEBUG', true]);
     }
 
     public function testSentryHandlerWhenConfigurationIsWrong()
@@ -487,7 +478,7 @@ class MonologExtensionTest extends DependencyInjectionTest
         $this->assertDICDefinitionMethodCallAt(1, $logger, 'pushHandler', [new Reference('monolog.handler.sentry')]);
 
         $handler = $container->getDefinition('monolog.handler.sentry');
-        $this->assertDICConstructorArguments($handler, [new Reference('sentry.hub'), \Monolog\Logger::DEBUG, true, false]);
+        $this->assertDICConstructorArguments($handler, [new Reference('sentry.hub'), 'DEBUG', true, false]);
     }
 
     public function testSentryHandlerWhenAHubAndAClientAreSpecified()
@@ -540,7 +531,7 @@ class MonologExtensionTest extends DependencyInjectionTest
         $this->assertDICDefinitionMethodCallAt(1, $logger, 'pushHandler', [new Reference('monolog.handler.loggly')]);
         $handler = $container->getDefinition('monolog.handler.loggly');
         $this->assertDICDefinitionClass($handler, 'Monolog\Handler\LogglyHandler');
-        $this->assertDICConstructorArguments($handler, [$token, \Monolog\Logger::DEBUG, true]);
+        $this->assertDICConstructorArguments($handler, [$token, 'DEBUG', true]);
         $this->assertDICDefinitionMethodCallAt(0, $handler, 'pushProcessor', [new Reference('monolog.processor.psr_log_message')]);
 
         $container = $this->getContainer([['handlers' => ['loggly' => [
@@ -558,7 +549,7 @@ class MonologExtensionTest extends DependencyInjectionTest
             $this->markTestSkipped('Symfony MonologBridge < 5.2 is needed.');
         }
 
-        $this->doTestFingersCrossedHandlerWhenExcluded404sAreSpecified(\Monolog\Logger::WARNING);
+        $this->doTestFingersCrossedHandlerWhenExcluded404sAreSpecified('WARNING');
     }
 
     /** @group legacy */
@@ -568,7 +559,7 @@ class MonologExtensionTest extends DependencyInjectionTest
             $this->markTestSkipped('Symfony MonologBridge >= 5.2 is needed.');
         }
 
-        $this->doTestFingersCrossedHandlerWhenExcluded404sAreSpecified(new Definition(ErrorLevelActivationStrategy::class, [\Monolog\Logger::WARNING]));
+        $this->doTestFingersCrossedHandlerWhenExcluded404sAreSpecified(new Definition(ErrorLevelActivationStrategy::class, ['WARNING']));
     }
 
     private function doTestFingersCrossedHandlerWhenExcluded404sAreSpecified($activation)
@@ -603,7 +594,7 @@ class MonologExtensionTest extends DependencyInjectionTest
             $this->markTestSkipped('Symfony MonologBridge < 5.2 is needed.');
         }
 
-        $this->doTestFingersCrossedHandlerWhenExcludedHttpCodesAreSpecified(\Monolog\Logger::WARNING);
+        $this->doTestFingersCrossedHandlerWhenExcludedHttpCodesAreSpecified('WARNING');
     }
 
     public function testFingersCrossedHandlerWhenExcludedHttpCodesAreSpecified()
@@ -612,7 +603,7 @@ class MonologExtensionTest extends DependencyInjectionTest
             $this->markTestSkipped('Symfony MonologBridge >= 5.2 is needed.');
         }
 
-        $this->doTestFingersCrossedHandlerWhenExcludedHttpCodesAreSpecified(new Definition(ErrorLevelActivationStrategy::class, [\Monolog\Logger::WARNING]));
+        $this->doTestFingersCrossedHandlerWhenExcludedHttpCodesAreSpecified(new Definition(ErrorLevelActivationStrategy::class, ['WARNING']));
     }
 
     private function doTestFingersCrossedHandlerWhenExcludedHttpCodesAreSpecified($activation)
@@ -716,18 +707,6 @@ class MonologExtensionTest extends DependencyInjectionTest
         ];
     }
 
-    public function testLogLevelfromInvalidparameterThrowsException()
-    {
-        $container = new ContainerBuilder();
-        $loader = new MonologExtension();
-        $config = [['handlers' => ['main' => ['type' => 'stream', 'level' => '%some_param%']]]];
-
-        $this->expectException(InvalidArgumentException::class);
-        $this->expectExceptionMessage('Could not match "%some_param%" to a log level.');
-
-        $loader->load($config, $container);
-    }
-
     /**
      * @dataProvider provideLoglevelParameterConfig
      */
@@ -753,13 +732,19 @@ class MonologExtensionTest extends DependencyInjectionTest
                 ['%log_level%' => 'info'],
                 ['type' => 'browser_console', 'level' => '%log_level%'],
                 'Monolog\Handler\BrowserConsoleHandler',
-                [200, true]
+                [
+                    '%log_level%',
+                    true,
+                ]
             ],
             'browser console with envvar level' => [
                 ['%env(LOG_LEVEL)%' => 'info'],
                 ['type' => 'browser_console', 'level' => '%env(LOG_LEVEL)%'],
                 'Monolog\Handler\BrowserConsoleHandler',
-                [200, true]
+                [
+                    '%env(LOG_LEVEL)%',
+                    true,
+                ]
             ],
             'stream with envvar level null or "~" (in yaml config)' => [
                 ['%env(LOG_LEVEL)%' => null],
@@ -767,7 +752,7 @@ class MonologExtensionTest extends DependencyInjectionTest
                 'Monolog\Handler\StreamHandler',
                 [
                     '%kernel.logs_dir%/%kernel.environment%.log',
-                    null,
+                    '%env(LOG_LEVEL)%',
                     true,
                     null,
                     false,
@@ -779,7 +764,7 @@ class MonologExtensionTest extends DependencyInjectionTest
                 'Monolog\Handler\StreamHandler',
                 [
                     '%kernel.logs_dir%/%kernel.environment%.log',
-                    400,
+                    '%env(LOG_LEVEL)%',
                     true,
                     null,
                     false,
@@ -791,7 +776,7 @@ class MonologExtensionTest extends DependencyInjectionTest
                 'Monolog\Handler\StreamHandler',
                 [
                     '%kernel.logs_dir%/%kernel.environment%.log',
-                    500,
+                    '%log_level%',
                     true,
                     null,
                     false,