Skip to content
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* 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
* Fix `host` configuration for `elastic_search` handler
* Add `hosts` configuration for `elastica` handler
* Add `enabled` option to `handlers` configuration

## 3.10.0 (2023-11-06)
Expand Down
3 changes: 2 additions & 1 deletion src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,7 @@ private function addElasticsearchSection(ArrayNodeDefinition $handlerNode)
->end()
->children()
->scalarNode('id')->end()
->arrayNode('hosts')->prototype('scalar')->end()->end()
->scalarNode('host')->end()
->scalarNode('port')->defaultValue(9200)->end()
->scalarNode('transport')->defaultValue('Http')->end()
Expand All @@ -916,7 +917,7 @@ private function addElasticsearchSection(ArrayNodeDefinition $handlerNode)
->end()
->validate()
->ifTrue(function ($v) {
return !isset($v['id']) && !isset($v['host']);
return !isset($v['id']) && !isset($v['host']) && !isset($v['hosts']);
})
->thenInvalid('What must be set is either the host or the id.')
->end()
Expand Down
19 changes: 13 additions & 6 deletions src/DependencyInjection/MonologExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler
$factory = class_exists('Elastic\Elasticsearch\ClientBuilder') ? 'Elastic\Elasticsearch\ClientBuilder' : 'Elasticsearch\ClientBuilder';
$client->setFactory([$factory, 'fromConfig']);
$clientArguments = [
'hosts' => [$handler['elasticsearch']['host']],
'hosts' => $handler['elasticsearch']['hosts'] ?? [$handler['elasticsearch']['host']],
];

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

$clientArguments = [
'host' => $handler['elasticsearch']['host'],
'port' => $handler['elasticsearch']['port'],
'transport' => $handler['elasticsearch']['transport'],
];
if (isset($handler['elasticsearch']['hosts'])) {
$clientArguments = [
'hosts' => $handler['elasticsearch']['hosts'],
'transport' => $handler['elasticsearch']['transport'],
];
} else {
$clientArguments = [
'host' => $handler['elasticsearch']['host'],
'port' => $handler['elasticsearch']['port'],
'transport' => $handler['elasticsearch']['transport'],
];
}

if (isset($handler['elasticsearch']['user'], $handler['elasticsearch']['password'])) {
$clientArguments['headers'] = [
Expand Down
56 changes: 56 additions & 0 deletions tests/DependencyInjection/MonologExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

use Monolog\Attribute\AsMonologProcessor;
use Monolog\Attribute\WithMonologChannel;
use Monolog\Handler\ElasticaHandler;
use Monolog\Handler\ElasticsearchHandler;
use Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy;
use Monolog\Handler\RollbarHandler;
use Monolog\Logger;
Expand Down Expand Up @@ -876,6 +878,60 @@ public function testWithLoggerChannelAutoconfiguration(): void
], $container->getDefinition(ServiceWithChannel::class)->getTag('monolog.logger'));
}

public function testElasticsearchAndElasticaHandlers()
{
if (Logger::API < 2) {
$this->markTestSkipped('Monolog >= 2 is needed.');
}

$container = new ContainerBuilder();
$container->setDefinition('elasticsearch.client', new Definition('Elasticsearch\\Client'));
$container->setDefinition('elastica.client', new Definition('Elastica\\Client'));

$config = [[
'handlers' => [
'es_handler' => [
'type' => 'elastic_search',
'elasticsearch' => [
'hosts' => ['es:9200'],
],
'index' => 'my-index',
'document_type' => 'my-type',
],
'elastica_handler' => [
'type' => 'elastica',
'elasticsearch' => [
'hosts' => ['es:9200'],
],
'index' => 'my-index',
'document_type' => 'my-type',
],
],
]];

$extension = new MonologExtension();
$extension->load($config, $container);

$this->assertTrue($container->hasDefinition('monolog.handler.es_handler'));
$this->assertTrue($container->hasDefinition('monolog.handler.elastica_handler'));

// Elasticsearch handler should receive the elasticsearch.client as first argument
$esHandler = $container->getDefinition('monolog.handler.es_handler');
$this->assertSame(ElasticsearchHandler::class,$esHandler->getClass());
$esClient = $esHandler->getArgument(0);
$this->assertInstanceOf(Definition::class, $esClient);
$this->assertStringEndsWith('Elasticsearch\Client', $esClient->getClass());
$this->assertSame(['hosts' => ['es:9200']], $esClient->getArgument(0));

// Elastica handler should receive the elastica.client as first argument
$elasticaHandler = $container->getDefinition('monolog.handler.elastica_handler');
$this->assertSame(ElasticaHandler::class,$elasticaHandler->getClass());
$elasticaClient = $elasticaHandler->getArgument(0);
$this->assertInstanceOf(Definition::class, $elasticaClient);
$this->assertSame('Elastica\Client', $elasticaClient->getClass());
$this->assertSame(['hosts' => ['es:9200'], 'transport' => 'Http'], $elasticaClient->getArgument(0));
}

protected function getContainer(array $config = [], array $thirdPartyDefinitions = []): ContainerBuilder
{
$container = new ContainerBuilder(new EnvPlaceholderParameterBag());
Expand Down
Loading