annotate core/lib/Drupal/Core/Database/Log.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Core\Database;
Chris@0 4
Chris@0 5 /**
Chris@0 6 * Database query logger.
Chris@0 7 *
Chris@0 8 * We log queries in a separate object rather than in the connection object
Chris@0 9 * because we want to be able to see all queries sent to a given database, not
Chris@0 10 * database target. If we logged the queries in each connection object we
Chris@0 11 * would not be able to track what queries went to which target.
Chris@0 12 *
Chris@0 13 * Every connection has one and only one logging object on it for all targets
Chris@0 14 * and logging keys.
Chris@0 15 */
Chris@0 16 class Log {
Chris@0 17
Chris@0 18 /**
Chris@0 19 * Cache of logged queries. This will only be used if the query logger is enabled.
Chris@0 20 *
Chris@0 21 * The structure for the logging array is as follows:
Chris@0 22 *
Chris@0 23 * array(
Chris@0 24 * $logging_key = array(
Chris@0 25 * array('query' => '', 'args' => array(), 'caller' => '', 'target' => '', 'time' => 0),
Chris@0 26 * array('query' => '', 'args' => array(), 'caller' => '', 'target' => '', 'time' => 0),
Chris@0 27 * ),
Chris@0 28 * );
Chris@0 29 *
Chris@0 30 * @var array
Chris@0 31 */
Chris@0 32 protected $queryLog = [];
Chris@0 33
Chris@0 34 /**
Chris@0 35 * The connection key for which this object is logging.
Chris@0 36 *
Chris@0 37 * @var string
Chris@0 38 */
Chris@0 39 protected $connectionKey = 'default';
Chris@0 40
Chris@0 41 /**
Chris@0 42 * Constructor.
Chris@0 43 *
Chris@0 44 * @param $key
Chris@0 45 * The database connection key for which to enable logging.
Chris@0 46 */
Chris@0 47 public function __construct($key = 'default') {
Chris@0 48 $this->connectionKey = $key;
Chris@0 49 }
Chris@0 50
Chris@0 51 /**
Chris@0 52 * Begin logging queries to the specified connection and logging key.
Chris@0 53 *
Chris@0 54 * If the specified logging key is already running this method does nothing.
Chris@0 55 *
Chris@0 56 * @param $logging_key
Chris@0 57 * The identification key for this log request. By specifying different
Chris@0 58 * logging keys we are able to start and stop multiple logging runs
Chris@0 59 * simultaneously without them colliding.
Chris@0 60 */
Chris@0 61 public function start($logging_key) {
Chris@0 62 if (empty($this->queryLog[$logging_key])) {
Chris@0 63 $this->clear($logging_key);
Chris@0 64 }
Chris@0 65 }
Chris@0 66
Chris@0 67 /**
Chris@0 68 * Retrieve the query log for the specified logging key so far.
Chris@0 69 *
Chris@0 70 * @param $logging_key
Chris@0 71 * The logging key to fetch.
Chris@0 72 * @return
Chris@0 73 * An indexed array of all query records for this logging key.
Chris@0 74 */
Chris@0 75 public function get($logging_key) {
Chris@0 76 return $this->queryLog[$logging_key];
Chris@0 77 }
Chris@0 78
Chris@0 79 /**
Chris@0 80 * Empty the query log for the specified logging key.
Chris@0 81 *
Chris@0 82 * This method does not stop logging, it simply clears the log. To stop
Chris@0 83 * logging, use the end() method.
Chris@0 84 *
Chris@0 85 * @param $logging_key
Chris@0 86 * The logging key to empty.
Chris@0 87 */
Chris@0 88 public function clear($logging_key) {
Chris@0 89 $this->queryLog[$logging_key] = [];
Chris@0 90 }
Chris@0 91
Chris@0 92 /**
Chris@0 93 * Stop logging for the specified logging key.
Chris@0 94 *
Chris@0 95 * @param $logging_key
Chris@0 96 * The logging key to stop.
Chris@0 97 */
Chris@0 98 public function end($logging_key) {
Chris@0 99 unset($this->queryLog[$logging_key]);
Chris@0 100 }
Chris@0 101
Chris@0 102 /**
Chris@0 103 * Log a query to all active logging keys.
Chris@0 104 *
Chris@0 105 * @param $statement
Chris@0 106 * The prepared statement object to log.
Chris@0 107 * @param $args
Chris@0 108 * The arguments passed to the statement object.
Chris@0 109 * @param $time
Chris@0 110 * The time in milliseconds the query took to execute.
Chris@0 111 */
Chris@0 112 public function log(StatementInterface $statement, $args, $time) {
Chris@0 113 foreach (array_keys($this->queryLog) as $key) {
Chris@0 114 $this->queryLog[$key][] = [
Chris@0 115 'query' => $statement->getQueryString(),
Chris@0 116 'args' => $args,
Chris@0 117 'target' => $statement->dbh->getTarget(),
Chris@0 118 'caller' => $this->findCaller(),
Chris@0 119 'time' => $time,
Chris@0 120 ];
Chris@0 121 }
Chris@0 122 }
Chris@0 123
Chris@0 124 /**
Chris@0 125 * Determine the routine that called this query.
Chris@0 126 *
Chris@0 127 * We define "the routine that called this query" as the first entry in
Chris@0 128 * the call stack that is not inside the includes/Drupal/Database directory,
Chris@0 129 * does not begin with db_ and does have a file (which excludes
Chris@0 130 * call_user_func_array(), anonymous functions and similar). That makes the
Chris@0 131 * climbing logic very simple, and handles the variable stack depth caused by
Chris@0 132 * the query builders.
Chris@0 133 *
Chris@0 134 * See the @link http://php.net/debug_backtrace debug_backtrace() @endlink
Chris@0 135 * function.
Chris@0 136 *
Chris@0 137 * @return
Chris@0 138 * This method returns a stack trace entry similar to that generated by
Chris@0 139 * debug_backtrace(). However, it flattens the trace entry and the trace
Chris@0 140 * entry before it so that we get the function and args of the function that
Chris@0 141 * called into the database system, not the function and args of the
Chris@0 142 * database call itself.
Chris@0 143 */
Chris@0 144 public function findCaller() {
Chris@0 145 $stack = debug_backtrace();
Chris@0 146 for ($i = 0, $stack_count = count($stack); $i < $stack_count; ++$i) {
Chris@0 147 // If the call was made from a function, 'class' will be empty. It's
Chris@0 148 // just easier to give it a default value than to try and integrate
Chris@0 149 // that into the if statement below.
Chris@0 150 if (empty($stack[$i]['class'])) {
Chris@0 151 $stack[$i]['class'] = '';
Chris@0 152 }
Chris@0 153 if (strpos($stack[$i]['class'], __NAMESPACE__) === FALSE && strpos($stack[$i + 1]['function'], 'db_') === FALSE && !empty($stack[$i]['file'])) {
Chris@0 154 $stack[$i] += ['file' => '?', 'line' => '?', 'args' => []];
Chris@0 155 return [
Chris@0 156 'file' => $stack[$i]['file'],
Chris@0 157 'line' => $stack[$i]['line'],
Chris@0 158 'function' => $stack[$i + 1]['function'],
Chris@0 159 'class' => isset($stack[$i + 1]['class']) ? $stack[$i + 1]['class'] : NULL,
Chris@0 160 'type' => isset($stack[$i + 1]['type']) ? $stack[$i + 1]['type'] : NULL,
Chris@0 161 'args' => $stack[$i + 1]['args'],
Chris@0 162 ];
Chris@0 163 }
Chris@0 164 }
Chris@0 165 }
Chris@0 166
Chris@0 167 }