Skip to content

Commit

Permalink
Use monolog Logger::toMonologLevel function
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN committed Dec 2, 2022
1 parent 319a920 commit d84d39c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 40 deletions.
19 changes: 14 additions & 5 deletions DependencyInjection/MonologExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
use Symfony\Bridge\Monolog\Processor\TokenProcessor;
use Symfony\Bridge\Monolog\Processor\WebProcessor;
use Symfony\Bundle\FullStack;
use Symfony\Bundle\MonologBundle\MonologBundle;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Argument\BoundArgument;
use Symfony\Component\DependencyInjection\ChildDefinition;
Expand All @@ -51,19 +50,29 @@ class MonologExtension extends Extension

private function levelToMonologConst($level, ContainerBuilder $container)
{
if (is_string($level) && str_contains($level, '%')) {
$serviceId = 'monolog.level.' . md5($level);
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([MonologBundle::class, 'levelToMonologConst'])
->setFactory([Logger::class, 'toMonologLevel'])
->setArguments([$level]);
}

return new Reference($serviceId);
}

return MonologBundle::levelToMonologConst($level);
$level = Logger::toMonologLevel($level);

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

return $level;
}

/**
Expand Down
16 changes: 0 additions & 16 deletions MonologBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,4 @@ public static function includeStacktraces(HandlerInterface $handler)
$formatter->includeStacktraces();
}
}

/**
* @internal
*/
public static function levelToMonologConst($level)
{
if (null === $level || is_numeric($level)) {
return $level;
}

if (defined('Monolog\Logger::'.strtoupper($level))) {
return constant('Monolog\Logger::' . strtoupper($level));
}

throw new \InvalidArgumentException(sprintf('Could not match "%s" to a log level.', $level));
}
}
50 changes: 31 additions & 19 deletions Tests/DependencyInjection/MonologExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ public function testLoadWithCustomValues()

public function testLoadWithUnknownLevel()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Could not match "warn" to a log level.');
$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]
]]]);
Expand Down Expand Up @@ -716,18 +716,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
*/
Expand All @@ -749,25 +737,49 @@ 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',
[200, true]
[
new Reference('monolog.level.%log_level%'),
true,
]
],
'browser console with envvar level' => [
['%env(LOG_LEVEL)%' => 'info'],
['type' => 'browser_console', 'level' => '%env(LOG_LEVEL)%'],
'Monolog\Handler\BrowserConsoleHandler',
[200, true]
[
new Reference('monolog.level.%env(LOG_LEVEL)%'),
true,
]
],
'stream with envvar level null or "~" (in yaml config)' => [
['%env(LOG_LEVEL)%' => null],
['type' => 'stream', 'level' => '%env(LOG_LEVEL)%'],
'Monolog\Handler\StreamHandler',
[
'%kernel.logs_dir%/%kernel.environment%.log',
null,
new Reference('monolog.level.%env(LOG_LEVEL)%'),
true,
null,
false,
Expand All @@ -779,7 +791,7 @@ public function provideLoglevelParameterConfig()
'Monolog\Handler\StreamHandler',
[
'%kernel.logs_dir%/%kernel.environment%.log',
400,
new Reference('monolog.level.%env(LOG_LEVEL)%'),
true,
null,
false,
Expand All @@ -791,7 +803,7 @@ public function provideLoglevelParameterConfig()
'Monolog\Handler\StreamHandler',
[
'%kernel.logs_dir%/%kernel.environment%.log',
500,
new Reference('monolog.level.%log_level%'),
true,
null,
false,
Expand Down

0 comments on commit d84d39c

Please sign in to comment.