3 namespace Drupal\Core\Database;
6 * Database query logger.
8 * We log queries in a separate object rather than in the connection object
9 * because we want to be able to see all queries sent to a given database, not
10 * database target. If we logged the queries in each connection object we
11 * would not be able to track what queries went to which target.
13 * Every connection has one and only one logging object on it for all targets
19 * Cache of logged queries. This will only be used if the query logger is enabled.
21 * The structure for the logging array is as follows:
24 * $logging_key = array(
25 * array('query' => '', 'args' => array(), 'caller' => '', 'target' => '', 'time' => 0),
26 * array('query' => '', 'args' => array(), 'caller' => '', 'target' => '', 'time' => 0),
32 protected $queryLog = [];
35 * The connection key for which this object is logging.
39 protected $connectionKey = 'default';
45 * The database connection key for which to enable logging.
47 public function __construct($key = 'default') {
48 $this->connectionKey = $key;
52 * Begin logging queries to the specified connection and logging key.
54 * If the specified logging key is already running this method does nothing.
57 * The identification key for this log request. By specifying different
58 * logging keys we are able to start and stop multiple logging runs
59 * simultaneously without them colliding.
61 public function start($logging_key) {
62 if (empty($this->queryLog[$logging_key])) {
63 $this->clear($logging_key);
68 * Retrieve the query log for the specified logging key so far.
71 * The logging key to fetch.
73 * An indexed array of all query records for this logging key.
75 public function get($logging_key) {
76 return $this->queryLog[$logging_key];
80 * Empty the query log for the specified logging key.
82 * This method does not stop logging, it simply clears the log. To stop
83 * logging, use the end() method.
86 * The logging key to empty.
88 public function clear($logging_key) {
89 $this->queryLog[$logging_key] = [];
93 * Stop logging for the specified logging key.
96 * The logging key to stop.
98 public function end($logging_key) {
99 unset($this->queryLog[$logging_key]);
103 * Log a query to all active logging keys.
106 * The prepared statement object to log.
108 * The arguments passed to the statement object.
110 * The time in milliseconds the query took to execute.
112 public function log(StatementInterface $statement, $args, $time) {
113 foreach (array_keys($this->queryLog) as $key) {
114 $this->queryLog[$key][] = [
115 'query' => $statement->getQueryString(),
117 'target' => $statement->dbh->getTarget(),
118 'caller' => $this->findCaller(),
125 * Determine the routine that called this query.
127 * We define "the routine that called this query" as the first entry in
128 * the call stack that is not inside the includes/Drupal/Database directory,
129 * does not begin with db_ and does have a file (which excludes
130 * call_user_func_array(), anonymous functions and similar). That makes the
131 * climbing logic very simple, and handles the variable stack depth caused by
132 * the query builders.
134 * See the @link http://php.net/debug_backtrace debug_backtrace() @endlink
138 * This method returns a stack trace entry similar to that generated by
139 * debug_backtrace(). However, it flattens the trace entry and the trace
140 * entry before it so that we get the function and args of the function that
141 * called into the database system, not the function and args of the
142 * database call itself.
144 public function findCaller() {
145 $stack = debug_backtrace();
146 for ($i = 0, $stack_count = count($stack); $i < $stack_count; ++$i) {
147 // If the call was made from a function, 'class' will be empty. It's
148 // just easier to give it a default value than to try and integrate
149 // that into the if statement below.
150 if (empty($stack[$i]['class'])) {
151 $stack[$i]['class'] = '';
153 if (strpos($stack[$i]['class'], __NAMESPACE__) === FALSE && strpos($stack[$i + 1]['function'], 'db_') === FALSE && !empty($stack[$i]['file'])) {
154 $stack[$i] += ['file' => '?', 'line' => '?', 'args' => []];
156 'file' => $stack[$i]['file'],
157 'line' => $stack[$i]['line'],
158 'function' => $stack[$i + 1]['function'],
159 'class' => isset($stack[$i + 1]['class']) ? $stack[$i + 1]['class'] : NULL,
160 'type' => isset($stack[$i + 1]['type']) ? $stack[$i + 1]['type'] : NULL,
161 'args' => $stack[$i + 1]['args'],