From 49a5a0797732194a5be316a039fc2ec2ac3e0329 Mon Sep 17 00:00:00 2001 From: darkdef Date: Wed, 17 Jan 2024 20:47:42 +0300 Subject: [PATCH] update --- docs/en/connection/logger.md | 2 +- src/Driver/Pdo/AbstractPdoCommand.php | 2 +- src/Driver/Pdo/AbstractPdoConnection.php | 8 ++++---- src/Driver/Pdo/AbstractPdoTransaction.php | 20 ++++++++++---------- src/Driver/Pdo/LogType.php | 8 +++----- tests/Common/CommonPdoCommandTest.php | 2 +- 6 files changed, 20 insertions(+), 22 deletions(-) diff --git a/docs/en/connection/logger.md b/docs/en/connection/logger.md index f0235f858..d27a496c6 100644 --- a/docs/en/connection/logger.md +++ b/docs/en/connection/logger.md @@ -80,7 +80,7 @@ class MyLogger extends ParentLoggerClass implements LoggerInterface { public function log($level, string|\Stringable $message, array $context = []): void { - if ($context[LogType::KEY] === LogType::TYPE_QUERY) { + if ($context['type'] === LogType::QUERY) { ... your logic here } } diff --git a/src/Driver/Pdo/AbstractPdoCommand.php b/src/Driver/Pdo/AbstractPdoCommand.php index 38bebdd6e..edd2adf81 100644 --- a/src/Driver/Pdo/AbstractPdoCommand.php +++ b/src/Driver/Pdo/AbstractPdoCommand.php @@ -259,7 +259,7 @@ protected function queryInternal(int $queryMode): mixed { $logCategory = self::class . '::' . $this->getQueryMode($queryMode); - $this->logger?->log(LogLevel::INFO, $rawSql = $this->getRawSql(), [$logCategory, LogType::KEY => LogType::TYPE_QUERY]); + $this->logger?->log(LogLevel::INFO, $rawSql = $this->getRawSql(), [$logCategory, 'type' => LogType::QUERY]); $queryContext = new CommandContext(__METHOD__, $logCategory, $this->getSql(), $this->getParams()); diff --git a/src/Driver/Pdo/AbstractPdoConnection.php b/src/Driver/Pdo/AbstractPdoConnection.php index ab39c0c0e..477c4b5dc 100644 --- a/src/Driver/Pdo/AbstractPdoConnection.php +++ b/src/Driver/Pdo/AbstractPdoConnection.php @@ -101,13 +101,13 @@ public function open(): void $connectionContext = new ConnectionContext(__METHOD__); try { - $this->logger?->log(LogLevel::INFO, $token, [LogType::KEY => LogType::TYPE_CONNECTION]); + $this->logger?->log(LogLevel::INFO, $token, ['type' => LogType::CONNECTION]); $this->profiler?->begin($token, $connectionContext); $this->initConnection(); $this->profiler?->end($token, $connectionContext); } catch (PDOException $e) { $this->profiler?->end($token, $connectionContext->setException($e)); - $this->logger?->log(LogLevel::ERROR, $token, [LogType::KEY => LogType::TYPE_CONNECTION]); + $this->logger?->log(LogLevel::ERROR, $token, ['type' => LogType::CONNECTION]); throw new Exception($e->getMessage(), (array) $e->errorInfo, $e); } @@ -119,7 +119,7 @@ public function close(): void $this->logger?->log( LogLevel::DEBUG, 'Closing DB connection: ' . $this->driver->getDsn() . ' ' . __METHOD__, - [LogType::KEY => LogType::TYPE_CONNECTION], + ['type' => LogType::CONNECTION], ); $this->pdo = null; @@ -226,7 +226,7 @@ protected function rollbackTransactionOnLevel(TransactionInterface $transaction, try { $transaction->rollBack(); } catch (Throwable $e) { - $this->logger?->log(LogLevel::ERROR, (string) $e, [__METHOD__, LogType::KEY => LogType::TYPE_TRANSACTION]); + $this->logger?->log(LogLevel::ERROR, (string) $e, [__METHOD__, 'type' => LogType::TRANSACTION]); /** hide this exception to be able to continue throwing original exception outside */ } } diff --git a/src/Driver/Pdo/AbstractPdoTransaction.php b/src/Driver/Pdo/AbstractPdoTransaction.php index a26e1a11f..7dff9fac8 100644 --- a/src/Driver/Pdo/AbstractPdoTransaction.php +++ b/src/Driver/Pdo/AbstractPdoTransaction.php @@ -61,7 +61,7 @@ public function begin(string $isolationLevel = null): void LogLevel::DEBUG, 'Begin transaction' . ($isolationLevel ? ' with isolation level ' . $isolationLevel : '') . ' ' . __METHOD__, - [LogType::KEY => LogType::TYPE_TRANSACTION] + ['type' => LogType::TRANSACTION] ); $this->db->getPDO()?->beginTransaction(); @@ -74,7 +74,7 @@ public function begin(string $isolationLevel = null): void $this->logger?->log( LogLevel::DEBUG, 'Set savepoint ' . $this->level . ' ' . __METHOD__, - [LogType::KEY => LogType::TYPE_TRANSACTION] + ['type' => LogType::TRANSACTION] ); $this->createSavepoint('LEVEL' . $this->level); @@ -82,7 +82,7 @@ public function begin(string $isolationLevel = null): void $this->logger?->log( LogLevel::DEBUG, 'Transaction not started: nested transaction not supported ' . __METHOD__, - [LogType::KEY => LogType::TYPE_TRANSACTION] + ['type' => LogType::TRANSACTION] ); throw new NotSupportedException('Transaction not started: nested transaction not supported.'); @@ -103,7 +103,7 @@ public function commit(): void $this->logger?->log( LogLevel::DEBUG, 'Commit transaction ' . __METHOD__, - [LogType::KEY => LogType::TYPE_TRANSACTION] + ['type' => LogType::TRANSACTION] ); $this->db->getPDO()?->commit(); @@ -114,14 +114,14 @@ public function commit(): void $this->logger?->log( LogLevel::DEBUG, 'Release savepoint ' . $this->level . ' ' . __METHOD__, - [LogType::KEY => LogType::TYPE_TRANSACTION] + ['type' => LogType::TRANSACTION] ); $this->releaseSavepoint('LEVEL' . $this->level); } else { $this->logger?->log( LogLevel::INFO, 'Transaction not committed: nested transaction not supported ' . __METHOD__, - [LogType::KEY => LogType::TYPE_TRANSACTION] + ['type' => LogType::TRANSACTION] ); } } @@ -153,7 +153,7 @@ public function rollBack(): void $this->logger?->log( LogLevel::INFO, 'Roll back transaction ' . __METHOD__, - [LogType::KEY => LogType::TYPE_TRANSACTION] + ['type' => LogType::TRANSACTION] ); $this->db->getPDO()?->rollBack(); @@ -164,14 +164,14 @@ public function rollBack(): void $this->logger?->log( LogLevel::DEBUG, 'Roll back to savepoint ' . $this->level . ' ' . __METHOD__, - [LogType::KEY => LogType::TYPE_TRANSACTION] + ['type' => LogType::TRANSACTION] ); $this->rollBackSavepoint('LEVEL' . $this->level); } else { $this->logger?->log( LogLevel::INFO, 'Transaction not rolled back: nested transaction not supported ' . __METHOD__, - [LogType::KEY => LogType::TYPE_TRANSACTION] + ['type' => LogType::TRANSACTION] ); } } @@ -185,7 +185,7 @@ public function setIsolationLevel(string $level): void $this->logger?->log( LogLevel::DEBUG, 'Setting transaction isolation level to ' . $this->level . ' ' . __METHOD__, - [LogType::KEY => LogType::TYPE_TRANSACTION] + ['type' => LogType::TRANSACTION] ); $this->setTransactionIsolationLevel($level); } diff --git a/src/Driver/Pdo/LogType.php b/src/Driver/Pdo/LogType.php index 9504d195c..0f27b7c28 100644 --- a/src/Driver/Pdo/LogType.php +++ b/src/Driver/Pdo/LogType.php @@ -6,9 +6,7 @@ final class LogType { - public const KEY = 'log-type'; - - public const TYPE_CONNECTION = 'connection'; - public const TYPE_QUERY = 'query'; - public const TYPE_TRANSACTION = 'transaction'; + public const CONNECTION = 'connection'; + public const QUERY = 'query'; + public const TRANSACTION = 'transaction'; } diff --git a/tests/Common/CommonPdoCommandTest.php b/tests/Common/CommonPdoCommandTest.php index 4584b7e00..1b00e39b2 100644 --- a/tests/Common/CommonPdoCommandTest.php +++ b/tests/Common/CommonPdoCommandTest.php @@ -239,7 +239,7 @@ protected function createQueryLogger(string $sql, array $params = []): LoggerInt ->with( LogLevel::INFO, $sql, - $params + ['log-type' => 'query'] + $params + ['type' => 'query'] ); return $logger; }