Skip to content

Commit

Permalink
Merge pull request #4967 from morozov/issues/4941-logging-middleware
Browse files Browse the repository at this point in the history
Introduce logging middleware
  • Loading branch information
morozov authored Nov 21, 2021
2 parents 263ddfa + 8fcd454 commit 5b456d8
Show file tree
Hide file tree
Showing 15 changed files with 550 additions and 49 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ jobs:
with:
fetch-depth: 2

- name: "Temporarily remove support for PSR Log 3 on PHP 8.1"
run: 'sed -i "s/\"psr\/log\": \"^1|^2|^3\"/\"psr\/log\": \"^1|^2\"/" composer.json'
if: "${{ matrix.php-version=='8.1' }}"

- name: "Install PHP"
uses: "shivammathur/setup-php@v2"
with:
Expand Down Expand Up @@ -82,6 +86,10 @@ jobs:
with:
fetch-depth: 2

- name: "Temporarily remove support for PSR Log 3 on PHP 8.1"
run: 'sed -i "s/\"psr\/log\": \"^1|^2|^3\"/\"psr\/log\": \"^1|^2\"/" composer.json'
if: "${{ matrix.php-version=='8.1' }}"

- name: "Install PHP"
uses: "shivammathur/setup-php@v2"
with:
Expand Down Expand Up @@ -125,6 +133,10 @@ jobs:
with:
fetch-depth: 2

- name: "Temporarily remove support for PSR Log 3 on PHP 8.1"
run: 'sed -i "s/\"psr\/log\": \"^1|^2|^3\"/\"psr\/log\": \"^1|^2\"/" composer.json'
if: "${{ matrix.php-version=='8.1' }}"

- name: "Install PHP"
uses: "shivammathur/setup-php@v2"
with:
Expand Down Expand Up @@ -180,6 +192,10 @@ jobs:
with:
fetch-depth: 2

- name: "Temporarily remove support for PSR Log 3 on PHP 8.1"
run: 'sed -i "s/\"psr\/log\": \"^1|^2|^3\"/\"psr\/log\": \"^1|^2\"/" composer.json'
if: "${{ matrix.php-version=='8.1' }}"

- name: "Install PHP"
uses: "shivammathur/setup-php@v2"
with:
Expand Down Expand Up @@ -242,6 +258,10 @@ jobs:
with:
fetch-depth: 2

- name: "Temporarily remove support for PSR Log 3 on PHP 8.1"
run: 'sed -i "s/\"psr\/log\": \"^1|^2|^3\"/\"psr\/log\": \"^1|^2\"/" composer.json'
if: "${{ matrix.php-version=='8.1' }}"

- name: "Install PHP"
uses: "shivammathur/setup-php@v2"
with:
Expand Down Expand Up @@ -330,6 +350,10 @@ jobs:
with:
fetch-depth: 2

- name: "Temporarily remove support for PSR Log 3 on PHP 8.1"
run: 'sed -i "s/\"psr\/log\": \"^1|^2|^3\"/\"psr\/log\": \"^1|^2\"/" composer.json'
if: "${{ matrix.php-version=='8.1' }}"

- name: "Install PHP"
uses: "shivammathur/setup-php@v2"
with:
Expand Down Expand Up @@ -398,6 +422,10 @@ jobs:
with:
fetch-depth: 2

- name: "Temporarily remove support for PSR Log 3 on PHP 8.1"
run: 'sed -i "s/\"psr\/log\": \"^1|^2|^3\"/\"psr\/log\": \"^1|^2\"/" composer.json'
if: "${{ matrix.php-version=='8.1' }}"

- name: "Install PHP"
uses: "shivammathur/setup-php@v2"
with:
Expand Down Expand Up @@ -455,6 +483,10 @@ jobs:
with:
fetch-depth: 2

- name: "Temporarily remove support for PSR Log 3 on PHP 8.1"
run: 'sed -i "s/\"psr\/log\": \"^1|^2|^3\"/\"psr\/log\": \"^1|^2\"/" composer.json'
if: "${{ matrix.php-version=='8.1' }}"

- name: "Install PHP"
uses: "shivammathur/setup-php@v2"
with:
Expand Down
5 changes: 5 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ awareness about deprecated code.

# Upgrade to 3.2

## Deprecated `SQLLogger` and its implementations.

The `SQLLogger` and its implementations `DebugStack` and `LoggerChain` have been deprecated.
For logging purposes, use `Doctrine\DBAL\Logging\Middleware` instead. No replacement for `DebugStack` is provided.

## Deprecated `SqliteSchemaManager::createDatabase()` and `dropDatabase()` methods.

