Skip to content

Commit 4be10cf

Browse files
committed
feat(logging): enable configurable logging for Elasticsearch connections
- Added 'logging' option to Elasticsearch connection config. - Enabled conditional logging setup in '_builderOptions' based on config. - Ensured logging can be toggled via configuration settings. Because logging should be more than just a silent observer now! 📜
1 parent d10a458 commit 4be10cf

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

src/Connection.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Elastic\Elasticsearch\ClientBuilder;
99
use Elastic\Elasticsearch\Exception\AuthenticationException;
1010
use Illuminate\Database\Connection as BaseConnection;
11+
use Illuminate\Support\Facades\Log;
1112
use Illuminate\Support\Str;
1213
use PDPhilip\Elasticsearch\DSL\Bridge;
1314
use PDPhilip\Elasticsearch\DSL\Results;
@@ -289,6 +290,7 @@ private function _sanitizeConfig(): void
289290
'api_id' => null,
290291
'index_prefix' => null,
291292
'options' => [
293+
'logging' => false,
292294
'allow_id_sort' => false,
293295
'ssl_verification' => true,
294296
'retires' => null,
@@ -375,25 +377,29 @@ private function _validateConnection(): void
375377
*/
376378
protected function _builderOptions(ClientBuilder $cb): ClientBuilder
377379
{
378-
$cb->setSSLVerification($this->sslVerification);
380+
$cb = $cb->setSSLVerification($this->sslVerification);
379381
if (isset($this->elasticMetaHeader)) {
380-
$cb->setElasticMetaHeader($this->elasticMetaHeader);
382+
$cb = $cb->setElasticMetaHeader($this->elasticMetaHeader);
381383
}
382384

383385
if (isset($this->retires)) {
384-
$cb->setRetries($this->retires);
386+
$cb = $cb->setRetries($this->retires);
387+
}
388+
389+
if ($this->config['options']['logging']) {
390+
$cb = $cb->setLogger(Log::getLogger());
385391
}
386392

387393
if ($this->config['ssl_cert']) {
388-
$cb->setCABundle($this->config['ssl_cert']);
394+
$cb = $cb->setCABundle($this->config['ssl_cert']);
389395
}
390396

391397
if ($this->config['ssl']['cert']) {
392-
$cb->setSSLCert($this->config['ssl']['cert'], $this->config['ssl']['cert_password']);
398+
$cb = $cb->setSSLCert($this->config['ssl']['cert'], $this->config['ssl']['cert_password']);
393399
}
394400

395401
if ($this->config['ssl']['key']) {
396-
$cb->setSSLKey($this->config['ssl']['key'], $this->config['ssl']['key_password']);
402+
$cb = $cb->setSSLKey($this->config['ssl']['key'], $this->config['ssl']['key_password']);
397403
}
398404

399405
return $cb;

tests/TestCase.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ protected function getEnvironmentSetUp($app): void
4545
'driver' => 'elasticsearch',
4646
'auth_type' => 'http',
4747
'hosts' => ['http://localhost:9200'],
48+
'options' => [
49+
'logging' => true
50+
]
4851
]);
4952
}
5053
}

0 commit comments

Comments
 (0)