Skip to content

Commit

Permalink
Log level mapping is done inside the handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN committed Dec 2, 2022
1 parent d84d39c commit b6e7432
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 99 deletions.
39 changes: 0 additions & 39 deletions DependencyInjection/MonologExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,33 +48,6 @@ class MonologExtension extends Extension

private $swiftMailerHandlers = [];

private function levelToMonologConst($level, ContainerBuilder $container)
{
if (null === $level || is_numeric($level)) {
return $level;
}

if (is_string($level) && (str_contains($level, 'env_') || str_contains($level, '%'))) {
$serviceId = 'monolog.level.' . $level;
if (!$container->has($serviceId)) {
$container->register($serviceId)
->setClass('int')
->setFactory([Logger::class, 'toMonologLevel'])
->setArguments([$level]);
}

return new Reference($serviceId);
}

$level = Logger::toMonologLevel($level);

if (function_exists('enum_exists') && enum_exists(Level::class) && $level instanceof Level) {
$level = $level->value;
}

return $level;
}

/**
* Loads the Monolog configuration.
*
Expand Down Expand Up @@ -199,8 +172,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 @@ -434,10 +405,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 @@ -483,12 +450,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
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
67 changes: 20 additions & 47 deletions Tests/DependencyInjection/MonologExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')]);
}

Expand All @@ -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(\Psr\Log\InvalidArgumentException::class);
$this->expectExceptionMessage('Level "warn" is not defined, use one of:');
$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()
Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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']);
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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' => [
Expand All @@ -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 */
Expand All @@ -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)
Expand Down Expand Up @@ -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()
Expand All @@ -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)
Expand Down Expand Up @@ -737,30 +728,12 @@ public function testLogLevelfromParameter(array $parameters, array $config, $exp
public function provideLoglevelParameterConfig()
{
return [
'browser console with numeric level' => [
[],
['type' => 'browser_console', 'level' => '300'],
'Monolog\Handler\BrowserConsoleHandler',
[
300,
true,
]
],
'browser console with named level' => [
[],
['type' => 'browser_console', 'level' => 'info'],
'Monolog\Handler\BrowserConsoleHandler',
[
200,
true,
]
],
'browser console with parameter level' => [
['%log_level%' => 'info'],
['type' => 'browser_console', 'level' => '%log_level%'],
'Monolog\Handler\BrowserConsoleHandler',
[
new Reference('monolog.level.%log_level%'),
'%log_level%',
true,
]
],
Expand All @@ -769,7 +742,7 @@ public function provideLoglevelParameterConfig()
['type' => 'browser_console', 'level' => '%env(LOG_LEVEL)%'],
'Monolog\Handler\BrowserConsoleHandler',
[
new Reference('monolog.level.%env(LOG_LEVEL)%'),
'%env(LOG_LEVEL)%',
true,
]
],
Expand All @@ -779,7 +752,7 @@ public function provideLoglevelParameterConfig()
'Monolog\Handler\StreamHandler',
[
'%kernel.logs_dir%/%kernel.environment%.log',
new Reference('monolog.level.%env(LOG_LEVEL)%'),
'%env(LOG_LEVEL)%',
true,
null,
false,
Expand All @@ -791,7 +764,7 @@ public function provideLoglevelParameterConfig()
'Monolog\Handler\StreamHandler',
[
'%kernel.logs_dir%/%kernel.environment%.log',
new Reference('monolog.level.%env(LOG_LEVEL)%'),
'%env(LOG_LEVEL)%',
true,
null,
false,
Expand All @@ -803,7 +776,7 @@ public function provideLoglevelParameterConfig()
'Monolog\Handler\StreamHandler',
[
'%kernel.logs_dir%/%kernel.environment%.log',
new Reference('monolog.level.%log_level%'),
'%log_level%',
true,
null,
false,
Expand Down

0 comments on commit b6e7432

Please sign in to comment.