Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
darkdef committed Jan 13, 2024
1 parent 58c2b20 commit e5dd0dc
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 19 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions docs/en/connection/logger.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Driver/Pdo/AbstractPdoCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down
8 changes: 4 additions & 4 deletions src/Driver/Pdo/AbstractPdoConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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;
Expand Down Expand Up @@ -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 */
}
}
Expand Down
20 changes: 10 additions & 10 deletions src/Driver/Pdo/AbstractPdoTransaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -74,15 +74,15 @@ 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);
} else {
$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.');
Expand All @@ -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();

Expand All @@ -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]
);
}
}
Expand Down Expand Up @@ -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();

Expand All @@ -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]
);
}
}
Expand All @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Driver/Pdo/LogTypes.php → src/Driver/Pdo/LogType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Yiisoft\Db\Driver\Pdo;

class LogTypes
final class LogType
{
public const KEY = 'log-type';

Expand Down

0 comments on commit e5dd0dc

Please sign in to comment.