Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
darkdef committed Jan 17, 2024
1 parent f342dce commit 49a5a07
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 22 deletions.
2 changes: 1 addition & 1 deletion docs/en/connection/logger.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
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, 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());

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, [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);
}
Expand All @@ -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;
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__, 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 */
}
}
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__,
[LogType::KEY => LogType::TYPE_TRANSACTION]
['type' => LogType::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__,
[LogType::KEY => LogType::TYPE_TRANSACTION]
['type' => LogType::TRANSACTION]
);

$this->createSavepoint('LEVEL' . $this->level);
} else {
$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.');
Expand All @@ -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();

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

Expand All @@ -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]
);
}
}
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__,
[LogType::KEY => LogType::TYPE_TRANSACTION]
['type' => LogType::TRANSACTION]

Check warning on line 188 in src/Driver/Pdo/AbstractPdoTransaction.php

View check run for this annotation

Codecov / codecov/patch

src/Driver/Pdo/AbstractPdoTransaction.php#L187-L188

Added lines #L187 - L188 were not covered by tests
);
$this->setTransactionIsolationLevel($level);
}
Expand Down
8 changes: 3 additions & 5 deletions src/Driver/Pdo/LogType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
2 changes: 1 addition & 1 deletion tests/Common/CommonPdoCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 49a5a07

Please sign in to comment.