Skip to content

Commit ae0d5e2

Browse files
committed
Improve logging
1 parent 5fd950a commit ae0d5e2

6 files changed

Lines changed: 204 additions & 195 deletions

File tree

src/Datasource/Log/ElasticLogger.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727
use Stringable;
2828

2929
/**
30-
* Adapter to convert logs to QueryLogger readable content
30+
* Adapter to convert Elastica logs to QueryLogger readable content
3131
*
32-
* Handles the new elasticsearch-php package logging format with PSR-7 requests/responses.
32+
* Handles the elasticsearch-php package logging format with PSR-7 requests/responses.
3333
*/
3434
class ElasticLogger extends AbstractLogger
3535
{
@@ -43,6 +43,11 @@ class ElasticLogger extends AbstractLogger
4343
*/
4444
protected Connection $_connection;
4545

46+
/**
47+
* Track previous log to avoid duplicates
48+
*/
49+
protected ?string $_lastLogHash = null;
50+
4651
/**
4752
* Constructor, set the QueryLogger instance
4853
*
@@ -95,7 +100,7 @@ public function log(mixed $level, Stringable|string $message, array $context = [
95100
* them to the cake defined logger instance
96101
*
97102
* Elastica log parameters with elasticsearch-php package:
98-
* -----------------------------------------------------------
103+
* --------------------------------------------------------
99104
* error:
100105
* message: "Elastica Request Failure"
101106
* context: [ exception, request, retry ]
@@ -112,9 +117,22 @@ public function log(mixed $level, Stringable|string $message, array $context = [
112117
*/
113118
protected function _log(string $level, string $message, array $context = []): void
114119
{
120+
// Skip empty contexts to reduce noise
121+
if ($context === []) {
122+
return;
123+
}
124+
115125
$logData = $this->extractLogData($context);
116126
$logDataJson = json_encode($logData, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) ?: '{}';
117127

128+
// Simple duplicate detection
129+
$logHash = md5($level . $logDataJson);
130+
if ($logHash === $this->_lastLogHash) {
131+
return;
132+
}
133+
134+
$this->_lastLogHash = $logHash;
135+
118136
if ($this->hasRequestResponse($context)) {
119137
$queryMetrics = $this->extractQueryMetrics($context);
120138

@@ -141,7 +159,6 @@ protected function _log(string $level, string $message, array $context = []): vo
141159
protected function extractLogData(array $context): array
142160
{
143161
if (!isset($context['request'])) {
144-
// Return original context when no request data to extract
145162
return $context;
146163
}
147164

src/TestSuite/Fixture/DeleteQueryStrategy.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function teardownTest(): void
7676
$esIndex->deleteByQuery(new MatchAll());
7777
} catch (ClientResponseException $e) {
7878
// Ignore version conflicts during test cleanup
79-
// ElasticSearch 9.x is stricter about optimistic concurrency control
79+
// ElasticSearch is stricter about optimistic concurrency control
8080
if ($e->getCode() !== 409) {
8181
throw $e;
8282
}

tests/TestCase/Datasource/ConnectionTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public function testConstructLogOption(): void
8989
*/
9090
public function testQueryLoggingWithLog(): void
9191
{
92-
// Create a connection with logging enabled using modern Elastica 9.x config
92+
// Create a connection with logging enabled using modern Elastica config
9393
/** @var Connection $connection */
9494
$connection = ConnectionManager::get('test');
9595
$connection->enableQueryLogging();
@@ -108,7 +108,7 @@ public function testQueryLoggingWithLog(): void
108108
}
109109

110110
/**
111-
* Ensure that QueryLogger integration works with Elastica 9.x.
111+
* Ensure that QueryLogger integration works with Elastica.
112112
*/
113113
public function testLoggerQueryLogger(): void
114114
{

0 commit comments

Comments
 (0)