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 6204234
Show file tree
Hide file tree
Showing 4 changed files with 34 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
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 6204234

Please sign in to comment.