diff --git a/core/lib/Drupal/Core/Database/Log.php b/core/lib/Drupal/Core/Database/Log.php
index bf142c2..32cf4e7 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/LoggingTest.php b/core/tests/Drupal/KernelTests/Core/Database/LoggingTest.php
index 0216d3e..225065a 100644
--- a/core/tests/Drupal/KernelTests/Core/Database/LoggingTest.php
+++ b/core/tests/Drupal/KernelTests/Core/Database/LoggingTest.php
@@ -2,11 +2,13 @@
 
 namespace Drupal\KernelTests\Core\Database;
 
+use Drupal\Core\Database\Log;
 use Drupal\Core\Database\Database;
 
 /**
  * Tests the query logging facility.
  *
+ * @coversDefaultClass \Drupal\Core\Database\Log
  * @group Database
  */
 class LoggingTest extends DatabaseTestBase {
@@ -135,4 +137,131 @@ public function testGetLoggingWrongKey() {
     $this->assertEqual($result, [], 'The function getLog with a wrong key returns an empty array.');
   }
 
+  /**
+   * Tests that a log called by a custom database driver return proper caller.
+   *
+   * @covers ::findCaller
+   */
+  public function testContribDriverLog() {
+    $log = new Log('default');
+
+    $stack = [
+      [
+        'file' => '/var/www/core/lib/Drupal/Core/Database/Log.php',
+        'line' => 118,
+        'function' => 'findCaller',
+        'class' => 'Drupal\\Core\\Database\\Log',
+        'type' => '->',
+        'args' => [],
+      ],
+      [
+        'file' => '/var/www/core/lib/Drupal/Core/Database/Statement.php',
+        'line' => 63,
+        'function' => 'log',
+        'class' => 'Drupal\\Core\\Database\\Log',
+        'type' => '->',
+        'args' => [],
+      ],
+      [
+        'file' => '/var/www/vendor/doctrine/dbal/lib/Doctrine/DBAL/Statement.php',
+        'line' => 168,
+        'function' => 'execute',
+        'class' => 'Drupal\\Core\\Database\\Statement',
+        'type' => '->',
+        'args' => [],
+      ],
+      [
+        'file' => '/var/www/modules/contrib/drudbal/drivers/lib/Drupal/Driver/Database/dbal/Connection.php',
+        'line' => 183,
+        'function' => 'execute',
+        'class' => 'Doctrine\\DBAL\\Statement',
+        'type' => '->',
+        'args' => [],
+      ],
+      [
+        'file' => '/var/www/core/includes/database.inc',
+        'line' => 61,
+        'function' => 'query',
+        'class' => 'Drupal\\Driver\\Database\\dbal\\Connection',
+        'object' => NULL,
+        'type' => '->',
+        'args' => [],
+      ],
+      [
+        'file' => '/var/www/tests/Drupal/KernelTests/Core/Database/LoggingTest.php',
+        'line' => 20,
+        'function' => 'db_query',
+        'args' => [],
+      ],
+      [
+        'class' => 'Drupal\\KernelTests\\Core\\Database\\LoggingTest',
+        'function' => 'testEnableLogging',
+        'type' => '->',
+        'args' => [],
+      ],
+      [
+        'file' => '/var/www/vendor/phpunit/phpunit/src/Framework/TestCase.php',
+        'line' => 908,
+        'function' => 'invokeArgs',
+        'class' => 'ReflectionMethod',
+        'object' => NULL,
+        'type' => '->',
+        'args' => [],
+      ],
+      [
+        'file' => '/var/www/vendor/phpunit/phpunit/src/Framework/TestCase.php',
+        'line' => 768,
+        'function' => 'runTest',
+        'class' => 'PHPUnit_Framework_TestCase',
+        'object' => NULL,
+        'type' => '->',
+        'args' => [],
+      ],
+      [
+        'file' => '/var/www/vendor/phpunit/phpunit/src/Framework/TestResult.php',
+        'line' => 612,
+        'function' => 'runBare',
+        'class' => 'PHPUnit_Framework_TestCase',
+        'object' => NULL,
+        'type' => '->',
+        'args' => [],
+      ],
+      [
+        'file' => '/var/www/vendor/phpunit/phpunit/src/Framework/TestCase.php',
+        'line' => 724,
+        'function' => 'run',
+        'class' => 'PHPUnit_Framework_TestResult',
+        'object' => NULL,
+        'type' => '->',
+        'args' => [],
+      ],
+      [
+        'file' => '-',
+        'line' => 52,
+        'function' => 'run',
+        'class' => 'PHPUnit_Framework_TestCase',
+        'object' => NULL,
+        'type' => '->',
+        'args' => [],
+      ],
+      [
+        'file' => '-',
+        'line' => 1574,
+        'function' => '__phpunit_run_isolated_test',
+        'args' => [],
+      ],
+    ];
+
+    $result = $log->findCaller($stack);
+    $expected_result = [
+      'class' => 'Drupal\\KernelTests\\Core\\Database\\LoggingTest',
+      'function' => 'testEnableLogging',
+      'file' => '/var/www/tests/Drupal/KernelTests/Core/Database/LoggingTest.php',
+      'line' => 20,
+      'type' => '->',
+      'args' => [],
+    ];
+    $this->assertEquals($expected_result, $result);
+  }
+
 }
