2727use 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 */
3434class 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
0 commit comments