Skip to content

Commit

Permalink
docs & refactor of logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeroen-G committed Sep 15, 2023
1 parent bfbbe6d commit 5caddf6
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 69 deletions.
5 changes: 5 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
7 changes: 6 additions & 1 deletion config/explorer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 17 additions & 0 deletions docs/logging.md
Original file line number Diff line number Diff line change
@@ -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'),
```
7 changes: 3 additions & 4 deletions src/ExplorerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Check failure on line 54 in src/ExplorerServiceProvider.php

View workflow job for this annotation

GitHub Actions / PHP8

Static method JeroenG\Explorer\Infrastructure\Elastic\ElasticClientBuilder::fromConfig() invoked with 2 parameters, 1 required.

Check failure on line 54 in src/ExplorerServiceProvider.php

View workflow job for this annotation

GitHub Actions / PHP8.2

Static method JeroenG\Explorer\Infrastructure\Elastic\ElasticClientBuilder::fromConfig() invoked with 2 parameters, 1 required.

Check failure on line 54 in src/ExplorerServiceProvider.php

View workflow job for this annotation

GitHub Actions / PHP8

Static method JeroenG\Explorer\Infrastructure\Elastic\ElasticClientBuilder::fromConfig() invoked with 2 parameters, 1 required.

Check failure on line 54 in src/ExplorerServiceProvider.php

View workflow job for this annotation

GitHub Actions / PHP8.2

Static method JeroenG\Explorer\Infrastructure\Elastic\ElasticClientBuilder::fromConfig() invoked with 2 parameters, 1 required.
);

$this->app->when(ElasticDocumentAdapter::class)
->needs(Client::class)
Expand Down
8 changes: 3 additions & 5 deletions src/Infrastructure/Elastic/ElasticClientBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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;
Expand Down
55 changes: 0 additions & 55 deletions tests/Support/Logger.php

This file was deleted.

37 changes: 33 additions & 4 deletions tests/Unit/ElasticClientBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
use Illuminate\Container\Container;
use JeroenG\Explorer\Infrastructure\Elastic\ElasticClientBuilder;
use JeroenG\Explorer\Tests\Support\ConfigRepository;
use JeroenG\Explorer\Tests\Support\Logger;
use Mockery\Adapter\Phpunit\MockeryTestCase;
use Psr\Log\NullLogger;

final class ElasticClientBuilderTest extends MockeryTestCase
{
Expand All @@ -21,16 +21,15 @@ final class ElasticClientBuilderTest extends MockeryTestCase
public function test_it_creates_client_with_config(array $config, ClientBuilder $expectedBuilder): void
{
$configRepository = new ConfigRepository([ 'explorer' => $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' => [
[
Expand Down Expand Up @@ -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]),
];
}
}

0 comments on commit 5caddf6

Please sign in to comment.