Skip to content

Commit b3b0cad

Browse files
committed
feature #547 Fix hosts key in Elastic 8 configuration (GromNaN)
This PR was merged into the 4.x branch. Discussion ---------- Fix `hosts` key in Elastic 8 configuration | Q | A | ------------- | --- | Branch? | 3.x | Bug fix? | yes | New feature? | yes | Deprecations? | no | Issues | Fix #499 | License | MIT Elastica 8+ client has an option `hosts` instead of `host` (ruflin/Elastica#2188). Adding a new `monolog.handlers.(elastica|elastic_search).elasticsearch.hosts` setting that is a list of strings. This way, we don't try to detect which version of elastica is used, an let the developers set the correct configuration depending on the version they have installed. Commits ------- a7f8dc0 Fix 'hosts' key in Elastic 8 configuration
2 parents fcd3680 + a7f8dc0 commit b3b0cad

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

src/DependencyInjection/Configuration.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,7 @@ private function addElasticsearchSection(ArrayNodeDefinition $handlerNode): void
782782
->end()
783783
->children()
784784
->scalarNode('id')->end()
785+
->arrayNode('hosts')->prototype('scalar')->end()->end()
785786
->scalarNode('host')->end()
786787
->scalarNode('port')->defaultValue(9200)->end()
787788
->scalarNode('transport')->defaultValue('Http')->end()
@@ -790,7 +791,7 @@ private function addElasticsearchSection(ArrayNodeDefinition $handlerNode): void
790791
->end()
791792
->validate()
792793
->ifTrue(function ($v) {
793-
return !isset($v['id']) && !isset($v['host']);
794+
return !isset($v['id']) && !isset($v['host']) && !isset($v['hosts']);
794795
})
795796
->thenInvalid('What must be set is either the host or the id.')
796797
->end()

src/DependencyInjection/MonologExtension.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ private function buildHandler(ContainerBuilder $container, string $name, array $
268268
$factory = class_exists('Elastic\Elasticsearch\ClientBuilder') ? 'Elastic\Elasticsearch\ClientBuilder' : 'Elasticsearch\ClientBuilder';
269269
$client->setFactory([$factory, 'fromConfig']);
270270
$clientArguments = [
271-
'hosts' => [$handler['elasticsearch']['host']],
271+
'hosts' => $handler['elasticsearch']['hosts'] ?? [$handler['elasticsearch']['host']],
272272
];
273273

274274
if (isset($handler['elasticsearch']['user'], $handler['elasticsearch']['password'])) {
@@ -277,11 +277,18 @@ private function buildHandler(ContainerBuilder $container, string $name, array $
277277
} else {
278278
$client = new Definition('Elastica\Client');
279279

280-
$clientArguments = [
281-
'host' => $handler['elasticsearch']['host'],
282-
'port' => $handler['elasticsearch']['port'],
283-
'transport' => $handler['elasticsearch']['transport'],
284-
];
280+
if (isset($handler['elasticsearch']['hosts'])) {
281+
$clientArguments = [
282+
'hosts' => $handler['elasticsearch']['hosts'],
283+
'transport' => $handler['elasticsearch']['transport'],
284+
];
285+
} else {
286+
$clientArguments = [
287+
'host' => $handler['elasticsearch']['host'],
288+
'port' => $handler['elasticsearch']['port'],
289+
'transport' => $handler['elasticsearch']['transport'],
290+
];
291+
}
285292

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

0 commit comments

Comments
 (0)