Skip to content

Commit

Permalink
Merge pull request #990 from cakephp/fix-989
Browse files Browse the repository at this point in the history
Improve log integration for elasticearch
  • Loading branch information
othercorey authored Jan 25, 2024
2 parents cdb3138 + dee4a13 commit 03a655e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/Database/Log/DebugLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,15 +135,16 @@ public function log($level, string|Stringable $message, array $context = []): vo

// This specific to Elastic Search
if (!$query instanceof LoggedQuery && isset($context['request']) && isset($context['response'])) {
$this->_totalTime += $context['response']['took'];
$took = $context['response']['took'] ?? 0;
$this->_totalTime += $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,
'took' => $took,
'rows' => $context['response']['hits']['total']['value'] ?? $context['response']['hits']['total'] ?? 0,
];

Expand Down
41 changes: 41 additions & 0 deletions tests/TestCase/Database/Log/DebugLogTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,47 @@ public function testLog()
$this->assertSame(20.0, $this->logger->totalTime());
}

/**
* Test logs being stored.
*
* @return void
*/
public function testLogElastic()
{
$this->assertCount(0, $this->logger->queries());

$this->logger->log(LogLevel::DEBUG, 'title:Good', [
'query' => 'title:Good',
'request' => [
'method' => 'GET',
'path' => '/articles/_search',
'data' => '',
],
'response' => [
'took' => 10,
'hits' => [
'total' => 15,
],
],
]);
$this->assertCount(1, $this->logger->queries());
$this->assertEquals(10, $this->logger->totalTime());

$this->logger->log(LogLevel::DEBUG, 'title:Best', [
'query' => 'title:Best',
'request' => [
'method' => 'GET',
'path' => '/articles/_search',
'data' => '',
],
'response' => [
'lol' => 'nope',
],
]);
$this->assertCount(2, $this->logger->queries());
$this->assertEquals(10, $this->logger->totalTime());
}

/**
* Test log ignores schema reflection
*
Expand Down

0 comments on commit 03a655e

Please sign in to comment.