diff --git a/core/lib/Drupal/Core/Database/Log.php b/core/lib/Drupal/Core/Database/Log.php
index bf142c2cde..eac37d580b 100644
--- a/core/lib/Drupal/Core/Database/Log.php
+++ b/core/lib/Drupal/Core/Database/Log.php
@@ -134,6 +134,10 @@ public function log(StatementInterface $statement, $args, $time) {
    * See the @link http://php.net/debug_backtrace debug_backtrace() @endlink
    * function.
    *
+   * @param array $stack
+   *   (optional) The backtrace to find the caller in. It falls back to the 
+   *   current backtrace.
+   *
    * @return
    *   This method returns a stack trace entry similar to that generated by
    *   debug_backtrace(). However, it flattens the trace entry and the trace
@@ -141,8 +145,21 @@ public function log(StatementInterface $statement, $args, $time) {
    *   called into the database system, not the function and args of the
    *   database call itself.
    */
-  public function findCaller() {
-    $stack = debug_backtrace();
+  public function findCaller($stack = NULL) {
+    if ($stack === NULL) {
+      $stack = debug_backtrace();
+    }
+
+    // Drop all stack entries before and including methods from the database
+    // connection class.
+    $connection_class = get_class(Database::getConnection($this->connectionKey));
+    while ((!empty($stack[0]['class']) ? $stack[0]['class'] : '') !== $connection_class) {
+      array_shift($stack);
+    }
+    while ((!empty($stack[0]['class']) ? $stack[0]['class'] : '') === $connection_class) {
+      array_shift($stack);
+    }
+
     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
diff --git a/core/tests/Drupal/KernelTests/Core/Database/LogTest.php b/core/tests/Drupal/KernelTests/Core/Database/LogTest.php
new file mode 100644
index 0000000000..61b3a6d2a0
--- /dev/null
+++ b/core/tests/Drupal/KernelTests/Core/Database/LogTest.php
@@ -0,0 +1,124 @@
+<?php
+
+namespace Drupal\KernelTests\Core\Database;
+
+use Drupal\Core\Database\Log;
+use Drupal\KernelTests\KernelTestBase;
+
+/**
+ * @group Database
+ */
+class LogTest extends KernelTestBase {
+
+  /**
+   * @covers ::findCaller
+   */
+  public function testContribDriver() {
+    $log = new Log('default');
+
+    $stack = [];
+    $stack[] = [
+      'class' => 'Drupal\Core\Database\Log',
+      'function' => 'findCaller',
+      'file' => 'core/lib/Drupal/Core/Database/Log.php',
+      'line' => 1,
+      'type' => '->',
+      'args' => [],
+    ];
+    $stack[] = [
+      'class' => 'Drupal\Core\Database\Log',
+      'function' => 'log',
+      'file' => 'core/lib/Drupal/Core/Database/Log.php',
+      'line' => 0,
+      'type' => '->',
+      'args' => [],
+    ];
+    $stack[] = [
+      'class' => 'Drupal\Core\Database\Statement',
+      'function' => 'execute',
+      'file' => 'core/lib/Drupal/Core/Database/Statement.php',
+      'line' => 0,
+      'type' => '->',
+      'args' => [],
+    ];
+    $stack[] = [
+      'class' => 'Doctrine\DBAL\Statement',
+      'function' => 'execute',
+      'file' => 'vendor/doctrine/dbal/Statement.php',
+      'line' => 0,
+      'type' => '->',
+      'args' => [],
+    ];
+    $stack[] = [
+      'class' => 'Drupal\Driver\Database\dbal\Connection',
+      'function' => 'execute',
+      'file' => 'modules/dbal/Drupal/Driver/Database/dbal/Connection.php',
+      'line' => 0,
+      'type' => '->',
+      'args' => [],
+    ];
+    $stack[] = [
+      'function' => 'db_query',
+      'file' => 'core/includes/database.inc',
+      'line' => 56,
+      'type' => '::',
+      'args' => [],
+    ];
+    $stack[] = [
+      'class' => 'Drupal\drudbal\Tests\LoggingTest',
+      'function' => 'testEnableLogging',
+      'file' => 'modules/dbal/tests/LoggingTest.php',
+      'line' => 11,
+      'type' => '->',
+      'args' => [],
+    ];
+    $stack[] = [
+      'class' => 'Drupal\simpletest\TestBase',
+      'function' => 'run',
+      'file' => 'core/modules/simpletest/src/TestBase.php',
+      'line' => 861,
+      'type' => '->',
+      'args' => [],
+    ];
+    $stack[] = [
+      'function' => '_simpletest_batch_operation',
+      'file' => 'core/modules/simpletest/simpletest.module',
+      'line' => 398,
+      'type' => '::',
+      'args' => [],
+    ];
+    $stack[] = [
+      'function' => '_batch_process',
+      'file' => 'core/includes/batch.inc',
+      'line' => 209,
+      'type' => '::',
+      'args' => [],
+    ];
+    $stack[] = [
+      'function' => '_batch_do',
+      'file' => 'core/includes/batch.inc',
+      'line' => 93,
+      'type' => '::',
+      'args' => [],
+    ];
+    $stack[] = [
+      'function' => '_batch_page',
+      'file' => 'core/includes/batch.inc',
+      'line' => 34,
+      'type' => '::',
+      'args' => [],
+    ];
+
+    $result = $log->findCaller($stack);
+    $expected_result = [
+      'class' => 'Drupal\drudbal\Tests\LoggingTest',
+      'function' => 'testEnableLogging',
+      'file' => 'modules/dbal/tests/LoggingTest.php',
+      'line' => 11,
+      'type' => '->',
+      'args' => [],
+    ];
+    $this->assertEquals($expected_result, $result);
+  }
+
+}
