Skip to content

Commit ef81e8b

Browse files
darkdefsamdarkvjik
authored
Logger context (#794)
* Logger context Co-authored-by: Alexander Makarov <[email protected]> Co-authored-by: Sergei Predvoditelev <[email protected]>
1 parent 38c9090 commit ef81e8b

File tree

7 files changed

+81
-15
lines changed

7 files changed

+81
-15
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
- Bug #788: Fix casting integer to string in `AbstractCommand::getRawSql()` (@Tigrov)
1818
- Enh #789: Remove unnecessary type casting to array in `AbstractDMLQueryBuilder::getTableUniqueColumnNames()` (@Tigrov)
1919
- Enh #795: Allow to use `DMLQueryBuilderInterface::batchInsert()` method with empty columns (@Tigrov)
20+
- Enh #794: Add message type to log context (@darkdef)
2021

2122
## 1.2.0 November 12, 2023
2223

docs/en/connection/logger.md

+27
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,30 @@ return [
6161
```
6262

6363
For other DBMS refer to ["Create connecton"](/docs/en/README.md#create-connection) section.
64+
65+
## Advanced usage of Logger
66+
67+
If you need to redefine logger messages or increase/decrease logging level:
68+
69+
1. Create a custom logger class
70+
2. Use the context to detect type of the message in the "log" method
71+
72+
```php
73+
<?php
74+
75+
declare(strict_types=1);
76+
77+
use Yiisoft\Db\Driver\Pdo\LogType;
78+
79+
class MyLogger extends ParentLoggerClass implements LoggerInterface
80+
{
81+
public function log($level, string|\Stringable $message, array $context = []): void
82+
{
83+
if ($context['type'] === LogType::QUERY) {
84+
... your logic here
85+
}
86+
}
87+
88+
// implements other methods of LoggerInterface without changes
89+
}
90+
```

src/Driver/Pdo/AbstractPdoCommand.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ protected function internalGetQueryResult(int $queryMode): mixed
260260
*/
261261
protected function logQuery(string $rawSql, string $category): void
262262
{
263-
$this->logger?->log(LogLevel::INFO, $rawSql, [$category]);
263+
$this->logger?->log(LogLevel::INFO, $rawSql, [$category, 'type' => LogType::QUERY]);
264264
}
265265

266266
protected function queryInternal(int $queryMode): mixed

src/Driver/Pdo/AbstractPdoConnection.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,13 @@ public function open(): void
101101
$connectionContext = new ConnectionContext(__METHOD__);
102102

103103
try {
104-
$this->logger?->log(LogLevel::INFO, $token);
104+
$this->logger?->log(LogLevel::INFO, $token, ['type' => LogType::CONNECTION]);
105105
$this->profiler?->begin($token, $connectionContext);
106106
$this->initConnection();
107107
$this->profiler?->end($token, $connectionContext);
108108
} catch (PDOException $e) {
109109
$this->profiler?->end($token, $connectionContext->setException($e));
110-
$this->logger?->log(LogLevel::ERROR, $token);
110+
$this->logger?->log(LogLevel::ERROR, $token, ['type' => LogType::CONNECTION]);
111111

112112
throw new Exception($e->getMessage(), (array) $e->errorInfo, $e);
113113
}
@@ -119,6 +119,7 @@ public function close(): void
119119
$this->logger?->log(
120120
LogLevel::DEBUG,
121121
'Closing DB connection: ' . $this->driver->getDsn() . ' ' . __METHOD__,
122+
['type' => LogType::CONNECTION],
122123
);
123124

124125
$this->pdo = null;
@@ -225,7 +226,7 @@ protected function rollbackTransactionOnLevel(TransactionInterface $transaction,
225226
try {
226227
$transaction->rollBack();
227228
} catch (Throwable $e) {
228-
$this->logger?->log(LogLevel::ERROR, (string) $e, [__METHOD__]);
229+
$this->logger?->log(LogLevel::ERROR, (string) $e, [__METHOD__, 'type' => LogType::TRANSACTION]);
229230
/** hide this exception to be able to continue throwing original exception outside */
230231
}
231232
}

src/Driver/Pdo/AbstractPdoTransaction.php

+35-10
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ public function begin(string $isolationLevel = null): void
6060
$this->logger?->log(
6161
LogLevel::DEBUG,
6262
'Begin transaction' . ($isolationLevel ? ' with isolation level ' . $isolationLevel : '')
63-
. ' ' . __METHOD__
63+
. ' ' . __METHOD__,
64+
['type' => LogType::TRANSACTION]
6465
);
6566

6667
$this->db->getPDO()?->beginTransaction();
@@ -70,13 +71,18 @@ public function begin(string $isolationLevel = null): void
7071
}
7172

7273
if ($this->db->isSavepointEnabled()) {
73-
$this->logger?->log(LogLevel::DEBUG, 'Set savepoint ' . $this->level . ' ' . __METHOD__);
74+
$this->logger?->log(
75+
LogLevel::DEBUG,
76+
'Set savepoint ' . $this->level . ' ' . __METHOD__,
77+
['type' => LogType::TRANSACTION]
78+
);
7479

7580
$this->createSavepoint('LEVEL' . $this->level);
7681
} else {
7782
$this->logger?->log(
7883
LogLevel::DEBUG,
79-
'Transaction not started: nested transaction not supported ' . __METHOD__
84+
'Transaction not started: nested transaction not supported ' . __METHOD__,
85+
['type' => LogType::TRANSACTION]
8086
);
8187

8288
throw new NotSupportedException('Transaction not started: nested transaction not supported.');
@@ -94,19 +100,28 @@ public function commit(): void
94100
$this->level--;
95101

96102
if ($this->level === 0) {
97-
$this->logger?->log(LogLevel::DEBUG, 'Commit transaction ' . __METHOD__);
103+
$this->logger?->log(
104+
LogLevel::DEBUG,
105+
'Commit transaction ' . __METHOD__,
106+
['type' => LogType::TRANSACTION]
107+
);
98108
$this->db->getPDO()?->commit();
99109

100110
return;
101111
}
102112

103113
if ($this->db->isSavepointEnabled()) {
104-
$this->logger?->log(LogLevel::DEBUG, 'Release savepoint ' . $this->level . ' ' . __METHOD__);
114+
$this->logger?->log(
115+
LogLevel::DEBUG,
116+
'Release savepoint ' . $this->level . ' ' . __METHOD__,
117+
['type' => LogType::TRANSACTION]
118+
);
105119
$this->releaseSavepoint('LEVEL' . $this->level);
106120
} else {
107121
$this->logger?->log(
108122
LogLevel::INFO,
109-
'Transaction not committed: nested transaction not supported ' . __METHOD__
123+
'Transaction not committed: nested transaction not supported ' . __METHOD__,
124+
['type' => LogType::TRANSACTION]
110125
);
111126
}
112127
}
@@ -135,19 +150,28 @@ public function rollBack(): void
135150
$this->level--;
136151

137152
if ($this->level === 0) {
138-
$this->logger?->log(LogLevel::INFO, 'Roll back transaction ' . __METHOD__);
153+
$this->logger?->log(
154+
LogLevel::INFO,
155+
'Roll back transaction ' . __METHOD__,
156+
['type' => LogType::TRANSACTION]
157+
);
139158
$this->db->getPDO()?->rollBack();
140159

141160
return;
142161
}
143162

144163
if ($this->db->isSavepointEnabled()) {
145-
$this->logger?->log(LogLevel::DEBUG, 'Roll back to savepoint ' . $this->level . ' ' . __METHOD__);
164+
$this->logger?->log(
165+
LogLevel::DEBUG,
166+
'Roll back to savepoint ' . $this->level . ' ' . __METHOD__,
167+
['type' => LogType::TRANSACTION]
168+
);
146169
$this->rollBackSavepoint('LEVEL' . $this->level);
147170
} else {
148171
$this->logger?->log(
149172
LogLevel::INFO,
150-
'Transaction not rolled back: nested transaction not supported ' . __METHOD__
173+
'Transaction not rolled back: nested transaction not supported ' . __METHOD__,
174+
['type' => LogType::TRANSACTION]
151175
);
152176
}
153177
}
@@ -160,7 +184,8 @@ public function setIsolationLevel(string $level): void
160184

161185
$this->logger?->log(
162186
LogLevel::DEBUG,
163-
'Setting transaction isolation level to ' . $this->level . ' ' . __METHOD__
187+
'Setting transaction isolation level to ' . $this->level . ' ' . __METHOD__,
188+
['type' => LogType::TRANSACTION]
164189
);
165190
$this->setTransactionIsolationLevel($level);
166191
}

src/Driver/Pdo/LogType.php

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yiisoft\Db\Driver\Pdo;
6+
7+
final class LogType
8+
{
9+
public const CONNECTION = 'connection';
10+
public const QUERY = 'query';
11+
public const TRANSACTION = 'transaction';
12+
}

tests/Common/CommonPdoCommandTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ protected function createQueryLogger(string $sql, array $params = []): LoggerInt
239239
->with(
240240
LogLevel::INFO,
241241
$sql,
242-
$params
242+
$params + ['type' => 'query']
243243
);
244244
return $logger;
245245
}

0 commit comments

Comments
 (0)