Final Class Yiisoft\Log\Target\Db\DbTarget
| Inheritance | Yiisoft\ |
|---|
Stores log messages in a database table.
Use {@see \
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Yiisoft\ |
|
| getDb() | Gets an instance of a database connection. | Yiisoft\ |
| getTable() | Gets the name of the database table to store the log messages. | Yiisoft\ |
Protected Methods
| Method | Description | Defined By |
|---|---|---|
| export() | Stores log messages to the database. | Yiisoft\ |
Method Details
| public mixed __construct ( \ | ||
| $db | \ |
The database connection instance. |
| $table | string |
The name of the database table to store the log messages. Defaults to "{{%yii_log}}". |
| $levels | string[] |
The {@see \ |
public function __construct(
private readonly ConnectionInterface $db,
private readonly string $table = '{{%yii_log}}',
array $levels = [],
) {
parent::__construct($levels);
}
Stores log messages to the database.
| protected void export ( ) | ||
| throws | RuntimeException |
If the log can't be exported. |
|---|---|---|
protected function export(): void
{
$formattedMessages = $this->getFormattedMessages();
$table = $this->db->getQuoter()->quoteTableName($this->table);
try {
$command = $this->db->createCommand();
foreach ($this->getMessages() as $key => $message) {
/** @psalm-var mixed $logTime */
$logTime = $message->context('time');
$columns = [
'level' => $message->level(),
'category' => $message->context('category', ''),
'log_time' => $logTime,
'message' => $formattedMessages[$key],
];
if ($logTime === null) {
unset($columns['log_time']);
}
$command->insert($table, $columns)->execute();
}
} catch (Throwable $e) {
throw new RuntimeException($e->getMessage(), (int) $e->getCode(), $e);
}
}
Gets an instance of a database connection.
| public \ |
public function getDb(): ConnectionInterface
{
return $this->db;
}
User Contributed Notes
Leave a comment
Join the conversation to share a note.