diff --git a/changelog.md b/changelog.md index 10c6a66..52485b5 100644 --- a/changelog.md +++ b/changelog.md @@ -6,8 +6,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased +## [3.8.0] + ### Added - Updating the index alias is now done through a (queueable) job. +- Nested aggregations in the results. +- Option to enable a PSR-3 compliant logger. +- Allow custom order by (as a syntax object). ## [3.7.0] diff --git a/config/explorer.php b/config/explorer.php index 76349bc..e1b69d1 100755 --- a/config/explorer.php +++ b/config/explorer.php @@ -38,5 +38,10 @@ */ 'prune_old_aliases' => true, - 'logger' => env('EXPLORER_ELASTIC_LOGGER_ENABLED', false), + /** + * When set to true, sends all the logs (requests, responses, etc.) from the Elasticsearch PHP SDK + * to a PSR-3 logger. Disabled by default for performance. + */ + 'logging' => env('EXPLORER_ELASTIC_LOGGER_ENABLED', false), + 'logger' => null, ]; diff --git a/docs/index.md b/docs/index.md index c16fc0a..7c701ed 100644 --- a/docs/index.md +++ b/docs/index.md @@ -29,6 +29,7 @@ Also do not forget to follow the [installation instructions for Laravel Scout](h - [Sorting search results](sorting.md) - [Pagination and search result size](pagination.md) - [Debugging](debugging.md) +- [Logging](logging.md) - [Testing](testing.md) - [Console commands](commands.md) - [Text analysis](text-analysis.md) diff --git a/docs/logging.md b/docs/logging.md new file mode 100644 index 0000000..98a9bf5 --- /dev/null +++ b/docs/logging.md @@ -0,0 +1,17 @@ +# Logging + +When enabled the Elastic SDK will send all its logs (such as requests and responses) to a PSR-3 logger. +By default the logger is disabled for performance. +To enable the logger set `EXPLORER_ELASTIC_LOGGER_ENABLED=true` in your environment variables and edit your `explorer.php` config to define a logger (see below for examples). + +See also the [SDK](https://github.com/elastic/elasticsearch-php/blob/main/docs/logger.asciidoc) docs. +More information on loggers in Laravel can be found in [Laravel's docs](https://laravel.com/docs/logging). + +Examples: +```php +'logger' => new \Psr\Log\NullLogger(), +``` + +```php +'logger' => \Illuminate\Support\Facades\Log::channel('daily'), +``` diff --git a/src/ExplorerServiceProvider.php b/src/ExplorerServiceProvider.php index 1a15b95..b88ce2f 100644 --- a/src/ExplorerServiceProvider.php +++ b/src/ExplorerServiceProvider.php @@ -50,10 +50,9 @@ public function boot(): void $this->app->when(ElasticClientFactory::class) ->needs(Client::class) - ->give(static fn () => ElasticClientBuilder::fromConfig( - config(), - logger() - )->build()); + ->give( + static fn () => ElasticClientBuilder::fromConfig(config(), logger())->build() + ); $this->app->when(ElasticDocumentAdapter::class) ->needs(Client::class) diff --git a/src/Infrastructure/Elastic/ElasticClientBuilder.php b/src/Infrastructure/Elastic/ElasticClientBuilder.php index b223159..e824f4c 100644 --- a/src/Infrastructure/Elastic/ElasticClientBuilder.php +++ b/src/Infrastructure/Elastic/ElasticClientBuilder.php @@ -4,16 +4,14 @@ namespace JeroenG\Explorer\Infrastructure\Elastic; -use Elasticsearch\Client; use Elasticsearch\ClientBuilder; use Illuminate\Contracts\Config\Repository; -use Psr\Log\LoggerInterface; final class ElasticClientBuilder { private const HOST_KEYS = ['host', 'port', 'scheme']; - public static function fromConfig(Repository $config, LoggerInterface $logger): ClientBuilder + public static function fromConfig(Repository $config): ClientBuilder { $builder = ClientBuilder::create(); @@ -66,8 +64,8 @@ public static function fromConfig(Repository $config, LoggerInterface $logger): $builder->setSSLCert($path, $password); } - if($config->get('explorer.logger', false)) { - $builder->setLogger($logger); + if($config->get('explorer.logging', false) && $config->has('explorer.logger')) { + $builder->setLogger($config->get('explorer.logger')); } return $builder; diff --git a/tests/Support/Logger.php b/tests/Support/Logger.php deleted file mode 100644 index c0a07bc..0000000 --- a/tests/Support/Logger.php +++ /dev/null @@ -1,55 +0,0 @@ - $config ]); - $logger = new Logger(); Container::getInstance()->instance('config', $configRepository); - $resultBuilder = ElasticClientBuilder::fromConfig($configRepository, $logger); + $resultBuilder = ElasticClientBuilder::fromConfig($configRepository); self::assertEquals($expectedBuilder, $resultBuilder); } - public function provideClientConfigs() + public function provideClientConfigs(): ?\Generator { yield 'simple host' => [ [ @@ -152,5 +151,35 @@ public function provideClientConfigs() ->setSSLCert('path/to/cert.pem') ->setSSLKey('path/to/key.pem'), ]; + + yield 'with logging' => [ + [ + 'logging' => true, + 'logger' => new NullLogger(), + 'connection' => self::CONNECTION, + ], + ClientBuilder::create() + ->setHosts([self::CONNECTION]) + ->setLogger(new NullLogger()), + ]; + + yield 'without logging' => [ + [ + 'logging' => false, + 'logger' => new NullLogger(), + 'connection' => self::CONNECTION, + ], + ClientBuilder::create() + ->setHosts([self::CONNECTION]), + ]; + + yield 'without logger' => [ + [ + 'logger' => new NullLogger(), + 'connection' => self::CONNECTION, + ], + ClientBuilder::create() + ->setHosts([self::CONNECTION]), + ]; } }