Skip to content

Commit 2a6d971

Browse files
committed
Fix 'hosts' key in Elastic 8 configuration
1 parent 26ecfa2 commit 2a6d971

File tree

4 files changed

+72
-7
lines changed

4 files changed

+72
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* Deprecate `sentry` and `raven` handler, use a `service` handler with [`sentry/sentry-symfony`](https://docs.sentry.io/platforms/php/guides/symfony/logs/) instead
1515
* Add configuration for Gelf encoders
1616
* Fix `host` configuration for `elastic_search` handler
17+
* Add `hosts` configuration for `elastica` handler
1718
* Add `enabled` option to `handlers` configuration
1819

1920
## 3.10.0 (2023-11-06)

src/DependencyInjection/Configuration.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -908,6 +908,7 @@ private function addElasticsearchSection(ArrayNodeDefinition $handlerNode)
908908
->end()
909909
->children()
910910
->scalarNode('id')->end()
911+
->arrayNode('hosts')->prototype('scalar')->end()->end()
911912
->scalarNode('host')->end()
912913
->scalarNode('port')->defaultValue(9200)->end()
913914
->scalarNode('transport')->defaultValue('Http')->end()
@@ -916,7 +917,7 @@ private function addElasticsearchSection(ArrayNodeDefinition $handlerNode)
916917
->end()
917918
->validate()
918919
->ifTrue(function ($v) {
919-
return !isset($v['id']) && !isset($v['host']);
920+
return !isset($v['id']) && !isset($v['host']) && !isset($v['hosts']);
920921
})
921922
->thenInvalid('What must be set is either the host or the id.')
922923
->end()

src/DependencyInjection/MonologExtension.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler
307307
$factory = class_exists('Elastic\Elasticsearch\ClientBuilder') ? 'Elastic\Elasticsearch\ClientBuilder' : 'Elasticsearch\ClientBuilder';
308308
$client->setFactory([$factory, 'fromConfig']);
309309
$clientArguments = [
310-
'hosts' => [$handler['elasticsearch']['host']],
310+
'hosts' => $handler['elasticsearch']['hosts'] ?? [$handler['elasticsearch']['host']],
311311
];
312312

313313
if (isset($handler['elasticsearch']['user'], $handler['elasticsearch']['password'])) {
@@ -316,11 +316,18 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler
316316
} else {
317317
$client = new Definition('Elastica\Client');
318318

319-
$clientArguments = [
320-
'host' => $handler['elasticsearch']['host'],
321-
'port' => $handler['elasticsearch']['port'],
322-
'transport' => $handler['elasticsearch']['transport'],
323-
];
319+
if (isset($handler['elasticsearch']['hosts'])) {
320+
$clientArguments = [
321+
'hosts' => $handler['elasticsearch']['hosts'],
322+
'transport' => $handler['elasticsearch']['transport'],
323+
];
324+
} else {
325+
$clientArguments = [
326+
'host' => $handler['elasticsearch']['host'],
327+
'port' => $handler['elasticsearch']['port'],
328+
'transport' => $handler['elasticsearch']['transport'],
329+
];
330+
}
324331

325332
if (isset($handler['elasticsearch']['user'], $handler['elasticsearch']['password'])) {
326333
$clientArguments['headers'] = [

tests/DependencyInjection/MonologExtensionTest.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
use Monolog\Attribute\AsMonologProcessor;
1515
use Monolog\Attribute\WithMonologChannel;
16+
use Monolog\Handler\ElasticaHandler;
17+
use Monolog\Handler\ElasticsearchHandler;
1618
use Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy;
1719
use Monolog\Handler\RollbarHandler;
1820
use Monolog\Logger;
@@ -876,6 +878,60 @@ public function testWithLoggerChannelAutoconfiguration(): void
876878
], $container->getDefinition(ServiceWithChannel::class)->getTag('monolog.logger'));
877879
}
878880

881+
public function testElasticsearchAndElasticaHandlers()
882+
{
883+
if (Logger::API < 2) {
884+
$this->markTestSkipped('Monolog >= 2 is needed.');
885+
}
886+
887+
$container = new ContainerBuilder();
888+
$container->setDefinition('elasticsearch.client', new Definition('Elasticsearch\\Client'));
889+
$container->setDefinition('elastica.client', new Definition('Elastica\\Client'));
890+
891+
$config = [[
892+
'handlers' => [
893+
'es_handler' => [
894+
'type' => 'elastic_search',
895+
'elasticsearch' => [
896+
'hosts' => ['es:9200'],
897+
],
898+
'index' => 'my-index',
899+
'document_type' => 'my-type',
900+
],
901+
'elastica_handler' => [
902+
'type' => 'elastica',
903+
'elasticsearch' => [
904+
'hosts' => ['es:9200'],
905+
],
906+
'index' => 'my-index',
907+
'document_type' => 'my-type',
908+
],
909+
],
910+
]];
911+
912+
$extension = new MonologExtension();
913+
$extension->load($config, $container);
914+
915+
$this->assertTrue($container->hasDefinition('monolog.handler.es_handler'));
916+
$this->assertTrue($container->hasDefinition('monolog.handler.elastica_handler'));
917+
918+
// Elasticsearch handler should receive the elasticsearch.client as first argument
919+
$esHandler = $container->getDefinition('monolog.handler.es_handler');
920+
$this->assertSame(ElasticsearchHandler::class,$esHandler->getClass());
921+
$esClient = $esHandler->getArgument(0);
922+
$this->assertInstanceOf(Definition::class, $esClient);
923+
$this->assertStringEndsWith('Elasticsearch\Client', $esClient->getClass());
924+
$this->assertSame(['hosts' => ['es:9200']], $esClient->getArgument(0));
925+
926+
// Elastica handler should receive the elastica.client as first argument
927+
$elasticaHandler = $container->getDefinition('monolog.handler.elastica_handler');
928+
$this->assertSame(ElasticaHandler::class,$elasticaHandler->getClass());
929+
$elasticaClient = $elasticaHandler->getArgument(0);
930+
$this->assertInstanceOf(Definition::class, $elasticaClient);
931+
$this->assertSame('Elastica\Client', $elasticaClient->getClass());
932+
$this->assertSame(['hosts' => ['es:9200'], 'transport' => 'Http'], $elasticaClient->getArgument(0));
933+
}
934+
879935
protected function getContainer(array $config = [], array $thirdPartyDefinitions = []): ContainerBuilder
880936
{
881937
$container = new ContainerBuilder(new EnvPlaceholderParameterBag());

0 commit comments

Comments
 (0)