1 | <?php |
||
210 | class TestLogger implements LoggerInterface |
||
211 | { |
||
212 | use LoggerTrait; |
||
213 | |||
214 | private $display; |
||
215 | |||
216 | private $countWrites; |
||
217 | private $countReads; |
||
218 | |||
219 | public function __construct() |
||
220 | { |
||
221 | $this->countWrites = 0; |
||
222 | $this->countReads = 0; |
||
223 | } |
||
224 | |||
225 | public function countWriteQueries(): int |
||
226 | { |
||
227 | return $this->countWrites; |
||
228 | } |
||
229 | |||
230 | public function countReadQueries(): int |
||
231 | { |
||
232 | return $this->countReads; |
||
233 | } |
||
234 | |||
235 | public function log($level, $message, array $context = []) |
||
236 | { |
||
237 | if (!empty($context['query'])) { |
||
238 | $sql = strtolower($context['query']); |
||
239 | if ( |
||
240 | strpos($sql, 'insert') === 0 |
||
241 | || strpos($sql, 'update') === 0 |
||
242 | || strpos($sql, 'delete') === 0 |
||
243 | ) { |
||
244 | $this->countWrites++; |
||
245 | } else { |
||
246 | if (!$this->isPostgresSystemQuery($sql)) { |
||
247 | $this->countReads++; |
||
248 | } |
||
249 | } |
||
250 | } |
||
251 | |||
252 | if (!$this->display) { |
||
253 | return; |
||
254 | } |
||
255 | |||
256 | if ($level == LogLevel::ERROR) { |
||
257 | echo " \n! \033[31m" . $message . "\033[0m"; |
||
258 | } elseif ($level == LogLevel::ALERT) { |
||
259 | echo " \n! \033[35m" . $message . "\033[0m"; |
||
260 | } elseif (strpos($message, 'SHOW') === 0) { |
||
261 | echo " \n> \033[34m" . $message . "\033[0m"; |
||
262 | } else { |
||
263 | if ($this->isPostgresSystemQuery($message)) { |
||
264 | echo " \n> \033[90m" . $message . "\033[0m"; |
||
265 | |||
266 | return; |
||
267 | } |
||
268 | |||
269 | if (strpos($message, 'SELECT') === 0) { |
||
270 | echo " \n> \033[32m" . $message . "\033[0m"; |
||
271 | } elseif (strpos($message, 'INSERT') === 0) { |
||
272 | echo " \n> \033[36m" . $message . "\033[0m"; |
||
273 | } else { |
||
274 | echo " \n> \033[33m" . $message . "\033[0m"; |
||
275 | } |
||
276 | } |
||
277 | } |
||
278 | |||
279 | public function display() |
||
280 | { |
||
281 | $this->display = true; |
||
282 | } |
||
283 | |||
284 | public function hide() |
||
285 | { |
||
286 | $this->display = false; |
||
287 | } |
||
288 | |||
289 | protected function isPostgresSystemQuery(string $query): bool |
||
290 | { |
||
291 | $query = strtolower($query); |
||
292 | if ( |
||
293 | strpos($query, 'tc.constraint_name') |
||
294 | || strpos($query, 'pg_indexes') |
||
295 | || strpos($query, 'tc.constraint_name') |
||
296 | || strpos($query, 'pg_constraint') |
||
297 | || strpos($query, 'information_schema') |
||
298 | || strpos($query, 'pg_class') |
||
299 | ) { |
||
300 | return true; |
||
301 | } |
||
302 | |||
303 | return false; |
||
304 | } |
||
305 | } |