-
Notifications
You must be signed in to change notification settings - Fork 567
Expand file tree
/
Copy pathDebugLog.php
More file actions
223 lines (195 loc) · 5.93 KB
/
DebugLog.php
File metadata and controls
223 lines (195 loc) · 5.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<?php
declare(strict_types=1);
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since 3.0
* @license https://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace DebugKit\Database\Log;
use Cake\Database\Log\LoggedQuery;
use Psr\Log\AbstractLogger;
use Psr\Log\LoggerInterface;
use Stringable;
/**
* DebugKit Query logger.
*
* This logger decorates the existing logger if it exists,
* and stores log messages internally so they can be displayed
* or stored for future use.
*/
class DebugLog extends AbstractLogger
{
/**
* Logs from the current request.
*
* @var array
*/
protected array $_queries = [];
/**
* Decorated logger.
*
* @var \Psr\Log\LoggerInterface|null
*/
protected ?LoggerInterface $_logger = null;
/**
* Name of the connection being logged.
*
* @var string
*/
protected string $_connectionName;
/**
* Total time (ms) of all queries
*
* @var float
*/
protected float $_totalTime = 0;
/**
* Set to true to capture schema reflection queries
* in the SQL log panel.
*
* @var bool
*/
protected bool $_includeSchema = false;
/**
* Whether a transaction is currently open or not.
*
* @var bool
*/
protected bool $inTransaction = false;
/**
* Constructor
*
* @param \Psr\Log\LoggerInterface|null $logger The logger to decorate and spy on.
* @param string $name The name of the connection being logged.
* @param bool $includeSchema Whether or not schema reflection should be included.
*/
public function __construct(?LoggerInterface $logger, string $name, bool $includeSchema = false)
{
$this->_logger = $logger;
$this->_connectionName = $name;
$this->_includeSchema = $includeSchema;
}
/**
* Set the schema include flag.
*
* @param bool $value Set
* @return $this
*/
public function setIncludeSchema(bool $value)
{
$this->_includeSchema = $value;
return $this;
}
/**
* Get the connection name.
*
* @return string
*/
public function name(): string
{
return $this->_connectionName;
}
/**
* Get the stored logs.
*
* @return array
*/
public function queries(): array
{
return $this->_queries;
}
/**
* Get the total time
*
* @return float
*/
public function totalTime(): float
{
return $this->_totalTime;
}
/**
* @inheritDoc
*/
public function log($level, string|Stringable $message, array $context = []): void
{
/** @var \Cake\Database\Log\LoggedQuery|object|null $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'])) {
$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' => $took,
'rows' => $context['response']['hits']['total']['value'] ?? $context['response']['hits']['total'] ?? 0,
'inTransaction' => $this->inTransaction,
'isCommitOrRollback' => false,
'role' => '',
];
return;
}
if (
!$query instanceof LoggedQuery ||
($this->_includeSchema === false && $this->isSchemaQuery($query))
) {
return;
}
$data = $query->jsonSerialize();
$this->_totalTime += $data['took'];
$sql = (string)$query;
$isBegin = $sql === 'BEGIN';
$isCommitOrRollback = $sql === 'COMMIT' || $sql === 'ROLLBACK';
if ($isBegin) {
$this->inTransaction = true;
}
$this->_queries[] = [
'query' => $sql,
'took' => $data['took'],
'rows' => $data['numRows'],
'inTransaction' => $this->inTransaction,
'isCommitOrRollback' => $isCommitOrRollback,
'role' => $query->getContext()['role'],
];
if ($isCommitOrRollback) {
$this->inTransaction = false;
}
}
/**
* Sniff SQL statements for things only found in schema reflection.
*
* @param \Cake\Database\Log\LoggedQuery $query The query to check.
* @return bool
*/
protected function isSchemaQuery(LoggedQuery $query): bool
{
$querystring = $query->jsonSerialize()['query'];
return // Multiple engines
strpos($querystring, 'FROM information_schema') !== false ||
// Postgres
strpos($querystring, 'FROM pg_catalog') !== false ||
// MySQL
strpos($querystring, 'SHOW TABLE') === 0 ||
strpos($querystring, 'SHOW FULL COLUMNS') === 0 ||
strpos($querystring, 'SHOW INDEXES') === 0 ||
// Sqlite
strpos($querystring, 'FROM sqlite_master') !== false ||
strpos($querystring, 'PRAGMA') === 0 ||
// Sqlserver
strpos($querystring, 'FROM INFORMATION_SCHEMA') !== false ||
strpos($querystring, 'FROM sys.') !== false;
}
}