Skip to content

Commit

Permalink
5.x: fix elasticsearch logger error (#971)
Browse files Browse the repository at this point in the history
* fix elasticsearch logger error

* don't log query if it isn't a LoggedQuery instance

* fix psalm

* add elastic search specific query logging

* add elastic search specific query logging

* Update templates/element/sql_log_panel.php

Co-authored-by: ADmad <[email protected]>

* apply changes from Christopher-HM

---------

Co-authored-by: ADmad <[email protected]>
  • Loading branch information
LordSimal and ADmad authored Jan 4, 2024
1 parent a9da550 commit 8cad9af
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 5 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,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"
},
"suggest": {
"ext-pdo_sqlite": "DebugKit needs to store panel data in a database. SQLite is simple and easy to use."
Expand Down
7 changes: 6 additions & 1 deletion psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="5.10.0@a5effd2d2dddd1a7ea7a0f6a051ce63ff979e356">
<files psalm-version="5.13.1@086b94371304750d1c673315321a55d15fc59015">
<file src="src/Database/Log/DebugLog.php">
<InternalMethod>
<code>jsonSerialize</code>
</InternalMethod>
</file>
<file src="src/DebugInclude.php">
<PossiblyNullArrayOffset>
<code><![CDATA[$this->_composerPaths]]></code>
Expand Down
24 changes: 22 additions & 2 deletions src/Database/Log/DebugLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,33 @@ 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);
}

if ($this->_includeSchema === false && $this->isSchemaQuery($query)) {
// This specific to Elastic Search
if (!$query instanceof LoggedQuery && isset($context['request']) && isset($context['response'])) {
$this->_totalTime += $context['response']['took'];

$this->_queries[] = [
'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;
}

if (
!$query instanceof LoggedQuery ||
($this->_includeSchema === false && $this->isSchemaQuery($query))
) {
return;
}

Expand Down
10 changes: 9 additions & 1 deletion src/Panel/SqlLogPanel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 8cad9af

Please sign in to comment.