From 0733100b4490062ddbe3d281f2b35be77d72b555 Mon Sep 17 00:00:00 2001 From: Moritz Friedrich Date: Tue, 14 May 2024 17:04:18 +0200 Subject: [PATCH] Fixed logging settings --- config/{es.php => elasticsearch.php} | 0 config/logging.php | 13 ++++++ src/ElasticsearchServiceProvider.php | 62 ++++++++++++++++++++-------- tests/LoggingTest.php | 9 +--- 4 files changed, 60 insertions(+), 24 deletions(-) rename config/{es.php => elasticsearch.php} (100%) create mode 100644 config/logging.php diff --git a/config/es.php b/config/elasticsearch.php similarity index 100% rename from config/es.php rename to config/elasticsearch.php diff --git a/config/logging.php b/config/logging.php new file mode 100644 index 0000000..c41665a --- /dev/null +++ b/config/logging.php @@ -0,0 +1,13 @@ + [ + 'elasticsearch' => [ + 'driver' => 'stack', + 'name' => 'elasticsearch', + 'level' => 'warning', + ], + ], +]; diff --git a/src/ElasticsearchServiceProvider.php b/src/ElasticsearchServiceProvider.php index c150540..e3a385d 100755 --- a/src/ElasticsearchServiceProvider.php +++ b/src/ElasticsearchServiceProvider.php @@ -8,7 +8,7 @@ use Illuminate\Contracts\Container\BindingResolutionException; use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Contracts\Foundation\Application; -use Illuminate\Log\Logger; +use Illuminate\Contracts\Foundation\CachesConfiguration; use Illuminate\Support\Facades\Config; use Illuminate\Support\ServiceProvider; use Illuminate\Support\Str; @@ -28,6 +28,7 @@ use function class_exists; use function config_path; +use function dirname; use function file_exists; use function method_exists; use function trigger_deprecation; @@ -76,24 +77,29 @@ public function boot(): void $this->registerScoutEngine(); } + + /** + * @throws BindingResolutionException + */ protected function configure(): void { - if (file_exists(__DIR__ . '/../config/es.php')) { - $configPath = __DIR__ . '/../config/es.php'; + if (file_exists($this->packageConfigPath('es.php'))) { + $configPath = $this->packageConfigPath('es.php'); trigger_deprecation( 'matchory/elasticsearch', '3.0.0', 'The "es.php" configuration file is deprecated. Use "elasticsearch.php" instead.' ); } else { - $configPath = __DIR__ . '/../config/elasticsearch.php'; + $configPath = $this->packageConfigPath('elasticsearch.php'); } $configKey = basename($configPath, '.php'); $this->mergeConfigFrom($configPath, $configKey); + $this->mergeLoggingChannelsFrom($this->packageConfigPath('logging.php')); $this->publishes([ - __DIR__ . '/../config/' => config_path(), + $this->packageConfigPath() => config_path(), ], "{$configKey}.config"); // Autoconfiguration with lumen framework. @@ -123,10 +129,7 @@ protected function registerScoutEngine(): void ->setHosts($config['servers']) ->build(); - return new ScoutEngine( - $elastic, - $config['index'] - ); + return new ScoutEngine($elastic, $config['index']); }); } catch (BindingResolutionException) { // Class is not resolved. @@ -178,9 +181,12 @@ protected function registerCommands(): void */ protected function registerLogger(): void { - $this->app->bind('elasticsearch.logger', fn(Application $app) => new Logger( - $app->make('log')->channel(Config::get('elasticsearch.logger')) - )); + $this->app->bind( + 'elasticsearch.logger', + fn(Application $app) => $app + ->make(LoggerInterface::class) + ->channel('elasticsearch') + ); } /** @@ -195,11 +201,9 @@ protected function registerClientFactory(): void ClientFactory::class ); - if ($this->app->bound('elasticsearch.logger')) { - $this->app->when(ClientFactory::class) - ->needs(LoggerInterface::class) - ->give('elasticsearch.logger'); - } + $this->app->when(ClientFactory::class) + ->needs(LoggerInterface::class) + ->give('elasticsearch.logger'); $this->app->alias( ClientFactoryInterface::class, @@ -275,4 +279,28 @@ protected function registerDefaultConnection(): void 'elasticsearch.connection' ); } + + /** + * @throws BindingResolutionException + */ + private function mergeLoggingChannelsFrom(string $file): void + { + if (!($this->app instanceof CachesConfiguration && $this->app->configurationIsCached())) { + $packageLoggingConfig = require $file; + + $config = $this->app->make('config'); + $config->set( + 'logging.channels', + array_merge( + $packageLoggingConfig['channels'] ?? [], + $config->get('logging.channels', []) + ) + ); + } + } + + private function packageConfigPath(string $path = ''): string + { + return dirname(__DIR__) . '/config' . ($path ? '/' . $path : $path); + } } diff --git a/tests/LoggingTest.php b/tests/LoggingTest.php index b36dcb0..b64c479 100644 --- a/tests/LoggingTest.php +++ b/tests/LoggingTest.php @@ -1,12 +1,6 @@ [ 'enabled' => true,