From d7f1e38e43b8f853638a74818793caee394d5b64 Mon Sep 17 00:00:00 2001 From: Julio Foulquie Date: Thu, 5 Mar 2020 17:08:51 -0300 Subject: [PATCH] Create the logger on the service provider instead of on the config file, making the config file cacheable --- .../ElasticsearchServiceProvider.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/Shift31/LaravelElasticsearch/ElasticsearchServiceProvider.php b/src/Shift31/LaravelElasticsearch/ElasticsearchServiceProvider.php index 5e40663..9a3bca3 100644 --- a/src/Shift31/LaravelElasticsearch/ElasticsearchServiceProvider.php +++ b/src/Shift31/LaravelElasticsearch/ElasticsearchServiceProvider.php @@ -2,7 +2,9 @@ namespace Shift31\LaravelElasticsearch; use Elasticsearch\ClientBuilder; +use Illuminate\Support\Arr; use Illuminate\Support\ServiceProvider; +use Monolog\Logger; class ElasticsearchServiceProvider extends ServiceProvider { @@ -22,6 +24,18 @@ public function register() { $this->mergeConfigFrom(realpath(__DIR__ . '/../../config/elasticsearch.php'), 'elasticsearch'); $this->app->singleton('elasticsearch', function ($app) { + if (empty($app->config->get('elasticsearch.logger'))) { + $config = $app->config->get('elasticsearch'); + + $logPath = Arr::get($config, 'elasticsearch.logPath', storage_path('logs/elastic-search.log')); + $logLevel = Arr::get($config, 'elasticsearch.logLevel', Logger::WARNING); + $logger = ClientBuilder::defaultLogger($logPath, $logLevel); + unset($config['logLevel']); + unset($config['logPath']); + $config['logger'] = $logger; + $app->config->set('elasticsearch', $config); + } + return ClientBuilder::fromConfig($app->config->get('elasticsearch')); }); }