The `SqliteSchemaManager::createDatabase()` and `dropDatabase()` methods have been deprecated. The SQLite engine
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
"doctrine/cache": "^1.11|^2.0",
"doctrine/deprecations": "^0.5.3",
"doctrine/event-manager": "^1.0",
"psr/cache": "^1|^2|^3"
"psr/cache": "^1|^2|^3",
"psr/log": "^1|^2|^3"
},
"require-dev": {
"doctrine/coding-standard": "9.0.0",
Expand Down
12 changes: 12 additions & 0 deletions psalm.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@
TODO: remove in 4.0.0
-->
<referencedClass name="Doctrine\DBAL\Schema\Constraint"/>
<!--
TODO: remove in 4.0.0
See https://github.com/doctrine/dbal/pull/4967
-->
<referencedClass name="Doctrine\DBAL\Logging\DebugStack"/>
<referencedClass name="Doctrine\DBAL\Logging\SQLLogger"/>
<referencedClass name="Doctrine\DBAL\Logging\LoggerChain"/>
</errorLevel>
</DeprecatedClass>
<DeprecatedInterface>
Expand All @@ -80,6 +87,11 @@
TODO: remove in 4.0.0
-->
<referencedClass name="Doctrine\DBAL\Schema\Constraint"/>
<!--
TODO: remove in 4.0.0
See https://github.com/doctrine/dbal/pull/4967
-->
<referencedClass name="Doctrine\DBAL\Logging\SQLLogger"/>
</errorLevel>
</DeprecatedInterface>
<DeprecatedMethod>
Expand Down
126 changes: 126 additions & 0 deletions src/Logging/Connection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php

declare(strict_types=1);

namespace Doctrine\DBAL\Logging;

use Doctrine\DBAL\Driver\Connection as ConnectionInterface;
use Doctrine\DBAL\Driver\Result;
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
use Doctrine\DBAL\Driver\Statement as DriverStatement;
use Doctrine\DBAL\ParameterType;
use Doctrine\Deprecations\Deprecation;
use LogicException;
use Psr\Log\LoggerInterface;

final class Connection implements ServerInfoAwareConnection
{
/** @var ConnectionInterface */
private $connection;

/** @var LoggerInterface */
private $logger;

/**
* @internal This connection can be only instantiated by its driver.
*/
public function __construct(ConnectionInterface $connection, LoggerInterface $logger)
{
$this->connection = $connection;
$this->logger = $logger;
}

public function __destruct()
{
$this->logger->info('Disconnecting');
}

public function prepare(string $sql): DriverStatement
{
return new Statement(
$this->connection->prepare($sql),
$this->logger,
$sql
);
}

public function query(string $sql): Result
{
$this->logger->debug('Executing query: {sql}', ['sql' => $sql]);

return $this->connection->query($sql);
}

/**
* {@inheritDoc}
*/
public function quote($value, $type = ParameterType::STRING)
{
return $this->connection->quote($value, $type);
}

public function exec(string $sql): int
{
$this->logger->debug('Executing statement: {sql}', ['sql' => $sql]);

return $this->connection->exec($sql);
}

/**
* {@inheritDoc}
*/
public function lastInsertId($name = null)
{
if ($name !== null) {
Deprecation::triggerIfCalledFromOutside(
'doctrine/dbal',
'https://github.com/doctrine/dbal/issues/4687',
'The usage of Connection::lastInsertId() with a sequence name is deprecated.'
);
}

return $this->connection->lastInsertId($name);
}

/**
* {@inheritDoc}
*/
public function beginTransaction()
{
$this->logger->debug('Beginning transaction');

return $this->connection->beginTransaction();
}

/**
* {@inheritDoc}
*/
public function commit()
{
$this->logger->debug('Committing transaction');

return $this->connection->commit();
}

/**
* {@inheritDoc}
*/
public function rollBack()
{
$this->logger->debug('Rolling back transaction');

return $this->connection->rollBack();
}

/**
* {@inheritDoc}
*/
public function getServerVersion()
{
if (! $this->connection instanceof ServerInfoAwareConnection) {
throw new LogicException('The underlying connection is not a ServerInfoAwareConnection');
}

return $this->connection->getServerVersion();
}
}
13 changes: 13 additions & 0 deletions src/Logging/DebugStack.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

namespace Doctrine\DBAL\Logging;

use Doctrine\Deprecations\Deprecation;

use function microtime;

/**
* Includes executed SQLs in a Debug Stack.
*
* @deprecated
*/
class DebugStack implements SQLLogger
{
Expand All @@ -29,6 +33,15 @@ class DebugStack implements SQLLogger
/** @var int */
public $currentQuery = 0;

public function __construct()
{
Deprecation::trigger(
'doctrine/dbal',
'https://github.com/doctrine/dbal/pull/4967',
'DebugStack is deprecated.'
);
}

/**
* {@inheritdoc}
*/
Expand Down
90 changes: 90 additions & 0 deletions src/Logging/Driver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

declare(strict_types=1);

namespace Doctrine\DBAL\Logging;

use Doctrine\DBAL\Connection as DBALConnection;
use Doctrine\DBAL\Driver as DriverInterface;
use Doctrine\DBAL\Driver\API\ExceptionConverter;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\VersionAwarePlatformDriver;
use Psr\Log\LoggerInterface;

final class Driver implements VersionAwarePlatformDriver
{
/** @var DriverInterface */
private $driver;

/** @var LoggerInterface */
private $logger;

/**
* @internal This driver can be only instantiated by its middleware.
*/
public function __construct(DriverInterface $driver, LoggerInterface $logger)
{
$this->driver = $driver;
$this->logger = $logger;
}

/**
* {@inheritDoc}
*/
public function connect(array $params)
{
$this->logger->info('Connecting with parameters {params}', ['params' => $this->maskPassword($params)]);

return new Connection(
$this->driver->connect($params),
$this->logger
);
}

/**
* {@inheritDoc}
*/
public function getDatabasePlatform()
{
return $this->driver->getDatabasePlatform();
}

/**
* {@inheritDoc}
*/
public function getSchemaManager(DBALConnection $conn, AbstractPlatform $platform)
{
return $this->driver->getSchemaManager($conn, $platform);
}

public function getExceptionConverter(): ExceptionConverter
{
return $this->driver->getExceptionConverter();
}

/**
* {@inheritDoc}
*/
public function createDatabasePlatformForVersion($version)
{
if ($this->driver instanceof VersionAwarePlatformDriver) {
return $this->driver->createDatabasePlatformForVersion($version);
}

return $this->driver->getDatabasePlatform();
}

/**
* @param array<string,mixed> $params Connection parameters
*
* @return array<string,mixed>
*/
private function maskPassword(array $params): array
{
if (isset($params['password'])) {
$params['password'] = '<redacted>';
}

return $params;
}
}
Loading

0 comments on commit 5b456d8

Please sign in to comment.