diff --git a/DependencyInjection/MonologExtension.php b/DependencyInjection/MonologExtension.php index 7d766714..0c5bee27 100644 --- a/DependencyInjection/MonologExtension.php +++ b/DependencyInjection/MonologExtension.php @@ -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; @@ -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 @@ 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']); } @@ -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); @@ -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']; diff --git a/Tests/DependencyInjection/ConfigurationTest.php b/Tests/DependencyInjection/ConfigurationTest.php index c9b6a23a..38847364 100644 --- a/Tests/DependencyInjection/ConfigurationTest.php +++ b/Tests/DependencyInjection/ConfigurationTest.php @@ -189,6 +189,7 @@ public function testMergingInvalidChannels() $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 3cbb1924..33841133 100644 --- a/Tests/DependencyInjection/FixtureMonologExtensionTest.php +++ b/Tests/DependencyInjection/FixtureMonologExtensionTest.php @@ -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() @@ -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) @@ -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 */ @@ -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() @@ -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) @@ -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'); @@ -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() @@ -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() @@ -209,7 +209,7 @@ public function testServerLog() $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 52307b98..4ff3cf93 100644 --- a/Tests/DependencyInjection/MonologExtensionTest.php +++ b/Tests/DependencyInjection/MonologExtensionTest.php @@ -44,7 +44,7 @@ public function testLoadWithDefault() $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 @@ public function testLoadWithCustomValues() $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 @@ public function testLoadWithNestedHandler() $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 @@ public function testSyslogHandlerWithLogopts() $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 @@ public function testRollbarHandlerCreatesNotifier() $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 @@ public function testRollbarHandlerReusesNotifier() $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 @@ public function testSocketHandler() $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 @@ public function testRavenHandlerWhenADSNAndAClientAreSpecified() $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 @@ public function testRavenHandlerWhenAClientIsSpecified() $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 @@ public function testSentryHandlerWhenAHubIsSpecified() $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 @@ public function testLogglyHandler() $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 @@ public function testLegacyFingersCrossedHandlerWhenExcluded404sAreSpecified() $this->markTestSkipped('Symfony MonologBridge < 5.2 is needed.'); } - $this->doTestFingersCrossedHandlerWhenExcluded404sAreSpecified(\Monolog\Logger::WARNING); + $this->doTestFingersCrossedHandlerWhenExcluded404sAreSpecified('WARNING'); } /** @group legacy */ @@ -568,7 +559,7 @@ public function testFingersCrossedHandlerWhenExcluded404sAreSpecified() $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 @@ public function testLegacyFingersCrossedHandlerWhenExcludedHttpCodesAreSpecified $this->markTestSkipped('Symfony MonologBridge < 5.2 is needed.'); } - $this->doTestFingersCrossedHandlerWhenExcludedHttpCodesAreSpecified(\Monolog\Logger::WARNING); + $this->doTestFingersCrossedHandlerWhenExcludedHttpCodesAreSpecified('WARNING'); } public function testFingersCrossedHandlerWhenExcludedHttpCodesAreSpecified() @@ -612,7 +603,7 @@ public function testFingersCrossedHandlerWhenExcludedHttpCodesAreSpecified() $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 @@ public function v1AddedDataProvider() ]; } - 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 @@ public function provideLoglevelParameterConfig() ['%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 @@ public function provideLoglevelParameterConfig() 'Monolog\Handler\StreamHandler', [ '%kernel.logs_dir%/%kernel.environment%.log', - null, + '%env(LOG_LEVEL)%', true, null, false, @@ -779,7 +764,7 @@ public function provideLoglevelParameterConfig() 'Monolog\Handler\StreamHandler', [ '%kernel.logs_dir%/%kernel.environment%.log', - 400, + '%env(LOG_LEVEL)%', true, null, false, @@ -791,7 +776,7 @@ public function provideLoglevelParameterConfig() 'Monolog\Handler\StreamHandler', [ '%kernel.logs_dir%/%kernel.environment%.log', - 500, + '%log_level%', true, null, false,