diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 8828e9a3..24a1530b 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -67,7 +67,25 @@ public function getConfigTreeBuilder() ->booleanNode('stop_buffering')->defaultTrue()->end()// fingers_crossed ->scalarNode('buffer_size')->defaultValue(0)->end() // fingers_crossed and buffer ->scalarNode('handler')->end() // fingers_crossed and buffer - ->scalarNode('publisher')->end() // gelf + ->arrayNode('publisher') + ->canBeUnset() + ->beforeNormalization() + ->ifString() + ->then(function($v) { return array('id'=> $v); }) + ->end() + ->children() + ->scalarNode('id')->end() + ->scalarNode('hostname')->end() + ->scalarNode('port')->defaultValue(12201)->end() + ->scalarNode('chunk_size')->defaultValue(1420)->end() + ->end() + ->validate() + ->ifTrue(function($v) { + return !isset($v['id']) && !isset($v['hostname']); + }) + ->thenInvalid('What must be set is either the hostname or the id.') + ->end() + ->end() // gelf ->arrayNode('members') // group ->canBeUnset() ->performNoDeepMerging() @@ -159,6 +177,10 @@ public function getConfigTreeBuilder() ->ifTrue(function($v) { return 'service' === $v['type'] && !isset($v['id']); }) ->thenInvalid('The id has to be specified to use a service as handler') ->end() + ->validate() + ->ifTrue(function($v) { return 'gelf' === $v['type'] && !isset($v['publisher']); }) + ->thenInvalid('The publisher has to be specified to use a GelfHandler') + ->end() ->end() ->validate() ->ifTrue(function($v) { return isset($v['debug']); }) diff --git a/DependencyInjection/MonologExtension.php b/DependencyInjection/MonologExtension.php index 3af643f1..5636a847 100644 --- a/DependencyInjection/MonologExtension.php +++ b/DependencyInjection/MonologExtension.php @@ -130,8 +130,22 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler break; case 'gelf': + if (isset($handler['publisher']['id'])) { + $publisherId = $handler['publisher']['id']; + } else { + $publisher = new Definition("%monolog.gelf.publisher.class%", array( + $handler['publisher']['hostname'], + $handler['publisher']['port'], + $handler['publisher']['chunk_size'], + )); + + $publisherId = 'monolog.gelf.publisher'; + $publisher->setPublic(false); + $container->setDefinition($publisherId, $publisher); + } + $definition->setArguments(array( - new Reference($handler['publisher']), + new Reference($publisherId), $handler['level'], $handler['bubble'], )); diff --git a/Resources/config/monolog.xml b/Resources/config/monolog.xml index 533f06e2..5e76780e 100644 --- a/Resources/config/monolog.xml +++ b/Resources/config/monolog.xml @@ -6,6 +6,7 @@ Symfony\Bridge\Monolog\Logger + Gelf\MessagePublisher Monolog\Handler\StreamHandler Monolog\Handler\GroupHandler Monolog\Handler\BufferHandler diff --git a/Resources/config/schema/monolog-1.0.xsd b/Resources/config/schema/monolog-1.0.xsd index 2af5634d..aeba3a31 100644 --- a/Resources/config/schema/monolog-1.0.xsd +++ b/Resources/config/schema/monolog-1.0.xsd @@ -18,6 +18,7 @@ + @@ -32,7 +33,6 @@ - @@ -64,6 +64,13 @@ + + + + + + + diff --git a/Tests/DependencyInjection/ConfigurationTest.php b/Tests/DependencyInjection/ConfigurationTest.php index 84af068f..a01bc70b 100644 --- a/Tests/DependencyInjection/ConfigurationTest.php +++ b/Tests/DependencyInjection/ConfigurationTest.php @@ -32,14 +32,14 @@ public function testProcessSimpleCase() $this->assertArrayHasKey('handlers', $config); $this->assertArrayHasKey('foobar', $config['handlers']); - $this->assertEquals('stream', $config['handlers']['foobar']['type']); + $this->assertEquals('stream', $config['handlers']['foobar']['type']); $this->assertEquals('/foo/bar', $config['handlers']['foobar']['path']); } public function provideProcessStringChannels() { return array( - array('foo', 'foo', true), + array('foo', 'foo', true), array('!foo', 'foo', false) ); } @@ -53,8 +53,8 @@ public function testProcessStringChannels($string, $expectedString, $isInclusive array( 'handlers' => array( 'foobar' => array( - 'type' => 'stream', - 'path' => '/foo/bar', + 'type' => 'stream', + 'path' => '/foo/bar', 'channels' => $string ) ) @@ -68,6 +68,43 @@ public function testProcessStringChannels($string, $expectedString, $isInclusive $this->assertEquals($expectedString, $config['handlers']['foobar']['channels']['elements'][0]); } + public function provideGelfPublisher() + { + return array( + array( + 'gelf.publisher' + ), + array( + array( + 'id' => 'gelf.publisher' + ) + ) + ); + } + + /** + * @dataProvider provideGelfPublisher + */ + public function testGelfPublisherService($publisher) + { + $configs = array( + array( + 'handlers' => array( + 'gelf' => array( + 'type' => 'gelf', + 'publisher' => $publisher, + ), + ) + ) + ); + + $config = $this->process($configs); + + $this->assertArrayHasKey('id', $config['handlers']['gelf']['publisher']); + $this->assertArrayNotHasKey('hostname', $config['handlers']['gelf']['publisher']); + $this->assertEquals('gelf.publisher', $config['handlers']['gelf']['publisher']['id']); + } + public function testArrays() { $configs = array( diff --git a/Tests/DependencyInjection/MonologExtensionTest.php b/Tests/DependencyInjection/MonologExtensionTest.php index af9b37ef..6d49ffc8 100644 --- a/Tests/DependencyInjection/MonologExtensionTest.php +++ b/Tests/DependencyInjection/MonologExtensionTest.php @@ -194,6 +194,28 @@ public function testExceptionWhenUsingBufferWithoutHandler() $loader->load(array(array('handlers' => array('main' => array('type' => 'buffer')))), $container); } + /** + * @expectedException Symfony\Component\Config\Definition\Exception\InvalidConfigurationException + */ + public function testExceptionWhenUsingGelfWithoutPublisher() + { + $container = new ContainerBuilder(); + $loader = new MonologExtension(); + + $loader->load(array(array('handlers' => array('gelf' => array('type' => 'gelf')))), $container); + } + + /** + * @expectedException Symfony\Component\Config\Definition\Exception\InvalidConfigurationException + */ + public function testExceptionWhenUsingGelfWithoutPublisherHostname() + { + $container = new ContainerBuilder(); + $loader = new MonologExtension(); + + $loader->load(array(array('handlers' => array('gelf' => array('type' => 'gelf', 'publisher' => array())))), $container); + } + /** * @expectedException Symfony\Component\Config\Definition\Exception\InvalidConfigurationException */