Skip to content

Commit

Permalink
Create the logger on the service provider instead of on the config fi…
Browse files Browse the repository at this point in the history
…le, making the config file cacheable
  • Loading branch information
jfoulquie-tnw committed Mar 5, 2020
1 parent 975b353 commit d7f1e38
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/Shift31/LaravelElasticsearch/ElasticsearchServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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'));
});
}
Expand Down

0 comments on commit d7f1e38

Please sign in to comment.