From 3f091eac1e3a1a615c2771d681e1c1b89e3e74ea Mon Sep 17 00:00:00 2001 From: Kevin Pfeifer Date: Mon, 30 Oct 2023 17:49:21 +0100 Subject: [PATCH 1/7] fix elasticsearch logger error --- src/Panel/SqlLogPanel.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/Panel/SqlLogPanel.php b/src/Panel/SqlLogPanel.php index b199fd225..1d0b21947 100644 --- a/src/Panel/SqlLogPanel.php +++ b/src/Panel/SqlLogPanel.php @@ -15,6 +15,7 @@ namespace DebugKit\Panel; use Cake\Core\Configure; +use Cake\Database\Driver; use Cake\Datasource\ConnectionInterface; use Cake\Datasource\ConnectionManager; use Cake\ORM\Locator\LocatorAwareTrait; @@ -57,7 +58,14 @@ public function initialize(): void ) { continue; } - $logger = $connection->getDriver()->getLogger(); + $driver = $connection->getDriver(); + $logger = null; + if ($driver instanceof Driver) { + $logger = $driver->getLogger(); + } elseif (method_exists($connection, 'getLogger')) { + // ElasticSearch connection holds the logger, not the Elastica Driver + $logger = $connection->getLogger(); + } if ($logger instanceof DebugLog) { $logger->setIncludeSchema($includeSchemaReflection); From 573aabef4acbc7b37812c8aca8615376fcd13e21 Mon Sep 17 00:00:00 2001 From: Kevin Pfeifer Date: Thu, 4 Jan 2024 11:21:42 +0100 Subject: [PATCH 2/7] don't log query if it isn't a LoggedQuery instance --- composer.json | 2 +- src/Database/Log/DebugLog.php | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 7a21b3d5c..02048abb6 100644 --- a/composer.json +++ b/composer.json @@ -30,7 +30,7 @@ "require-dev": { "cakephp/authorization": "^3.0", "cakephp/cakephp-codesniffer": "^5.0", - "phpunit/phpunit": "^10.1.0" + "phpunit/phpunit": "^10.1.0 <=10.5.3" }, "autoload": { "psr-4": { diff --git a/src/Database/Log/DebugLog.php b/src/Database/Log/DebugLog.php index 9e3acb162..c55667df0 100644 --- a/src/Database/Log/DebugLog.php +++ b/src/Database/Log/DebugLog.php @@ -133,7 +133,10 @@ public function log($level, string|Stringable $message, array $context = []): vo $this->_logger->log($level, $message, $context); } - if ($this->_includeSchema === false && $this->isSchemaQuery($query)) { + if ( + !$query instanceof LoggedQuery || + ($this->_includeSchema === false && $this->isSchemaQuery($query)) + ) { return; } From fd6a99deed95443c1f127ce3ca48923d11d8587f Mon Sep 17 00:00:00 2001 From: Kevin Pfeifer Date: Thu, 4 Jan 2024 11:29:49 +0100 Subject: [PATCH 3/7] fix psalm --- psalm-baseline.xml | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 64cd47585..4f97615d4 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -1,5 +1,10 @@ - + + + + jsonSerialize + + _composerPaths]]> From fb902b2f95b124f80948ce4d897b3e3089b4e346 Mon Sep 17 00:00:00 2001 From: Kevin Pfeifer Date: Thu, 4 Jan 2024 16:21:15 +0100 Subject: [PATCH 4/7] add elastic search specific query logging --- src/Database/Log/DebugLog.php | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Database/Log/DebugLog.php b/src/Database/Log/DebugLog.php index c55667df0..65e2d96ba 100644 --- a/src/Database/Log/DebugLog.php +++ b/src/Database/Log/DebugLog.php @@ -127,12 +127,25 @@ public function totalTime(): float */ public function log($level, string|Stringable $message, array $context = []): void { - $query = $context['query']; + $query = $context['query'] ?? null; if ($this->_logger) { $this->_logger->log($level, $message, $context); } + // This specific to Elastic Search + if (!$query instanceof LoggedQuery && isset($context['request']) && isset($context['response'])) { + $this->_totalTime += $context['response']['took']; + + $this->_queries[] = [ + 'query' => $context['request'], + 'took' => $context['response']['took'], + 'rows' => $context['response']['hits']['total'], + ]; + + return; + } + if ( !$query instanceof LoggedQuery || ($this->_includeSchema === false && $this->isSchemaQuery($query)) From d9e40c277e3066a7ef2b3be84a7f2edc383221dc Mon Sep 17 00:00:00 2001 From: Kevin Pfeifer Date: Thu, 4 Jan 2024 16:48:12 +0100 Subject: [PATCH 5/7] add elastic search specific query logging --- src/Database/Log/DebugLog.php | 2 ++ templates/element/sql_log_panel.php | 11 +++++++---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/Database/Log/DebugLog.php b/src/Database/Log/DebugLog.php index 65e2d96ba..b4e7d677c 100644 --- a/src/Database/Log/DebugLog.php +++ b/src/Database/Log/DebugLog.php @@ -138,6 +138,7 @@ public function log($level, string|Stringable $message, array $context = []): vo $this->_totalTime += $context['response']['took']; $this->_queries[] = [ + 'isSqlQuery' => false, 'query' => $context['request'], 'took' => $context['response']['took'], 'rows' => $context['response']['hits']['total'], @@ -158,6 +159,7 @@ public function log($level, string|Stringable $message, array $context = []): vo $this->_totalTime += $data['took']; $this->_queries[] = [ + 'isSqlQuery' => true, 'query' => (string)$query, 'took' => $data['took'], 'rows' => $data['numRows'], diff --git a/templates/element/sql_log_panel.php b/templates/element/sql_log_panel.php index 4fbba0c93..a0509261b 100644 --- a/templates/element/sql_log_panel.php +++ b/templates/element/sql_log_panel.php @@ -75,8 +75,9 @@ - 'style="color: #004d40;"', HtmlHighlighter::HIGHLIGHT_BACKTICK_QUOTE => 'style="color: #26a69a;"', @@ -85,8 +86,10 @@ HtmlHighlighter::HIGHLIGHT_PRE => 'style="color: #222; background-color: transparent;"', ]) )) - ->format($query['query']) - ?> + ->format($query['query']); + else: + print_r($query['query']); + endif; ?> From 77b6026060d20fb7992e226a9720997c8d1b1a5c Mon Sep 17 00:00:00 2001 From: Kevin Pfeifer Date: Thu, 4 Jan 2024 17:42:22 +0100 Subject: [PATCH 6/7] Update templates/element/sql_log_panel.php Co-authored-by: ADmad --- templates/element/sql_log_panel.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/element/sql_log_panel.php b/templates/element/sql_log_panel.php index a0509261b..95bbe3767 100644 --- a/templates/element/sql_log_panel.php +++ b/templates/element/sql_log_panel.php @@ -76,7 +76,7 @@ 'style="color: #004d40;"', From df2ac1436bd941f7672f4027d7eb64ab5277a742 Mon Sep 17 00:00:00 2001 From: Kevin Pfeifer Date: Thu, 4 Jan 2024 19:23:12 +0100 Subject: [PATCH 7/7] apply changes from Christopher-HM --- src/Database/Log/DebugLog.php | 12 +++++++----- templates/element/sql_log_panel.php | 11 ++++------- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/Database/Log/DebugLog.php b/src/Database/Log/DebugLog.php index b4e7d677c..3e069f4d7 100644 --- a/src/Database/Log/DebugLog.php +++ b/src/Database/Log/DebugLog.php @@ -138,10 +138,13 @@ public function log($level, string|Stringable $message, array $context = []): vo $this->_totalTime += $context['response']['took']; $this->_queries[] = [ - 'isSqlQuery' => false, - 'query' => $context['request'], - 'took' => $context['response']['took'], - 'rows' => $context['response']['hits']['total'], + 'query' => json_encode([ + 'method' => $context['request']['method'], + 'path' => $context['request']['path'], + 'data' => $context['request']['data'], + ], JSON_PRETTY_PRINT), + 'took' => $context['response']['took'] ?: 0, + 'rows' => $context['response']['hits']['total']['value'] ?? $context['response']['hits']['total'] ?? 0, ]; return; @@ -159,7 +162,6 @@ public function log($level, string|Stringable $message, array $context = []): vo $this->_totalTime += $data['took']; $this->_queries[] = [ - 'isSqlQuery' => true, 'query' => (string)$query, 'took' => $data['took'], 'rows' => $data['numRows'], diff --git a/templates/element/sql_log_panel.php b/templates/element/sql_log_panel.php index 95bbe3767..4fbba0c93 100644 --- a/templates/element/sql_log_panel.php +++ b/templates/element/sql_log_panel.php @@ -75,9 +75,8 @@ - 'style="color: #004d40;"', HtmlHighlighter::HIGHLIGHT_BACKTICK_QUOTE => 'style="color: #26a69a;"', @@ -86,10 +85,8 @@ HtmlHighlighter::HIGHLIGHT_PRE => 'style="color: #222; background-color: transparent;"', ]) )) - ->format($query['query']); - else: - print_r($query['query']); - endif; ?> + ->format($query['query']) + ?>