From 0b0afbe0e00327bec046d2744791795789810d7a Mon Sep 17 00:00:00 2001 From: Nikita Konstantinov Date: Thu, 9 Oct 2014 19:45:06 +0400 Subject: [PATCH] Added "file_permission" option for "stream" and "rotating_file" handler types --- DependencyInjection/Configuration.php | 13 ++++++++++ DependencyInjection/MonologExtension.php | 2 ++ Resources/config/schema/monolog-1.0.xsd | 1 + .../DependencyInjection/ConfigurationTest.php | 25 +++++++++++++++++++ .../FixtureMonologExtensionTest.php | 10 ++++---- .../Fixtures/xml/multiple_handlers.xml | 2 +- .../Fixtures/yml/multiple_handlers.yml | 1 + .../MonologExtensionTest.php | 8 +++--- 8 files changed, 53 insertions(+), 9 deletions(-) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index e01645a0..ee689747 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -270,6 +270,19 @@ public function getConfigTreeBuilder() ->scalarNode('level')->defaultValue('DEBUG')->end() ->booleanNode('bubble')->defaultTrue()->end() ->scalarNode('path')->defaultValue('%kernel.logs_dir%/%kernel.environment%.log')->end() // stream and rotating + ->scalarNode('file_permission') // stream and rotating + ->defaultNull() + ->beforeNormalization() + ->ifString() + ->then(function ($v) { + if (substr($v, 0, 1) === '0') { + return octdec($v); + } + + return (int) $v; + }) + ->end() + ->end() ->scalarNode('ident')->defaultFalse()->end() // syslog ->scalarNode('logopts')->defaultValue(LOG_PID)->end() // syslog ->scalarNode('facility')->defaultValue('user')->end() // syslog diff --git a/DependencyInjection/MonologExtension.php b/DependencyInjection/MonologExtension.php index 8eaa653d..d50efc6d 100644 --- a/DependencyInjection/MonologExtension.php +++ b/DependencyInjection/MonologExtension.php @@ -135,6 +135,7 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler $handler['path'], $handler['level'], $handler['bubble'], + $handler['file_permission'], )); break; @@ -242,6 +243,7 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler $handler['max_files'], $handler['level'], $handler['bubble'], + $handler['file_permission'], )); break; diff --git a/Resources/config/schema/monolog-1.0.xsd b/Resources/config/schema/monolog-1.0.xsd index 968735f7..ac841eed 100644 --- a/Resources/config/schema/monolog-1.0.xsd +++ b/Resources/config/schema/monolog-1.0.xsd @@ -61,6 +61,7 @@ + diff --git a/Tests/DependencyInjection/ConfigurationTest.php b/Tests/DependencyInjection/ConfigurationTest.php index 69b86019..1c38da9e 100644 --- a/Tests/DependencyInjection/ConfigurationTest.php +++ b/Tests/DependencyInjection/ConfigurationTest.php @@ -271,6 +271,31 @@ public function testWithType() $this->assertEquals('B', $config['handlers']['foo']['channels']['elements'][1]); } + public function testWithFilePermission() + { + $configs = array( + array( + 'handlers' => array( + 'foo' => array( + 'type' => 'stream', + 'path' => '/foo', + 'file_permission' => '0666', + ), + 'bar' => array( + 'type' => 'stream', + 'path' => '/bar', + 'file_permission' => 0777 + ) + ) + ) + ); + + $config = $this->process($configs); + + $this->assertSame(0666, $config['handlers']['foo']['file_permission']); + $this->assertSame(0777, $config['handlers']['bar']['file_permission']); + } + /** * Processes an array of configurations and returns a compiled version. * diff --git a/Tests/DependencyInjection/FixtureMonologExtensionTest.php b/Tests/DependencyInjection/FixtureMonologExtensionTest.php index afdfd819..338eca2c 100644 --- a/Tests/DependencyInjection/FixtureMonologExtensionTest.php +++ b/Tests/DependencyInjection/FixtureMonologExtensionTest.php @@ -35,7 +35,7 @@ public function testLoadWithSeveralHandlers() $handler = $container->getDefinition('monolog.handler.custom'); $this->assertDICDefinitionClass($handler, '%monolog.handler.stream.class%'); - $this->assertDICConstructorArguments($handler, array('/tmp/symfony.log', \Monolog\Logger::ERROR, false)); + $this->assertDICConstructorArguments($handler, array('/tmp/symfony.log', \Monolog\Logger::ERROR, false, 0666)); $handler = $container->getDefinition('monolog.handler.main'); $this->assertDICDefinitionClass($handler, '%monolog.handler.fingers_crossed.class%'); @@ -62,7 +62,7 @@ public function testLoadWithOverwriting() $handler = $container->getDefinition('monolog.handler.custom'); $this->assertDICDefinitionClass($handler, '%monolog.handler.stream.class%'); - $this->assertDICConstructorArguments($handler, array('/tmp/symfony.log', \Monolog\Logger::WARNING, true)); + $this->assertDICConstructorArguments($handler, array('/tmp/symfony.log', \Monolog\Logger::WARNING, true, null)); $handler = $container->getDefinition('monolog.handler.main'); $this->assertDICDefinitionClass($handler, '%monolog.handler.fingers_crossed.class%'); @@ -87,7 +87,7 @@ public function testLoadWithNewAtEnd() $handler = $container->getDefinition('monolog.handler.new'); $this->assertDICDefinitionClass($handler, '%monolog.handler.stream.class%'); - $this->assertDICConstructorArguments($handler, array('/tmp/monolog.log', \Monolog\Logger::ERROR, true)); + $this->assertDICConstructorArguments($handler, array('/tmp/monolog.log', \Monolog\Logger::ERROR, true, null)); } public function testLoadWithNewAndPriority() @@ -114,11 +114,11 @@ public function testLoadWithNewAndPriority() $handler = $container->getDefinition('monolog.handler.first'); $this->assertDICDefinitionClass($handler, '%monolog.handler.rotating_file.class%'); - $this->assertDICConstructorArguments($handler, array('/tmp/monolog.log', 0, \Monolog\Logger::ERROR, true)); + $this->assertDICConstructorArguments($handler, array('/tmp/monolog.log', 0, \Monolog\Logger::ERROR, true, null)); $handler = $container->getDefinition('monolog.handler.last'); $this->assertDICDefinitionClass($handler, '%monolog.handler.stream.class%'); - $this->assertDICConstructorArguments($handler, array('/tmp/last.log', \Monolog\Logger::ERROR, true)); + $this->assertDICConstructorArguments($handler, array('/tmp/last.log', \Monolog\Logger::ERROR, true, null)); } public function testHandlersWithChannels() diff --git a/Tests/DependencyInjection/Fixtures/xml/multiple_handlers.xml b/Tests/DependencyInjection/Fixtures/xml/multiple_handlers.xml index df9e790c..a28e03e4 100644 --- a/Tests/DependencyInjection/Fixtures/xml/multiple_handlers.xml +++ b/Tests/DependencyInjection/Fixtures/xml/multiple_handlers.xml @@ -7,7 +7,7 @@ http://symfony.com/schema/dic/monolog http://symfony.com/schema/dic/monolog/monolog-1.0.xsd"> - + diff --git a/Tests/DependencyInjection/Fixtures/yml/multiple_handlers.yml b/Tests/DependencyInjection/Fixtures/yml/multiple_handlers.yml index 653e74ea..0cece12b 100644 --- a/Tests/DependencyInjection/Fixtures/yml/multiple_handlers.yml +++ b/Tests/DependencyInjection/Fixtures/yml/multiple_handlers.yml @@ -5,6 +5,7 @@ monolog: path: /tmp/symfony.log bubble: false level: ERROR + file_permission: 0666 main: type: fingers_crossed action_level: ERROR diff --git a/Tests/DependencyInjection/MonologExtensionTest.php b/Tests/DependencyInjection/MonologExtensionTest.php index 35da2f1b..d0ddb64e 100644 --- a/Tests/DependencyInjection/MonologExtensionTest.php +++ b/Tests/DependencyInjection/MonologExtensionTest.php @@ -31,12 +31,14 @@ public function testLoadWithDefault() $handler = $container->getDefinition('monolog.handler.main'); $this->assertDICDefinitionClass($handler, '%monolog.handler.stream.class%'); - $this->assertDICConstructorArguments($handler, array('%kernel.logs_dir%/%kernel.environment%.log', \Monolog\Logger::DEBUG, true)); + $this->assertDICConstructorArguments($handler, array('%kernel.logs_dir%/%kernel.environment%.log', \Monolog\Logger::DEBUG, true, null)); } public function testLoadWithCustomValues() { - $container = $this->getContainer(array(array('handlers' => array('custom' => array('type' => 'stream', 'path' => '/tmp/symfony.log', 'bubble' => false, 'level' => 'ERROR'))))); + $container = $this->getContainer(array(array('handlers' => array( + 'custom' => array('type' => 'stream', 'path' => '/tmp/symfony.log', 'bubble' => false, 'level' => 'ERROR', 'file_permission' => '0666') + )))); $this->assertTrue($container->hasDefinition('monolog.logger')); $this->assertTrue($container->hasDefinition('monolog.handler.custom')); @@ -45,7 +47,7 @@ public function testLoadWithCustomValues() $handler = $container->getDefinition('monolog.handler.custom'); $this->assertDICDefinitionClass($handler, '%monolog.handler.stream.class%'); - $this->assertDICConstructorArguments($handler, array('/tmp/symfony.log', \Monolog\Logger::ERROR, false)); + $this->assertDICConstructorArguments($handler, array('/tmp/symfony.log', \Monolog\Logger::ERROR, false, 0666)); } /**