From f922941c20f5a416708cc7a1b0f111e3d3f522be Mon Sep 17 00:00:00 2001 From: Sergei Morozov Date: Sun, 20 May 2018 19:29:10 -0700 Subject: [PATCH] Introduced a PSR-3 logger adapter Additionally, deprecated `EchoSQLLogger` as per proposal in https://github.com/doctrine/dbal/pull/2911#issuecomment-343755494 --- UPGRADE.md | 8 ++++ lib/Doctrine/DBAL/Logging/EchoSQLLogger.php | 2 + lib/Doctrine/DBAL/Logging/PsrAdapter.php | 37 +++++++++++++++++++ tests/Doctrine/Tests/DBAL/ConnectionTest.php | 1 + .../Tests/DBAL/Logging/PsrAdapterTest.php | 29 +++++++++++++++ 5 files changed, 77 insertions(+) create mode 100644 lib/Doctrine/DBAL/Logging/PsrAdapter.php create mode 100644 tests/Doctrine/Tests/DBAL/Logging/PsrAdapterTest.php diff --git a/UPGRADE.md b/UPGRADE.md index cc238ee34ac..ab100ebe4db 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -1,3 +1,11 @@ +# Upgrade to 2.8 + +## Deprecated logger implementations + +With the introduction of the [PSR-3](https://www.php-fig.org/psr/psr-3/) logger interface standard, we can focus on implementing DBAL-specific features and delegate to [other libraries](https://packagist.org/providers/psr/log-implementation). + +`Doctrine\DBAL\Logging\EchoSQLLogger` has been deprecated. Please use `Doctrine\DBAL\Logging\PsrAdapter` and a PSR-3 compatible implementation instead. + # Upgrade to 2.7 ## Doctrine\DBAL\Platforms\AbstractPlatform::DATE_INTERVAL_UNIT_* constants deprecated diff --git a/lib/Doctrine/DBAL/Logging/EchoSQLLogger.php b/lib/Doctrine/DBAL/Logging/EchoSQLLogger.php index a6c1ae5ea0b..5413158c5f3 100644 --- a/lib/Doctrine/DBAL/Logging/EchoSQLLogger.php +++ b/lib/Doctrine/DBAL/Logging/EchoSQLLogger.php @@ -31,6 +31,8 @@ * @author Guilherme Blanco * @author Jonathan Wage * @author Roman Borschel + * + * @deprecated */ class EchoSQLLogger implements SQLLogger { diff --git a/lib/Doctrine/DBAL/Logging/PsrAdapter.php b/lib/Doctrine/DBAL/Logging/PsrAdapter.php new file mode 100644 index 00000000000..bc609e10645 --- /dev/null +++ b/lib/Doctrine/DBAL/Logging/PsrAdapter.php @@ -0,0 +1,37 @@ +logger = $logger; + } + + /** + * {@inheritDoc} + */ + public function startQuery($sql, array $params = null, array $types = null) + { + $this->logger->debug($sql, [ + 'params' => $params, + 'types' => $types, + ]); + } + + /** + * {@inheritDoc} + */ + public function stopQuery() + { + } +} diff --git a/tests/Doctrine/Tests/DBAL/ConnectionTest.php b/tests/Doctrine/Tests/DBAL/ConnectionTest.php index 819dd71f487..a9abc2db64a 100644 --- a/tests/Doctrine/Tests/DBAL/ConnectionTest.php +++ b/tests/Doctrine/Tests/DBAL/ConnectionTest.php @@ -196,6 +196,7 @@ public function getQueryMethods() * Pretty dumb test, however we want to check that the EchoSQLLogger correctly implements the interface. * * @group DBAL-11 + * @group legacy */ public function testEchoSQLLogger() { diff --git a/tests/Doctrine/Tests/DBAL/Logging/PsrAdapterTest.php b/tests/Doctrine/Tests/DBAL/Logging/PsrAdapterTest.php new file mode 100644 index 00000000000..23bfcf37f1c --- /dev/null +++ b/tests/Doctrine/Tests/DBAL/Logging/PsrAdapterTest.php @@ -0,0 +1,29 @@ +markTestSkipped('PSR-3 LoggerInterface is unavailable'); + } + + $logger = $this->createMock(LoggerInterface::class); + $logger->expects($this->once()) + ->method('debug') + ->with('SELECT name FROM users WHERE id = ?', [ + 'params' => [1], + 'types' => [ParameterType::INTEGER], + ]); + + $adapter = new PsrAdapter($logger); + $adapter->startQuery('SELECT name FROM users WHERE id = ?', [1], [ParameterType::INTEGER]); + } +}