Skip to content
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* Drop support for Symfony < 6.4
* Add TelegramBotHandler `topic` support
* Deprecate `sentry` and `raven` handler, use a `service` handler with [`sentry/sentry-symfony`](https://docs.sentry.io/platforms/php/guides/symfony/logs/) instead
* Add configuration for Gelf encoders

## 3.10.0 (2023-11-06)

Expand Down
11 changes: 10 additions & 1 deletion src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,15 @@
* - [bubble]: bool, defaults to true
*
* - gelf:
* - publisher: {id: ...} or {hostname: ..., port: ..., chunk_size: ...}
* - 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
*
Expand Down Expand Up @@ -831,6 +839,7 @@ private function addGelfSection(ArrayNodeDefinition $handlerNode)
->scalarNode('hostname')->end()
->scalarNode('port')->defaultValue(12201)->end()
->scalarNode('chunk_size')->defaultValue(1420)->end()
->enumNode('encoder')->values(['json', 'compressed_json'])->end()
->end()
->validate()
->ifTrue(function ($v) {
Expand Down
18 changes: 18 additions & 0 deletions src/DependencyInjection/MonologExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,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 \RuntimeException('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 \RuntimeException('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 @@ -111,6 +111,28 @@ public function testGelfPublisherService($publisher)
$this->assertEquals('gelf.publisher', $config['handlers']['gelf']['publisher']['id']);
}

public function testGelfPublisherWithEncoder()
{
$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