Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for configuring Gelf encoders in Monolog configuration #492

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,17 @@
* - [bubble]: bool, defaults to true
*
* - gelf:
* - publisher: {id: ...} or {hostname: ..., port: ..., chunk_size: ...}
* - [level]: level name or int value, defaults to DEBUG
* - [bubble]: bool, defaults to true
* - publisher: (one of the following configurations)
* # Option 1: Service-based configuration
* - id: string, service id of a publisher implementation
*
* # Option 2: Direct connection configuration
* - hostname: string, server hostname
* - [port]: int, server port (default: 12201)
* - [chunk_size]: int, UDP packet size (default: 1420)
* - [encoder]: string, encoding format ('json' or 'compressed_json')
* - [level]: level name or int value, defaults to DEBUG
* - [bubble]: bool, defaults to true
*
* - chromephp:
* - [level]: level name or int value, defaults to DEBUG
Expand Down Expand Up @@ -816,6 +824,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) {
Expand Down
19 changes: 19 additions & 0 deletions DependencyInjection/MonologExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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'],
Expand Down
22 changes: 22 additions & 0 deletions Tests/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
Loading