diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 9412b24..708f3b2 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -24,6 +24,7 @@ public function getConfigTreeBuilder() ->arrayNode('application')->isRequired() ->children() ->scalarNode('name')->defaultNull()->cannotBeEmpty()->end() + ->scalarNode('locale')->defaultNull()->end() ->end() ->end() ->arrayNode('handlers')->isRequired() diff --git a/DependencyInjection/DeamonLoggerExtraExtension.php b/DependencyInjection/DeamonLoggerExtraExtension.php index 743c6d4..d6d05e3 100644 --- a/DependencyInjection/DeamonLoggerExtraExtension.php +++ b/DependencyInjection/DeamonLoggerExtraExtension.php @@ -27,7 +27,8 @@ public function load(array $configs, ContainerBuilder $container) $loader->load('processors.xml'); $definition = $container->getDefinition('deamon.logger_extra.context'); - $definition->replaceArgument(1, $config['application']['name']); + $definition->replaceArgument(0, $config['application']['name']); + $definition->addArgument($config['application']['locale']); $definition = $container->getDefinition('deamon.logger_extra.processors.web_processor'); $definition->replaceArgument(0, $config['config']); diff --git a/Processors/Monolog/DeamonLoggerExtraWebProcessor.php b/Processors/Monolog/DeamonLoggerExtraWebProcessor.php index c4377e2..932354d 100644 --- a/Processors/Monolog/DeamonLoggerExtraWebProcessor.php +++ b/Processors/Monolog/DeamonLoggerExtraWebProcessor.php @@ -137,7 +137,7 @@ private function addUserInfo() } /** - * append method result of user object + * append method result of user object. * * @param $user */ @@ -151,7 +151,7 @@ private function appendUserMethodInfo($user) } /** - * Check if passed token is an instance of TokenInterface and an instance of config UserClass + * Check if passed token is an instance of TokenInterface and an instance of config UserClass. * * @param $token * @@ -182,7 +182,7 @@ private function addChannelInfo() */ private function addInfo($key, $value) { - if ($this->configShowExtraInfo($key)) { + if ($this->configShowExtraInfo($key) && $value !== null) { $this->record['extra'][$key] = $value; } } diff --git a/README.md b/README.md index d8fb7e4..d69ad69 100644 --- a/README.md +++ b/README.md @@ -89,7 +89,8 @@ deamon_logger_extra: // app/config/config.yml deamon_logger_extra: application: - name: "loc-deamonfront" # default to null + name: "loc-deamonfront" # default to null + locale: "fr" # default to null handlers: [default_info] # the only required field config: channel_prefix: "v0.1" # default to null diff --git a/Resources/config/services.xml b/Resources/config/services.xml index 1062a6b..4df1903 100644 --- a/Resources/config/services.xml +++ b/Resources/config/services.xml @@ -9,7 +9,6 @@ - %locale% diff --git a/Services/DeamonLoggerExtraContext.php b/Services/DeamonLoggerExtraContext.php index 902370c..564f127 100644 --- a/Services/DeamonLoggerExtraContext.php +++ b/Services/DeamonLoggerExtraContext.php @@ -7,10 +7,10 @@ class DeamonLoggerExtraContext private $locale; private $applicationName; - public function __construct($locale, $applicationName) + public function __construct($applicationName, $locale = null) { - $this->locale = $locale; $this->applicationName = $applicationName; + $this->locale = $locale; } public function getLocale() diff --git a/Tests/DependencyInjection/DeamonLoggerExtraExtensionTest.php b/Tests/DependencyInjection/DeamonLoggerExtraExtensionTest.php index e966fb4..d164dc7 100644 --- a/Tests/DependencyInjection/DeamonLoggerExtraExtensionTest.php +++ b/Tests/DependencyInjection/DeamonLoggerExtraExtensionTest.php @@ -42,7 +42,9 @@ public function testLoad() $this->assertTrue($this->container->hasDefinition('deamon.logger_extra.processors.web_processor')); $definition1 = $this->container->getDefinition('deamon.logger_extra.context'); - $this->assertEquals('foo', $definition1->getArgument(1)); + $this->assertEquals('foo', $definition1->getArgument(0)); + $this->assertCount(2, $definition1->getArguments()); + $this->assertEquals('fr', $definition1->getArgument(1)); $definition2 = $this->container->getDefinition('deamon.logger_extra.processors.web_processor'); $this->assertEquals($configs[0]['config'], $definition2->getArgument(0)); @@ -84,6 +86,8 @@ public function testDefaultValue() $this->assertTrue($this->container->hasDefinition('deamon.logger_extra.processors.web_processor')); $definition1 = $this->container->getDefinition('deamon.logger_extra.context'); + $this->assertNull($definition1->getArgument(0)); + $this->assertCount(2, $definition1->getArguments()); $this->assertNull($definition1->getArgument(1)); $definition2 = $this->container->getDefinition('deamon.logger_extra.processors.web_processor'); @@ -120,6 +124,7 @@ private function getValidConfigFull() return [ 'application' => [ 'name' => 'foo', + 'locale' => 'fr', ], 'handlers' => ['bar'], 'config' => [ diff --git a/Tests/Processors/Monolog/DeamonLoggerExtraWebProcessorTest.php b/Tests/Processors/Monolog/DeamonLoggerExtraWebProcessorTest.php index 4fdc0eb..41bbb9e 100644 --- a/Tests/Processors/Monolog/DeamonLoggerExtraWebProcessorTest.php +++ b/Tests/Processors/Monolog/DeamonLoggerExtraWebProcessorTest.php @@ -44,6 +44,27 @@ public function testAddContextInfo() $this->assertArrayHasKeyAndEquals('application', $record['extra'], 'foo_app'); } + /** + * @runInSeparateProcess + */ + public function testAddContextInfoWithoutLocale() + { + $config = $this->getDisplayConfig([ + 'env' => true, + 'locale' => true, + 'application_name' => true, + ]); + + $processor = new DeamonLoggerExtraWebProcessor($config); + $processor->setLoggerExtraContext($this->getLoggerExtraContext(null)); + $processor->setEnvironment('env_foo'); + $record = $processor->__invoke($this->getRecord()); + + $this->assertArrayHasKeyAndEquals('env', $record['extra'], 'env_foo'); + $this->assertArrayNotHasKey('locale', $record['extra'], 'fr'); + $this->assertArrayHasKeyAndEquals('application', $record['extra'], 'foo_app'); + } + public function testAddRequestInfo() { $config = $this->getDisplayConfig( @@ -226,9 +247,9 @@ private function getRequestStack() return $stack; } - private function getLoggerExtraContext() + private function getLoggerExtraContext($locale = 'fr') { - return new DeamonLoggerExtraContext('fr', 'foo_app'); + return new DeamonLoggerExtraContext('foo_app', $locale); } private function getTokenStorage(UserInterface $user = null) diff --git a/Tests/Services/DeamonLoggerExtraContextTest.php b/Tests/Services/DeamonLoggerExtraContextTest.php index f17e849..66b6f5b 100644 --- a/Tests/Services/DeamonLoggerExtraContextTest.php +++ b/Tests/Services/DeamonLoggerExtraContextTest.php @@ -10,11 +10,11 @@ class DeamonLoggerExtraContextTest extends TestCase /** * @dataProvider getLocaleDataset * - * @param $locale + * @param string $locale */ public function testGetLocale($locale) { - $context = new DeamonLoggerExtraContext($locale, ''); + $context = new DeamonLoggerExtraContext('', $locale); $this->assertEquals($locale, $context->getLocale(), sprintf('locale should be %s, %s returned.', $locale, $context->getLocale())); } @@ -33,7 +33,7 @@ public function getLocaleDataset() */ public function testGetApplicationName($applicationName) { - $context = new DeamonLoggerExtraContext('fr', $applicationName); + $context = new DeamonLoggerExtraContext($applicationName, 'fr'); $this->assertEquals($applicationName, $context->getApplicationName(), sprintf('application_name should be %s, %s returned.', $applicationName, $context->getApplicationName())); }