Chris@0: '', 'args' => array(), 'caller' => '', 'target' => '', 'time' => 0), Chris@0: * array('query' => '', 'args' => array(), 'caller' => '', 'target' => '', 'time' => 0), Chris@0: * ), Chris@0: * ); Chris@0: * Chris@0: * @var array Chris@0: */ Chris@0: protected $queryLog = []; Chris@0: Chris@0: /** Chris@0: * The connection key for which this object is logging. Chris@0: * Chris@0: * @var string Chris@0: */ Chris@0: protected $connectionKey = 'default'; Chris@0: Chris@0: /** Chris@0: * Constructor. Chris@0: * Chris@0: * @param $key Chris@0: * The database connection key for which to enable logging. Chris@0: */ Chris@0: public function __construct($key = 'default') { Chris@0: $this->connectionKey = $key; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Begin logging queries to the specified connection and logging key. Chris@0: * Chris@0: * If the specified logging key is already running this method does nothing. Chris@0: * Chris@0: * @param $logging_key Chris@0: * The identification key for this log request. By specifying different Chris@0: * logging keys we are able to start and stop multiple logging runs Chris@0: * simultaneously without them colliding. Chris@0: */ Chris@0: public function start($logging_key) { Chris@0: if (empty($this->queryLog[$logging_key])) { Chris@0: $this->clear($logging_key); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Retrieve the query log for the specified logging key so far. Chris@0: * Chris@0: * @param $logging_key Chris@0: * The logging key to fetch. Chris@0: * @return Chris@0: * An indexed array of all query records for this logging key. Chris@0: */ Chris@0: public function get($logging_key) { Chris@0: return $this->queryLog[$logging_key]; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Empty the query log for the specified logging key. Chris@0: * Chris@0: * This method does not stop logging, it simply clears the log. To stop Chris@0: * logging, use the end() method. Chris@0: * Chris@0: * @param $logging_key Chris@0: * The logging key to empty. Chris@0: */ Chris@0: public function clear($logging_key) { Chris@0: $this->queryLog[$logging_key] = []; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Stop logging for the specified logging key. Chris@0: * Chris@0: * @param $logging_key Chris@0: * The logging key to stop. Chris@0: */ Chris@0: public function end($logging_key) { Chris@0: unset($this->queryLog[$logging_key]); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Log a query to all active logging keys. Chris@0: * Chris@0: * @param $statement Chris@0: * The prepared statement object to log. Chris@0: * @param $args Chris@0: * The arguments passed to the statement object. Chris@0: * @param $time Chris@0: * The time in milliseconds the query took to execute. Chris@0: */ Chris@0: public function log(StatementInterface $statement, $args, $time) { Chris@0: foreach (array_keys($this->queryLog) as $key) { Chris@0: $this->queryLog[$key][] = [ Chris@0: 'query' => $statement->getQueryString(), Chris@0: 'args' => $args, Chris@0: 'target' => $statement->dbh->getTarget(), Chris@0: 'caller' => $this->findCaller(), Chris@0: 'time' => $time, Chris@0: ]; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Determine the routine that called this query. Chris@0: * Chris@0: * We define "the routine that called this query" as the first entry in Chris@0: * the call stack that is not inside the includes/Drupal/Database directory, Chris@0: * does not begin with db_ and does have a file (which excludes Chris@0: * call_user_func_array(), anonymous functions and similar). That makes the Chris@0: * climbing logic very simple, and handles the variable stack depth caused by Chris@0: * the query builders. Chris@0: * Chris@0: * See the @link http://php.net/debug_backtrace debug_backtrace() @endlink Chris@0: * function. Chris@0: * Chris@0: * @return Chris@0: * This method returns a stack trace entry similar to that generated by Chris@0: * debug_backtrace(). However, it flattens the trace entry and the trace Chris@0: * entry before it so that we get the function and args of the function that Chris@0: * called into the database system, not the function and args of the Chris@0: * database call itself. Chris@0: */ Chris@0: public function findCaller() { Chris@0: $stack = debug_backtrace(); Chris@0: for ($i = 0, $stack_count = count($stack); $i < $stack_count; ++$i) { Chris@0: // If the call was made from a function, 'class' will be empty. It's Chris@0: // just easier to give it a default value than to try and integrate Chris@0: // that into the if statement below. Chris@0: if (empty($stack[$i]['class'])) { Chris@0: $stack[$i]['class'] = ''; Chris@0: } Chris@0: if (strpos($stack[$i]['class'], __NAMESPACE__) === FALSE && strpos($stack[$i + 1]['function'], 'db_') === FALSE && !empty($stack[$i]['file'])) { Chris@0: $stack[$i] += ['file' => '?', 'line' => '?', 'args' => []]; Chris@0: return [ Chris@0: 'file' => $stack[$i]['file'], Chris@0: 'line' => $stack[$i]['line'], Chris@0: 'function' => $stack[$i + 1]['function'], Chris@0: 'class' => isset($stack[$i + 1]['class']) ? $stack[$i + 1]['class'] : NULL, Chris@0: 'type' => isset($stack[$i + 1]['type']) ? $stack[$i + 1]['type'] : NULL, Chris@0: 'args' => $stack[$i + 1]['args'], Chris@0: ]; Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: }