diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 255eba47..3661370f 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -50,7 +50,12 @@ * - [bubble]: bool, defaults to true * * - gelf: - * - publisher: {id: ...} or {hostname: ..., port: ..., chunk_size: ...} + * - publisher: + * - id: string, service id of a publisher implementation, optional if hostname is given + * - hostname: string, optional if id is given + * - [port]: int, defaults to 12201 + * - [chunk_size]: int, defaults to 1420 + * - [encoder]: string, its value can be 'json' or 'compressed_json' * - [level]: level name or int value, defaults to DEBUG * - [bubble]: bool, defaults to true * @@ -816,6 +821,7 @@ private function addGelfSection(ArrayNodeDefinition $handerNode) ->scalarNode('hostname')->end() ->scalarNode('port')->defaultValue(12201)->end() ->scalarNode('chunk_size')->defaultValue(1420)->end() + ->scalarNode('encoder')->end() ->end() ->validate() ->ifTrue(function ($v) { diff --git a/DependencyInjection/MonologExtension.php b/DependencyInjection/MonologExtension.php index 1d47a984..dfad84ae 100644 --- a/DependencyInjection/MonologExtension.php +++ b/DependencyInjection/MonologExtension.php @@ -24,6 +24,7 @@ use Symfony\Bridge\Monolog\Processor\SwitchUserTokenProcessor; use Symfony\Bridge\Monolog\Processor\TokenProcessor; use Symfony\Bridge\Monolog\Processor\WebProcessor; +use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; use Symfony\Component\Config\FileLocator; use Symfony\Component\DependencyInjection\Argument\BoundArgument; use Symfony\Component\DependencyInjection\ChildDefinition; @@ -227,10 +228,28 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler ]); $transport->setPublic(false); + if (isset($handler['publisher']['encoder'])) { + if ('compressed_json' === $handler['publisher']['encoder']) { + $encoderClass = 'Gelf\Encoder\CompressedJsonEncoder'; + } elseif ('json' === $handler['publisher']['encoder']) { + $encoderClass = 'Gelf\Encoder\JsonEncoder'; + } else { + throw new InvalidConfigurationException('The gelf message encoder must be either "compressed_json" or "json".'); + } + + $encoder = new Definition($encoderClass); + $encoder->setPublic(false); + + $transport->addMethodCall('setMessageEncoder', [$encoder]); + } + $publisher = new Definition('Gelf\Publisher', []); $publisher->addMethodCall('addTransport', [$transport]); $publisher->setPublic(false); } elseif (class_exists('Gelf\MessagePublisher')) { + if (isset($handler['publisher']['encoder']) && 'compressed_json' !== $handler['publisher']['encoder']) { + throw new InvalidConfigurationException('The Gelf\MessagePublisher publisher supports only the compressed json encoding. Omit the option to use the default encoding or use "compressed_json" as the encoder option.'); + } $publisher = new Definition('Gelf\MessagePublisher', [ $handler['publisher']['hostname'], $handler['publisher']['port'], diff --git a/Tests/DependencyInjection/ConfigurationTest.php b/Tests/DependencyInjection/ConfigurationTest.php index 516565e0..227997d6 100644 --- a/Tests/DependencyInjection/ConfigurationTest.php +++ b/Tests/DependencyInjection/ConfigurationTest.php @@ -110,6 +110,28 @@ public function testGelfPublisherService($publisher) $this->assertEquals('gelf.publisher', $config['handlers']['gelf']['publisher']['id']); } + public function testGelfPublisherWithEncoder(): void + { + $configs = [ + [ + 'handlers' => [ + 'gelf' => [ + 'type' => 'gelf', + 'publisher' => [ + 'hostname' => 'localhost', + 'encoder' => 'compressed_json', + ], + ], + ], + ], + ]; + + $config = $this->process($configs); + + $this->assertEquals('localhost', $config['handlers']['gelf']['publisher']['hostname']); + $this->assertEquals('compressed_json', $config['handlers']['gelf']['publisher']['encoder']); + } + public function testArrays() { $configs = [