‪TYPO3CMS  ‪main
DoctrineSqlLogger.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Psr\Log\LoggerAwareInterface;
21 use Psr\Log\LoggerAwareTrait;
23 
29 class ‪DoctrineSqlLogger implements LoggerAwareInterface
30 {
31  use LoggerAwareTrait;
32 
34  protected array ‪$queries = [];
36  protected bool ‪$enabled = false;
37  protected float ‪$start;
38  protected int ‪$currentQuery = 0;
39 
40  public function ‪enable(): void
41  {
42  $this->enabled = true;
43  }
44 
45  public function ‪startQuery($sql, array $params = null, array $types = null): void
46  {
47  if ($this->enabled && ‪MemoryUtility::isMemoryConsumptionTooHigh()) {
48  $this->enabled = false;
49  $this->logger->warning('SQL Logging consumed too much memory, aborted. Not all queries have been recorded.');
50  }
51  if ($this->enabled) {
52  $visibleBacktraceLength = 4;
53  $removeFromBacktraceLength = 4;
54 
55  $this->start = microtime(true);
56  $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, $visibleBacktraceLength + $removeFromBacktraceLength);
57  // remove internal doctrine and logging methods from the visible backtrace
58  array_splice($backtrace, 0, $removeFromBacktraceLength);
59 
60  $this->queries[++‪$this->currentQuery] = [
61  'sql' => $sql,
62  'params' => $params ?? [],
63  'types' => $types ?? [],
64  'executionMS' => 0,
65  'backtrace' => $backtrace,
66  ];
67  }
68  }
69 
70  public function ‪stopQuery(): void
71  {
72  if ($this->enabled) {
73  $this->queries[‪$this->currentQuery]['executionMS'] = microtime(true) - ‪$this->start;
74  }
75  }
76 
77  public function ‪getQueries(): array
78  {
79  return ‪$this->queries;
80  }
81 }
‪TYPO3\CMS\Adminpanel\Log\DoctrineSqlLogger\startQuery
‪startQuery($sql, array $params=null, array $types=null)
Definition: DoctrineSqlLogger.php:45
‪TYPO3\CMS\Adminpanel\Log\DoctrineSqlLogger\$currentQuery
‪int $currentQuery
Definition: DoctrineSqlLogger.php:38
‪TYPO3\CMS\Adminpanel\Log\DoctrineSqlLogger\$start
‪float $start
Definition: DoctrineSqlLogger.php:37
‪TYPO3\CMS\Adminpanel\Log\DoctrineSqlLogger\getQueries
‪getQueries()
Definition: DoctrineSqlLogger.php:77
‪TYPO3\CMS\Adminpanel\Log
Definition: DoctrineSqlLogger.php:18
‪TYPO3\CMS\Adminpanel\Log\DoctrineSqlLogger
Definition: DoctrineSqlLogger.php:30
‪TYPO3\CMS\Adminpanel\Log\DoctrineSqlLogger\stopQuery
‪stopQuery()
Definition: DoctrineSqlLogger.php:70
‪TYPO3\CMS\Adminpanel\Log\DoctrineSqlLogger\$queries
‪array $queries
Definition: DoctrineSqlLogger.php:34
‪TYPO3\CMS\Adminpanel\Log\DoctrineSqlLogger\$enabled
‪bool $enabled
Definition: DoctrineSqlLogger.php:36
‪TYPO3\CMS\Adminpanel\Utility\MemoryUtility\isMemoryConsumptionTooHigh
‪static isMemoryConsumptionTooHigh()
Definition: MemoryUtility.php:37
‪TYPO3\CMS\Adminpanel\Utility\MemoryUtility
Definition: MemoryUtility.php:28
‪TYPO3\CMS\Adminpanel\Log\DoctrineSqlLogger\enable
‪enable()
Definition: DoctrineSqlLogger.php:40