'', 'args' => [], 'caller' => '', 'target' => '', 'time' => 0, 'start' => 0], * ['query' => '', 'args' => [], 'caller' => '', 'target' => '', 'time' => 0, 'start' => 0], * ], * ]; * @endcode */ protected $queryLog = []; /** * The connection key for which this object is logging. * * @var string */ protected $connectionKey = 'default'; /** * Constructor. * * @param string $key * The database connection key for which to enable logging. */ public function __construct($key = 'default') { $this->connectionKey = $key; } /** * Begin logging queries to the specified connection and logging key. * * If the specified logging key is already running this method does nothing. * * @param string $logging_key * The identification key for this log request. By specifying different * logging keys we are able to start and stop multiple logging runs * simultaneously without them colliding. */ public function start($logging_key) { if (empty($this->queryLog[$logging_key])) { $this->clear($logging_key); } } /** * Retrieve the query log for the specified logging key so far. * * @param string $logging_key * The logging key to fetch. * * @return array * An indexed array of all query records for this logging key. */ public function get($logging_key) { return $this->queryLog[$logging_key]; } /** * Empty the query log for the specified logging key. * * This method does not stop logging, it simply clears the log. To stop * logging, use the end() method. * * @param string $logging_key * The logging key to empty. */ public function clear($logging_key) { $this->queryLog[$logging_key] = []; } /** * Stop logging for the specified logging key. * * @param string $logging_key * The logging key to stop. */ public function end($logging_key) { unset($this->queryLog[$logging_key]); } /** * Log a query to all active logging keys, from a statement execution event. * * @param \Drupal\Core\Database\Event\StatementExecutionEndEvent $event * The statement execution event. */ public function logFromEvent(StatementExecutionEndEvent $event): void { foreach (array_keys($this->queryLog) as $key) { $this->queryLog[$key][] = [ 'query' => $event->queryString, 'args' => $event->args, 'target' => $event->target, 'caller' => $event->caller, 'time' => $event->getElapsedTime(), 'start' => $event->startTime, ]; } } }