From e5dd0dce17f54424f8c3279b27b3a724a507da40 Mon Sep 17 00:00:00 2001 From: darkdef Date: Sat, 13 Jan 2024 14:59:16 +0300 Subject: [PATCH] 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';