diff --git a/core/lib/Drupal/Core/Database/Log.php b/core/lib/Drupal/Core/Database/Log.php index 7a014cc..3f30665 100644 --- a/core/lib/Drupal/Core/Database/Log.php +++ b/core/lib/Drupal/Core/Database/Log.php @@ -125,9 +125,9 @@ public function log(StatementInterface $statement, $args, $time) { * Determine the routine that called this query. * * We define "the routine that called this query" as the first entry in - * the call stack that is coming after any method called from the database - * Connection class, is not inside the Drupal\Core\Database namespace, does - * not begin with db_ and does have a file (which excludes + * the call stack that is coming after any method called from the namespace + * of the database driver, is not inside the Drupal\Core\Database namespace, + * does not begin with db_ and does have a file (which excludes * call_user_func_array(), anonymous functions and similar). That makes the * climbing logic very simple, and handles the variable stack depth caused by * the query builders. @@ -144,24 +144,26 @@ public function log(StatementInterface $statement, $args, $time) { */ public function findCaller() { $stack = $this->getDebugBacktrace(); - - // Drop all stack entries before and including methods called from the - // database connection namespace. $connection_namespace = $this->getConnectionNamespace(); - while (count($stack) > 2 && strpos((!empty($stack[0]['class']) ? $stack[0]['class'] : ''), $connection_namespace) === FALSE) { - array_shift($stack); - } - while (count($stack) > 2 && strpos((!empty($stack[0]['class']) ? $stack[0]['class'] : ''), $connection_namespace) === 0) { - array_shift($stack); - } + $skipped_driver_methods = FALSE; for ($i = 0, $stack_count = count($stack); $i < $stack_count; ++$i) { // If the call was made from a function, 'class' will be empty. It's // just easier to give it a default value than to try and integrate - // that into the if statement below. + // that into the if statements below. if (empty($stack[$i]['class'])) { $stack[$i]['class'] = ''; } + // Skip all stack entries before and including methods called from the + // database driver namespace. In any case, ensure the last entry of the + // trace is returned. + $is_driver_method = (strpos($stack[$i]['class'], $connection_namespace) === 0); + if (($is_driver_method === TRUE || $skipped_driver_methods === FALSE) && $i < $stack_count - 2) { + if ($is_driver_method === TRUE) { + $skipped_driver_methods = TRUE; + } + continue; + } if (strpos($stack[$i]['class'], __NAMESPACE__) === FALSE && strpos($stack[$i + 1]['function'], 'db_') === FALSE && !empty($stack[$i]['file'])) { $stack[$i] += ['file' => '?', 'line' => '?', 'args' => []]; return [