-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4967 from morozov/issues/4941-logging-middleware
Introduce logging middleware
- Loading branch information
Showing
15 changed files
with
550 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.