Skip to content

Commit 03a655e

Browse files
authored
Merge pull request #990 from cakephp/fix-989
Improve log integration for elasticearch
2 parents cdb3138 + dee4a13 commit 03a655e

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

src/Database/Log/DebugLog.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,15 +135,16 @@ public function log($level, string|Stringable $message, array $context = []): vo
135135

136136
// This specific to Elastic Search
137137
if (!$query instanceof LoggedQuery && isset($context['request']) && isset($context['response'])) {
138-
$this->_totalTime += $context['response']['took'];
138+
$took = $context['response']['took'] ?? 0;
139+
$this->_totalTime += $took;
139140

140141
$this->_queries[] = [
141142
'query' => json_encode([
142143
'method' => $context['request']['method'],
143144
'path' => $context['request']['path'],
144145
'data' => $context['request']['data'],
145146
], JSON_PRETTY_PRINT),
146-
'took' => $context['response']['took'] ?: 0,
147+
'took' => $took,
147148
'rows' => $context['response']['hits']['total']['value'] ?? $context['response']['hits']['total'] ?? 0,
148149
];
149150

tests/TestCase/Database/Log/DebugLogTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,47 @@ public function testLog()
6666
$this->assertSame(20.0, $this->logger->totalTime());
6767
}
6868

69+
/**
70+
* Test logs being stored.
71+
*
72+
* @return void
73+
*/
74+
public function testLogElastic()
75+
{
76+
$this->assertCount(0, $this->logger->queries());
77+
78+
$this->logger->log(LogLevel::DEBUG, 'title:Good', [
79+
'query' => 'title:Good',
80+
'request' => [
81+
'method' => 'GET',
82+
'path' => '/articles/_search',
83+
'data' => '',
84+
],
85+
'response' => [
86+
'took' => 10,
87+
'hits' => [
88+
'total' => 15,
89+
],
90+
],
91+
]);
92+
$this->assertCount(1, $this->logger->queries());
93+
$this->assertEquals(10, $this->logger->totalTime());
94+
95+
$this->logger->log(LogLevel::DEBUG, 'title:Best', [
96+
'query' => 'title:Best',
97+
'request' => [
98+
'method' => 'GET',
99+
'path' => '/articles/_search',
100+
'data' => '',
101+
],
102+
'response' => [
103+
'lol' => 'nope',
104+
],
105+
]);
106+
$this->assertCount(2, $this->logger->queries());
107+
$this->assertEquals(10, $this->logger->totalTime());
108+
}
109+
69110
/**
70111
* Test log ignores schema reflection
71112
*

0 commit comments

Comments
 (0)