Browse Source

Add ability to get logger instance from db driver.

The Crud plugin for e.g. has a feature of including query logs in
serialized responses for debugging purposes. This features breaks
without a way to access the custom logger it sets for the drivers.
ADmad 3 years ago
parent
commit
ae77a08cf9

+ 2 - 2
src/Database/Driver.php

@@ -540,9 +540,9 @@ abstract class Driver implements DriverInterface
     /**
      * @inheritDoc
      */
-    public function setLogger(LoggerInterface $logger): void
+    public function getLogger(): ?LoggerInterface
     {
-        $this->logger = $logger;
+        return $this->logger;
     }
 
     /**

+ 8 - 0
src/Database/DriverInterface.php

@@ -20,6 +20,7 @@ use Cake\Database\Schema\SchemaDialect;
 use Cake\Database\Schema\TableSchemaInterface;
 use Closure;
 use Psr\Log\LoggerAwareInterface;
+use Psr\Log\LoggerInterface;
 use Stringable;
 
 /**
@@ -333,4 +334,11 @@ interface DriverInterface extends LoggerAwareInterface
      * @return bool True if message was logged.
      */
     public function log(Stringable|string $message, array $context = []): bool;
+
+    /**
+     * Get the logger instance.
+     *
+     * @return \Psr\Log\LoggerInterface|null
+     */
+    public function getLogger(): ?LoggerInterface;
 }

+ 0 - 6
tests/test_app/TestApp/Database/Driver/StubDriver.php

@@ -16,7 +16,6 @@ declare(strict_types=1);
 namespace TestApp\Database\Driver;
 
 use Cake\Database\Driver;
-use Psr\Log\LoggerInterface;
 
 abstract class StubDriver extends Driver
 {
@@ -24,9 +23,4 @@ abstract class StubDriver extends Driver
     {
         $this->pdo = $this->createPdo('', []);
     }
-
-    public function getLogger(): ?LoggerInterface
-    {
-        return $this->logger;
-    }
 }