From 0857a35d6ec0c4c024951a1a1a951e70d0f30cee Mon Sep 17 00:00:00 2001 From: darkdef Date: Wed, 3 Jan 2024 11:40:24 +0300 Subject: [PATCH 01/12] Logger context --- docs/en/connection/logger.md | 26 +++++++++++++ src/Driver/Pdo/AbstractPdoCommand.php | 14 ++----- src/Driver/Pdo/AbstractPdoConnection.php | 7 ++-- src/Driver/Pdo/AbstractPdoTransaction.php | 45 ++++++++++++++++++----- src/Driver/Pdo/LogTypes.php | 14 +++++++ 5 files changed, 82 insertions(+), 24 deletions(-) create mode 100644 src/Driver/Pdo/LogTypes.php diff --git a/docs/en/connection/logger.md b/docs/en/connection/logger.md index 499d88dbd..7ae3aa8ae 100644 --- a/docs/en/connection/logger.md +++ b/docs/en/connection/logger.md @@ -61,3 +61,29 @@ return [ ``` For other DBMS refer to ["Create connecton"](/docs/en/README.md#create-connection) section. + +## Advanced usage of Logger + +If you needed re-define logger messages or increase(decrease) level of log: +1. Create custom logger class +2. Use context for detect type of message in method "log" + +```php +logger?->log(LogLevel::INFO, $rawSql, [$category]); - } - protected function queryInternal(int $queryMode): mixed { $logCategory = self::class . '::' . $this->getQueryMode($queryMode); + $rawSql = $this->getRawSql(); if ($this->logger !== null) { - $rawSql = $this->getRawSql(); - $this->logQuery($rawSql, $logCategory); + $this->logger?->log(LogLevel::INFO, $rawSql, [$logCategory, LogTypes::KEY => LogTypes::TYPE_QUERY]); } $queryContext = new CommandContext(__METHOD__, $logCategory, $this->getSql(), $this->getParams()); @@ -279,7 +271,7 @@ protected function queryInternal(int $queryMode): mixed * @psalm-suppress RedundantConditionGivenDocblockType * @psalm-suppress DocblockTypeContradiction */ - $this->profiler?->begin($rawSql ??= $this->getRawSql(), $queryContext); + $this->profiler?->begin($rawSql, $queryContext); try { /** @psalm-var mixed $result */ $result = parent::queryInternal($queryMode); diff --git a/src/Driver/Pdo/AbstractPdoConnection.php b/src/Driver/Pdo/AbstractPdoConnection.php index abcaf7198..6620874c5 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); + $this->logger?->log(LogLevel::INFO, $token, [LogTypes::KEY => LogTypes::TYPE_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); + $this->logger?->log(LogLevel::ERROR, $token, [LogTypes::KEY => LogTypes::TYPE_CONNECTION]); throw new Exception($e->getMessage(), (array) $e->errorInfo, $e); } @@ -119,6 +119,7 @@ public function close(): void $this->logger?->log( LogLevel::DEBUG, 'Closing DB connection: ' . $this->driver->getDsn() . ' ' . __METHOD__, + [LogTypes::KEY => LogTypes::TYPE_CONNECTION], ); $this->pdo = null; @@ -225,7 +226,7 @@ protected function rollbackTransactionOnLevel(TransactionInterface $transaction, try { $transaction->rollBack(); } catch (Throwable $e) { - $this->logger?->log(LogLevel::ERROR, (string) $e, [__METHOD__]); + $this->logger?->log(LogLevel::ERROR, (string) $e, [__METHOD__, LogTypes::KEY => LogTypes::TYPE_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 647b0941b..324751cf9 100644 --- a/src/Driver/Pdo/AbstractPdoTransaction.php +++ b/src/Driver/Pdo/AbstractPdoTransaction.php @@ -60,7 +60,8 @@ public function begin(string $isolationLevel = null): void $this->logger?->log( LogLevel::DEBUG, 'Begin transaction' . ($isolationLevel ? ' with isolation level ' . $isolationLevel : '') - . ' ' . __METHOD__ + . ' ' . __METHOD__, + [LogTypes::KEY => LogTypes::TYPE_TRANSACTION] ); $this->db->getPDO()?->beginTransaction(); @@ -70,13 +71,18 @@ public function begin(string $isolationLevel = null): void } if ($this->db->isSavepointEnabled()) { - $this->logger?->log(LogLevel::DEBUG, 'Set savepoint ' . $this->level . ' ' . __METHOD__); + $this->logger?->log( + LogLevel::DEBUG, + 'Set savepoint ' . $this->level . ' ' . __METHOD__, + [LogTypes::KEY => LogTypes::TYPE_TRANSACTION] + ); $this->createSavepoint('LEVEL' . $this->level); } else { $this->logger?->log( LogLevel::DEBUG, - 'Transaction not started: nested transaction not supported ' . __METHOD__ + 'Transaction not started: nested transaction not supported ' . __METHOD__, + [LogTypes::KEY => LogTypes::TYPE_TRANSACTION] ); throw new NotSupportedException('Transaction not started: nested transaction not supported.'); @@ -94,19 +100,28 @@ public function commit(): void $this->level--; if ($this->level === 0) { - $this->logger?->log(LogLevel::DEBUG, 'Commit transaction ' . __METHOD__); + $this->logger?->log( + LogLevel::DEBUG, + 'Commit transaction ' . __METHOD__, + [LogTypes::KEY => LogTypes::TYPE_TRANSACTION] + ); $this->db->getPDO()?->commit(); return; } if ($this->db->isSavepointEnabled()) { - $this->logger?->log(LogLevel::DEBUG, 'Release savepoint ' . $this->level . ' ' . __METHOD__); + $this->logger?->log( + LogLevel::DEBUG, + 'Release savepoint ' . $this->level . ' ' . __METHOD__, + [LogTypes::KEY => LogTypes::TYPE_TRANSACTION] + ); $this->releaseSavepoint('LEVEL' . $this->level); } else { $this->logger?->log( LogLevel::INFO, - 'Transaction not committed: nested transaction not supported ' . __METHOD__ + 'Transaction not committed: nested transaction not supported ' . __METHOD__, + [LogTypes::KEY => LogTypes::TYPE_TRANSACTION] ); } } @@ -135,19 +150,28 @@ public function rollBack(): void $this->level--; if ($this->level === 0) { - $this->logger?->log(LogLevel::INFO, 'Roll back transaction ' . __METHOD__); + $this->logger?->log( + LogLevel::INFO, + 'Roll back transaction ' . __METHOD__, + [LogTypes::KEY => LogTypes::TYPE_TRANSACTION] + ); $this->db->getPDO()?->rollBack(); return; } if ($this->db->isSavepointEnabled()) { - $this->logger?->log(LogLevel::DEBUG, 'Roll back to savepoint ' . $this->level . ' ' . __METHOD__); + $this->logger?->log( + LogLevel::DEBUG, + 'Roll back to savepoint ' . $this->level . ' ' . __METHOD__, + [LogTypes::KEY => LogTypes::TYPE_TRANSACTION] + ); $this->rollBackSavepoint('LEVEL' . $this->level); } else { $this->logger?->log( LogLevel::INFO, - 'Transaction not rolled back: nested transaction not supported ' . __METHOD__ + 'Transaction not rolled back: nested transaction not supported ' . __METHOD__, + [LogTypes::KEY => LogTypes::TYPE_TRANSACTION] ); } } @@ -160,7 +184,8 @@ public function setIsolationLevel(string $level): void $this->logger?->log( LogLevel::DEBUG, - 'Setting transaction isolation level to ' . $this->level . ' ' . __METHOD__ + 'Setting transaction isolation level to ' . $this->level . ' ' . __METHOD__, + [LogTypes::KEY => LogTypes::TYPE_TRANSACTION] ); $this->setTransactionIsolationLevel($level); } diff --git a/src/Driver/Pdo/LogTypes.php b/src/Driver/Pdo/LogTypes.php new file mode 100644 index 000000000..7553dd053 --- /dev/null +++ b/src/Driver/Pdo/LogTypes.php @@ -0,0 +1,14 @@ + Date: Thu, 4 Jan 2024 12:59:24 +0300 Subject: [PATCH 02/12] update --- CHANGELOG.md | 1 + src/Driver/Pdo/AbstractPdoCommand.php | 6 +++--- tests/Common/CommonPdoCommandTest.php | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1448fd31f..a5207b632 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ - Enh #784: Remove unused code in `AbstractSchema::getTableIndexes()` (@vjik) - Bug #788: Fix casting integer to string in `AbstractCommand::getRawSql()` (@Tigrov) - Enh #789: Remove unnecessary type casting to array in `AbstractDMLQueryBuilder::getTableUniqueColumnNames()` (@Tigrov) +- Enh #794: Added category in log context (@darkdef) ## 1.2.0 November 12, 2023 diff --git a/src/Driver/Pdo/AbstractPdoCommand.php b/src/Driver/Pdo/AbstractPdoCommand.php index aedcdf621..96e74c90a 100644 --- a/src/Driver/Pdo/AbstractPdoCommand.php +++ b/src/Driver/Pdo/AbstractPdoCommand.php @@ -258,10 +258,10 @@ protected function internalGetQueryResult(int $queryMode): mixed protected function queryInternal(int $queryMode): mixed { $logCategory = self::class . '::' . $this->getQueryMode($queryMode); - $rawSql = $this->getRawSql(); if ($this->logger !== null) { - $this->logger?->log(LogLevel::INFO, $rawSql, [$logCategory, LogTypes::KEY => LogTypes::TYPE_QUERY]); + $rawSql = $this->getRawSql(); + $this->logger->log(LogLevel::INFO, $rawSql, [$logCategory, LogTypes::KEY => LogTypes::TYPE_QUERY]); } $queryContext = new CommandContext(__METHOD__, $logCategory, $this->getSql(), $this->getParams()); @@ -271,7 +271,7 @@ protected function queryInternal(int $queryMode): mixed * @psalm-suppress RedundantConditionGivenDocblockType * @psalm-suppress DocblockTypeContradiction */ - $this->profiler?->begin($rawSql, $queryContext); + $this->profiler?->begin($rawSql ??= $this->getRawSql(), $queryContext); try { /** @psalm-var mixed $result */ $result = parent::queryInternal($queryMode); diff --git a/tests/Common/CommonPdoCommandTest.php b/tests/Common/CommonPdoCommandTest.php index 87cb072c7..4584b7e00 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 + $params + ['log-type' => 'query'] ); return $logger; } From 785f989d6465408c97d4b32713483ad46e5165e7 Mon Sep 17 00:00:00 2001 From: Valerii Gorbachev Date: Thu, 4 Jan 2024 13:46:22 +0300 Subject: [PATCH 03/12] Update CHANGELOG.md Co-authored-by: Alexander Makarov --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a5207b632..4d6ec2ce5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,7 +16,7 @@ - Enh #784: Remove unused code in `AbstractSchema::getTableIndexes()` (@vjik) - Bug #788: Fix casting integer to string in `AbstractCommand::getRawSql()` (@Tigrov) - Enh #789: Remove unnecessary type casting to array in `AbstractDMLQueryBuilder::getTableUniqueColumnNames()` (@Tigrov) -- Enh #794: Added category in log context (@darkdef) +- Enh #794: Add category to log context (@darkdef) ## 1.2.0 November 12, 2023 From d02746e215a4ce31ac465434122b98602cb1b342 Mon Sep 17 00:00:00 2001 From: Valerii Gorbachev Date: Thu, 4 Jan 2024 13:46:32 +0300 Subject: [PATCH 04/12] Update docs/en/connection/logger.md Co-authored-by: Alexander Makarov --- docs/en/connection/logger.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/en/connection/logger.md b/docs/en/connection/logger.md index 7ae3aa8ae..f4ec1f3c5 100644 --- a/docs/en/connection/logger.md +++ b/docs/en/connection/logger.md @@ -64,7 +64,8 @@ For other DBMS refer to ["Create connecton"](/docs/en/README.md#create-connectio ## Advanced usage of Logger -If you needed re-define logger messages or increase(decrease) level of log: +If you need to redefine logger messages or increase/decrease logging level: + 1. Create custom logger class 2. Use context for detect type of message in method "log" From 4ab0355942ae81e7372c87cfe192dfb83b44bfcd Mon Sep 17 00:00:00 2001 From: Valerii Gorbachev Date: Thu, 4 Jan 2024 13:46:53 +0300 Subject: [PATCH 05/12] Update docs/en/connection/logger.md Co-authored-by: Alexander Makarov --- docs/en/connection/logger.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/en/connection/logger.md b/docs/en/connection/logger.md index f4ec1f3c5..c28a13791 100644 --- a/docs/en/connection/logger.md +++ b/docs/en/connection/logger.md @@ -66,8 +66,8 @@ For other DBMS refer to ["Create connecton"](/docs/en/README.md#create-connectio If you need to redefine logger messages or increase/decrease logging level: -1. Create custom logger class -2. Use context for detect type of message in method "log" +1. Create a custom logger class +2. Use the context to detect type of the message in the "log" method ```php Date: Thu, 4 Jan 2024 14:58:37 +0300 Subject: [PATCH 06/12] update2 --- src/Driver/Pdo/AbstractPdoCommand.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/Driver/Pdo/AbstractPdoCommand.php b/src/Driver/Pdo/AbstractPdoCommand.php index 96e74c90a..ad8e6d4ad 100644 --- a/src/Driver/Pdo/AbstractPdoCommand.php +++ b/src/Driver/Pdo/AbstractPdoCommand.php @@ -259,10 +259,7 @@ protected function queryInternal(int $queryMode): mixed { $logCategory = self::class . '::' . $this->getQueryMode($queryMode); - if ($this->logger !== null) { - $rawSql = $this->getRawSql(); - $this->logger->log(LogLevel::INFO, $rawSql, [$logCategory, LogTypes::KEY => LogTypes::TYPE_QUERY]); - } + $this->logger?->log(LogLevel::INFO, $rawSql = $this->getRawSql(), [$logCategory, LogTypes::KEY => LogTypes::TYPE_QUERY]); $queryContext = new CommandContext(__METHOD__, $logCategory, $this->getSql(), $this->getParams()); From 632638a51e0825e46de96e0df7fb06307707742e Mon Sep 17 00:00:00 2001 From: darkdef Date: Sat, 13 Jan 2024 12:14:38 +0300 Subject: [PATCH 07/12] review update --- CHANGELOG.md | 2 +- docs/en/connection/logger.md | 4 ++-- src/Driver/Pdo/AbstractPdoCommand.php | 2 +- src/Driver/Pdo/AbstractPdoConnection.php | 8 ++++---- src/Driver/Pdo/AbstractPdoTransaction.php | 20 ++++++++++---------- src/Driver/Pdo/LogType.php | 14 ++++++++++++++ src/Driver/Pdo/LogTypes.php | 14 -------------- 7 files changed, 32 insertions(+), 32 deletions(-) create mode 100644 src/Driver/Pdo/LogType.php delete mode 100644 src/Driver/Pdo/LogTypes.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 4d6ec2ce5..917eb85b2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,7 +16,7 @@ - Enh #784: Remove unused code in `AbstractSchema::getTableIndexes()` (@vjik) - Bug #788: Fix casting integer to string in `AbstractCommand::getRawSql()` (@Tigrov) - Enh #789: Remove unnecessary type casting to array in `AbstractDMLQueryBuilder::getTableUniqueColumnNames()` (@Tigrov) -- Enh #794: Add category to log context (@darkdef) +- Enh #794: Add message type to log context (@darkdef) ## 1.2.0 November 12, 2023 diff --git a/docs/en/connection/logger.md b/docs/en/connection/logger.md index c28a13791..93376e485 100644 --- a/docs/en/connection/logger.md +++ b/docs/en/connection/logger.md @@ -74,13 +74,13 @@ If you need to redefine logger messages or increase/decrease logging level: declare(strict_types=1); -use Yiisoft\Db\Driver\Pdo\LogTypes; +use Yiisoft\Db\Driver\Pdo\LogType; class MyLogger extends ParentLoggerClass implements LoggerInterface { public function log($level, string|\Stringable $message, array $context = []): void { - if ($context[LogTypes::KEY] === LogTypes::TYPE_QUERY) { + if ($context[LogType::LOG_TYPE] === LogType::QUERY) { ... your logic here } } diff --git a/src/Driver/Pdo/AbstractPdoCommand.php b/src/Driver/Pdo/AbstractPdoCommand.php index ad8e6d4ad..2d6844086 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, LogTypes::KEY => LogTypes::TYPE_QUERY]); + $this->logger?->log(LogLevel::INFO, $rawSql = $this->getRawSql(), [$logCategory, LogType::LOG_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 6620874c5..02d2e732e 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, [LogTypes::KEY => LogTypes::TYPE_CONNECTION]); + $this->logger?->log(LogLevel::INFO, $token, [LogType::LOG_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, [LogTypes::KEY => LogTypes::TYPE_CONNECTION]); + $this->logger?->log(LogLevel::ERROR, $token, [LogType::LOG_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__, - [LogTypes::KEY => LogTypes::TYPE_CONNECTION], + [LogType::LOG_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__, LogTypes::KEY => LogTypes::TYPE_TRANSACTION]); + $this->logger?->log(LogLevel::ERROR, (string) $e, [__METHOD__, LogType::LOG_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 324751cf9..6a6540af6 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__, - [LogTypes::KEY => LogTypes::TYPE_TRANSACTION] + [LogType::LOG_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__, - [LogTypes::KEY => LogTypes::TYPE_TRANSACTION] + [LogType::LOG_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__, - [LogTypes::KEY => LogTypes::TYPE_TRANSACTION] + [LogType::LOG_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__, - [LogTypes::KEY => LogTypes::TYPE_TRANSACTION] + [LogType::LOG_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__, - [LogTypes::KEY => LogTypes::TYPE_TRANSACTION] + [LogType::LOG_TYPE => LogType::TRANSACTION] ); $this->releaseSavepoint('LEVEL' . $this->level); } else { $this->logger?->log( LogLevel::INFO, 'Transaction not committed: nested transaction not supported ' . __METHOD__, - [LogTypes::KEY => LogTypes::TYPE_TRANSACTION] + [LogType::LOG_TYPE => LogType::TRANSACTION] ); } } @@ -153,7 +153,7 @@ public function rollBack(): void $this->logger?->log( LogLevel::INFO, 'Roll back transaction ' . __METHOD__, - [LogTypes::KEY => LogTypes::TYPE_TRANSACTION] + [LogType::LOG_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__, - [LogTypes::KEY => LogTypes::TYPE_TRANSACTION] + [LogType::LOG_TYPE => LogType::TRANSACTION] ); $this->rollBackSavepoint('LEVEL' . $this->level); } else { $this->logger?->log( LogLevel::INFO, 'Transaction not rolled back: nested transaction not supported ' . __METHOD__, - [LogTypes::KEY => LogTypes::TYPE_TRANSACTION] + [LogType::LOG_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__, - [LogTypes::KEY => LogTypes::TYPE_TRANSACTION] + [LogType::LOG_TYPE => LogType::TRANSACTION] ); $this->setTransactionIsolationLevel($level); } diff --git a/src/Driver/Pdo/LogType.php b/src/Driver/Pdo/LogType.php new file mode 100644 index 000000000..658ca50c2 --- /dev/null +++ b/src/Driver/Pdo/LogType.php @@ -0,0 +1,14 @@ + Date: Sat, 13 Jan 2024 14:33:52 +0300 Subject: [PATCH 08/12] Revert "review update" This reverts commit 632638a51e0825e46de96e0df7fb06307707742e. --- CHANGELOG.md | 2 +- docs/en/connection/logger.md | 4 ++-- src/Driver/Pdo/AbstractPdoCommand.php | 2 +- src/Driver/Pdo/AbstractPdoConnection.php | 8 ++++---- src/Driver/Pdo/AbstractPdoTransaction.php | 20 ++++++++++---------- src/Driver/Pdo/LogType.php | 14 -------------- src/Driver/Pdo/LogTypes.php | 14 ++++++++++++++ 7 files changed, 32 insertions(+), 32 deletions(-) delete mode 100644 src/Driver/Pdo/LogType.php create mode 100644 src/Driver/Pdo/LogTypes.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 917eb85b2..4d6ec2ce5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,7 +16,7 @@ - Enh #784: Remove unused code in `AbstractSchema::getTableIndexes()` (@vjik) - Bug #788: Fix casting integer to string in `AbstractCommand::getRawSql()` (@Tigrov) - Enh #789: Remove unnecessary type casting to array in `AbstractDMLQueryBuilder::getTableUniqueColumnNames()` (@Tigrov) -- Enh #794: Add message type to log context (@darkdef) +- Enh #794: Add category to log context (@darkdef) ## 1.2.0 November 12, 2023 diff --git a/docs/en/connection/logger.md b/docs/en/connection/logger.md index 93376e485..c28a13791 100644 --- a/docs/en/connection/logger.md +++ b/docs/en/connection/logger.md @@ -74,13 +74,13 @@ If you need to redefine logger messages or increase/decrease logging level: declare(strict_types=1); -use Yiisoft\Db\Driver\Pdo\LogType; +use Yiisoft\Db\Driver\Pdo\LogTypes; class MyLogger extends ParentLoggerClass implements LoggerInterface { public function log($level, string|\Stringable $message, array $context = []): void { - if ($context[LogType::LOG_TYPE] === LogType::QUERY) { + if ($context[LogTypes::KEY] === LogTypes::TYPE_QUERY) { ... your logic here } } diff --git a/src/Driver/Pdo/AbstractPdoCommand.php b/src/Driver/Pdo/AbstractPdoCommand.php index 2d6844086..ad8e6d4ad 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::LOG_TYPE => LogType::QUERY]); + $this->logger?->log(LogLevel::INFO, $rawSql = $this->getRawSql(), [$logCategory, LogTypes::KEY => LogTypes::TYPE_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 02d2e732e..6620874c5 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::LOG_TYPE => LogType::CONNECTION]); + $this->logger?->log(LogLevel::INFO, $token, [LogTypes::KEY => LogTypes::TYPE_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::LOG_TYPE => LogType::CONNECTION]); + $this->logger?->log(LogLevel::ERROR, $token, [LogTypes::KEY => LogTypes::TYPE_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::LOG_TYPE => LogType::CONNECTION], + [LogTypes::KEY => LogTypes::TYPE_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::LOG_TYPE => LogType::TRANSACTION]); + $this->logger?->log(LogLevel::ERROR, (string) $e, [__METHOD__, LogTypes::KEY => LogTypes::TYPE_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 6a6540af6..324751cf9 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::LOG_TYPE => LogType::TRANSACTION] + [LogTypes::KEY => LogTypes::TYPE_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::LOG_TYPE => LogType::TRANSACTION] + [LogTypes::KEY => LogTypes::TYPE_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::LOG_TYPE => LogType::TRANSACTION] + [LogTypes::KEY => LogTypes::TYPE_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::LOG_TYPE => LogType::TRANSACTION] + [LogTypes::KEY => LogTypes::TYPE_TRANSACTION] ); $this->db->getPDO()?->commit(); @@ -114,14 +114,14 @@ public function commit(): void $this->logger?->log( LogLevel::DEBUG, 'Release savepoint ' . $this->level . ' ' . __METHOD__, - [LogType::LOG_TYPE => LogType::TRANSACTION] + [LogTypes::KEY => LogTypes::TYPE_TRANSACTION] ); $this->releaseSavepoint('LEVEL' . $this->level); } else { $this->logger?->log( LogLevel::INFO, 'Transaction not committed: nested transaction not supported ' . __METHOD__, - [LogType::LOG_TYPE => LogType::TRANSACTION] + [LogTypes::KEY => LogTypes::TYPE_TRANSACTION] ); } } @@ -153,7 +153,7 @@ public function rollBack(): void $this->logger?->log( LogLevel::INFO, 'Roll back transaction ' . __METHOD__, - [LogType::LOG_TYPE => LogType::TRANSACTION] + [LogTypes::KEY => LogTypes::TYPE_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::LOG_TYPE => LogType::TRANSACTION] + [LogTypes::KEY => LogTypes::TYPE_TRANSACTION] ); $this->rollBackSavepoint('LEVEL' . $this->level); } else { $this->logger?->log( LogLevel::INFO, 'Transaction not rolled back: nested transaction not supported ' . __METHOD__, - [LogType::LOG_TYPE => LogType::TRANSACTION] + [LogTypes::KEY => LogTypes::TYPE_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::LOG_TYPE => LogType::TRANSACTION] + [LogTypes::KEY => LogTypes::TYPE_TRANSACTION] ); $this->setTransactionIsolationLevel($level); } diff --git a/src/Driver/Pdo/LogType.php b/src/Driver/Pdo/LogType.php deleted file mode 100644 index 658ca50c2..000000000 --- a/src/Driver/Pdo/LogType.php +++ /dev/null @@ -1,14 +0,0 @@ - Date: Sat, 13 Jan 2024 14:59:16 +0300 Subject: [PATCH 09/12] update --- CHANGELOG.md | 2 +- docs/en/connection/logger.md | 4 ++-- src/Driver/Pdo/AbstractPdoCommand.php | 2 +- src/Driver/Pdo/AbstractPdoConnection.php | 8 ++++---- src/Driver/Pdo/AbstractPdoTransaction.php | 20 ++++++++++---------- src/Driver/Pdo/{LogTypes.php => LogType.php} | 2 +- 6 files changed, 19 insertions(+), 19 deletions(-) rename src/Driver/Pdo/{LogTypes.php => LogType.php} (92%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4d6ec2ce5..917eb85b2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,7 +16,7 @@ - Enh #784: Remove unused code in `AbstractSchema::getTableIndexes()` (@vjik) - Bug #788: Fix casting integer to string in `AbstractCommand::getRawSql()` (@Tigrov) - Enh #789: Remove unnecessary type casting to array in `AbstractDMLQueryBuilder::getTableUniqueColumnNames()` (@Tigrov) -- Enh #794: Add category to log context (@darkdef) +- Enh #794: Add message type to log context (@darkdef) ## 1.2.0 November 12, 2023 diff --git a/docs/en/connection/logger.md b/docs/en/connection/logger.md index c28a13791..f0235f858 100644 --- a/docs/en/connection/logger.md +++ b/docs/en/connection/logger.md @@ -74,13 +74,13 @@ If you need to redefine logger messages or increase/decrease logging level: declare(strict_types=1); -use Yiisoft\Db\Driver\Pdo\LogTypes; +use Yiisoft\Db\Driver\Pdo\LogType; class MyLogger extends ParentLoggerClass implements LoggerInterface { public function log($level, string|\Stringable $message, array $context = []): void { - if ($context[LogTypes::KEY] === LogTypes::TYPE_QUERY) { + if ($context[LogType::KEY] === LogType::TYPE_QUERY) { ... your logic here } } diff --git a/src/Driver/Pdo/AbstractPdoCommand.php b/src/Driver/Pdo/AbstractPdoCommand.php index ad8e6d4ad..38bebdd6e 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, LogTypes::KEY => LogTypes::TYPE_QUERY]); + $this->logger?->log(LogLevel::INFO, $rawSql = $this->getRawSql(), [$logCategory, LogType::KEY => LogType::TYPE_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 6620874c5..ab39c0c0e 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, [LogTypes::KEY => LogTypes::TYPE_CONNECTION]); + $this->logger?->log(LogLevel::INFO, $token, [LogType::KEY => LogType::TYPE_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, [LogTypes::KEY => LogTypes::TYPE_CONNECTION]); + $this->logger?->log(LogLevel::ERROR, $token, [LogType::KEY => LogType::TYPE_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__, - [LogTypes::KEY => LogTypes::TYPE_CONNECTION], + [LogType::KEY => LogType::TYPE_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__, LogTypes::KEY => LogTypes::TYPE_TRANSACTION]); + $this->logger?->log(LogLevel::ERROR, (string) $e, [__METHOD__, LogType::KEY => LogType::TYPE_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 324751cf9..a26e1a11f 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__, - [LogTypes::KEY => LogTypes::TYPE_TRANSACTION] + [LogType::KEY => LogType::TYPE_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__, - [LogTypes::KEY => LogTypes::TYPE_TRANSACTION] + [LogType::KEY => LogType::TYPE_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__, - [LogTypes::KEY => LogTypes::TYPE_TRANSACTION] + [LogType::KEY => LogType::TYPE_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__, - [LogTypes::KEY => LogTypes::TYPE_TRANSACTION] + [LogType::KEY => LogType::TYPE_TRANSACTION] ); $this->db->getPDO()?->commit(); @@ -114,14 +114,14 @@ public function commit(): void $this->logger?->log( LogLevel::DEBUG, 'Release savepoint ' . $this->level . ' ' . __METHOD__, - [LogTypes::KEY => LogTypes::TYPE_TRANSACTION] + [LogType::KEY => LogType::TYPE_TRANSACTION] ); $this->releaseSavepoint('LEVEL' . $this->level); } else { $this->logger?->log( LogLevel::INFO, 'Transaction not committed: nested transaction not supported ' . __METHOD__, - [LogTypes::KEY => LogTypes::TYPE_TRANSACTION] + [LogType::KEY => LogType::TYPE_TRANSACTION] ); } } @@ -153,7 +153,7 @@ public function rollBack(): void $this->logger?->log( LogLevel::INFO, 'Roll back transaction ' . __METHOD__, - [LogTypes::KEY => LogTypes::TYPE_TRANSACTION] + [LogType::KEY => LogType::TYPE_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__, - [LogTypes::KEY => LogTypes::TYPE_TRANSACTION] + [LogType::KEY => LogType::TYPE_TRANSACTION] ); $this->rollBackSavepoint('LEVEL' . $this->level); } else { $this->logger?->log( LogLevel::INFO, 'Transaction not rolled back: nested transaction not supported ' . __METHOD__, - [LogTypes::KEY => LogTypes::TYPE_TRANSACTION] + [LogType::KEY => LogType::TYPE_TRANSACTION] ); } } @@ -185,7 +185,7 @@ public function setIsolationLevel(string $level): void $this->logger?->log( LogLevel::DEBUG, 'Setting transaction isolation level to ' . $this->level . ' ' . __METHOD__, - [LogTypes::KEY => LogTypes::TYPE_TRANSACTION] + [LogType::KEY => LogType::TYPE_TRANSACTION] ); $this->setTransactionIsolationLevel($level); } diff --git a/src/Driver/Pdo/LogTypes.php b/src/Driver/Pdo/LogType.php similarity index 92% rename from src/Driver/Pdo/LogTypes.php rename to src/Driver/Pdo/LogType.php index 7553dd053..9504d195c 100644 --- a/src/Driver/Pdo/LogTypes.php +++ b/src/Driver/Pdo/LogType.php @@ -4,7 +4,7 @@ namespace Yiisoft\Db\Driver\Pdo; -class LogTypes +final class LogType { public const KEY = 'log-type'; From 49a5a0797732194a5be316a039fc2ec2ac3e0329 Mon Sep 17 00:00:00 2001 From: darkdef Date: Wed, 17 Jan 2024 20:47:42 +0300 Subject: [PATCH 10/12] 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; } From ca5de23d998aa4e5dafcd6b4bae54d5e3e78cf7d Mon Sep 17 00:00:00 2001 From: darkdef Date: Sun, 21 Jan 2024 12:47:40 +0300 Subject: [PATCH 11/12] update --- CHANGELOG.md | 1 + src/Driver/Pdo/AbstractPdoCommand.php | 13 ++++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4b096acd4..22389beb9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ - Bug #788: Fix casting integer to string in `AbstractCommand::getRawSql()` (@Tigrov) - Enh #789: Remove unnecessary type casting to array in `AbstractDMLQueryBuilder::getTableUniqueColumnNames()` (@Tigrov) - Enh #795: Allow to use `DMLQueryBuilderInterface::batchInsert()` method with empty columns (@Tigrov) +- Enh #794: Add message type to log context (@darkdef) ## 1.2.0 November 12, 2023 diff --git a/src/Driver/Pdo/AbstractPdoCommand.php b/src/Driver/Pdo/AbstractPdoCommand.php index edd2adf81..506bbadea 100644 --- a/src/Driver/Pdo/AbstractPdoCommand.php +++ b/src/Driver/Pdo/AbstractPdoCommand.php @@ -255,11 +255,22 @@ protected function internalGetQueryResult(int $queryMode): mixed return $result; } + /** + * Logs the current database query if query logging is on and returns the profiling token if profiling is on. + */ + protected function logQuery(string $rawSql, string $category): void + { + $this->logger?->log(LogLevel::INFO, $rawSql, [$logCategory, 'type' => LogType::QUERY]); + } + protected function queryInternal(int $queryMode): mixed { $logCategory = self::class . '::' . $this->getQueryMode($queryMode); - $this->logger?->log(LogLevel::INFO, $rawSql = $this->getRawSql(), [$logCategory, 'type' => LogType::QUERY]); + if ($this->logger !== null) { + $rawSql = $this->getRawSql(); + $this->logQuery($rawSql, $logCategory); + } $queryContext = new CommandContext(__METHOD__, $logCategory, $this->getSql(), $this->getParams()); From 1789d2612c5deb0e8d3b8803a19fa4488a548471 Mon Sep 17 00:00:00 2001 From: darkdef Date: Sun, 21 Jan 2024 12:54:07 +0300 Subject: [PATCH 12/12] fix --- src/Driver/Pdo/AbstractPdoCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Driver/Pdo/AbstractPdoCommand.php b/src/Driver/Pdo/AbstractPdoCommand.php index 506bbadea..3f632e3ce 100644 --- a/src/Driver/Pdo/AbstractPdoCommand.php +++ b/src/Driver/Pdo/AbstractPdoCommand.php @@ -260,7 +260,7 @@ protected function internalGetQueryResult(int $queryMode): mixed */ protected function logQuery(string $rawSql, string $category): void { - $this->logger?->log(LogLevel::INFO, $rawSql, [$logCategory, 'type' => LogType::QUERY]); + $this->logger?->log(LogLevel::INFO, $rawSql, [$category, 'type' => LogType::QUERY]); } protected function queryInternal(int $queryMode): mixed