From 0857a35d6ec0c4c024951a1a1a951e70d0f30cee Mon Sep 17 00:00:00 2001 From: darkdef Date: Wed, 3 Jan 2024 11:40:24 +0300 Subject: [PATCH] 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 @@ +