Skip to content

Commit

Permalink
Fixed logging settings
Browse files Browse the repository at this point in the history
  • Loading branch information
Radiergummi committed May 14, 2024
1 parent e7defc3 commit 0733100
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 24 deletions.
File renamed without changes.
13 changes: 13 additions & 0 deletions config/logging.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

return [
'channels' => [
'elasticsearch' => [
'driver' => 'stack',
'name' => 'elasticsearch',
'level' => 'warning',
],
],
];
62 changes: 45 additions & 17 deletions src/ElasticsearchServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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')
);
}

/**
Expand All @@ -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,
Expand Down Expand Up @@ -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);
}
}
9 changes: 2 additions & 7 deletions tests/LoggingTest.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
<?php
/** @noinspection PhpUnhandledExceptionInspection */

/**
* Created by PhpStorm.
* User: mike
* Date: 07/05/18
* Time: 19:58
*/
declare(strict_types=1);

namespace Matchory\Elasticsearch\Tests;

Expand All @@ -21,6 +15,7 @@ class LoggingTest extends TestCase
public function testConfigureLogging(): void
{
$client = ClientBuilder::create();

$newClientBuilder = Connection::configureLogging($client, [
'logging' => [
'enabled' => true,
Expand Down

0 comments on commit 0733100

Please sign in to comment.