Skip to content

Commit

Permalink
Introduced a PSR-3 logger adapter
Browse files Browse the repository at this point in the history
Additionally, deprecated `DebugStack` and `EchoSQLLogger` as per (my own) proposal in #2911 (comment)
  • Loading branch information
morozov committed May 21, 2018
1 parent 1f91c2d commit 2900cef
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 0 deletions.
13 changes: 13 additions & 0 deletions UPGRADE.md
@@ -1,3 +1,16 @@
# 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).

The following implementations have been deprecated:

1. `Doctrine\DBAL\Logging\DebugStack`,
2. `Doctrine\DBAL\Logging\EchoSQLLogger`.

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
Expand Down
2 changes: 2 additions & 0 deletions lib/Doctrine/DBAL/Logging/DebugStack.php
Expand Up @@ -30,6 +30,8 @@
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
* @author Jonathan Wage <jonwage@gmail.com>
* @author Roman Borschel <roman@code-factory.org>
*
* @deprecated
*/
class DebugStack implements SQLLogger
{
Expand Down
2 changes: 2 additions & 0 deletions lib/Doctrine/DBAL/Logging/EchoSQLLogger.php
Expand Up @@ -31,6 +31,8 @@
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
* @author Jonathan Wage <jonwage@gmail.com>
* @author Roman Borschel <roman@code-factory.org>
*
* @deprecated
*/
class EchoSQLLogger implements SQLLogger
{
Expand Down
37 changes: 37 additions & 0 deletions lib/Doctrine/DBAL/Logging/PsrAdapter.php
@@ -0,0 +1,37 @@
<?php

namespace Doctrine\DBAL\Logging;

use Psr\Log\LoggerInterface;

/**
* Logs every query as a debug message
*/
final class PsrAdapter implements SQLLogger
{
/** @var LoggerInterface */
private $logger;

public function __construct(LoggerInterface $logger)
{
$this->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()
{
}
}
29 changes: 29 additions & 0 deletions tests/Doctrine/Tests/DBAL/Logging/PsrAdapterTest.php
@@ -0,0 +1,29 @@
<?php

namespace Doctrine\DBAL\Logging;

use Doctrine\DBAL\ParameterType;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use function class_exists;

class PsrAdapterTest extends TestCase
{
public function testLogging()
{
if (! class_exists(LoggerInterface::class)) {
$this->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]);
}
}

0 comments on commit 2900cef

Please sign in to comment.