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/docs/en/connection/logger.md b/docs/en/connection/logger.md index 499d88dbd..d27a496c6 100644 --- a/docs/en/connection/logger.md +++ b/docs/en/connection/logger.md @@ -61,3 +61,30 @@ return [ ``` For other DBMS refer to ["Create connecton"](/docs/en/README.md#create-connection) section. + +## Advanced usage of Logger + +If you need to redefine logger messages or increase/decrease logging level: + +1. Create a custom logger class +2. Use the context to detect type of the message in the "log" method + +```php +logger?->log(LogLevel::INFO, $rawSql, [$category]); + $this->logger?->log(LogLevel::INFO, $rawSql, [$category, 'type' => LogType::QUERY]); } protected function queryInternal(int $queryMode): mixed diff --git a/src/Driver/Pdo/AbstractPdoConnection.php b/src/Driver/Pdo/AbstractPdoConnection.php index abcaf7198..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); + $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); + $this->logger?->log(LogLevel::ERROR, $token, ['type' => LogType::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__, + ['type' => LogType::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__, '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 647b0941b..7dff9fac8 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__, + ['type' => LogType::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__, + ['type' => LogType::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__, + ['type' => LogType::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__, + ['type' => LogType::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__, + ['type' => LogType::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__, + ['type' => LogType::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__, + ['type' => LogType::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__, + ['type' => LogType::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__, + ['type' => LogType::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__, + ['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..0f27b7c28 --- /dev/null +++ b/src/Driver/Pdo/LogType.php @@ -0,0 +1,12 @@ +with( LogLevel::INFO, $sql, - $params + $params + ['type' => 'query'] ); return $logger; }