Final Class Yiisoft\Log\Target\Db\DbTarget
| Inheritance | Yiisoft\Log\Target\Db\DbTarget » Yiisoft\Log\Target |
|---|
Stores log messages in a database table.
Use {@see \Yiisoft\Log\Target\Db\DbSchemaManager::ensureTable()} to initialize database schema.
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Yiisoft\Log\Target\Db\DbTarget | |
| getDb() | Gets an instance of a database connection. | Yiisoft\Log\Target\Db\DbTarget |
| getTable() | Gets the name of the database table to store the log messages. | Yiisoft\Log\Target\Db\DbTarget |
Protected Methods
| Method | Description | Defined By |
|---|---|---|
| export() | Stores log messages to the database. | Yiisoft\Log\Target\Db\DbTarget |
Method Details
| public mixed __construct ( \Yiisoft\Db\Connection\ConnectionInterface $db, string $table = '{{%yii_log}}', string[] $levels = [] ) | ||
| $db | \Yiisoft\Db\Connection\ConnectionInterface |
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 \Psr\Log\LogLevel log message levels} that this target is interested in. |
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 \Yiisoft\Db\Connection\ConnectionInterface getDb ( ) |
public function getDb(): ConnectionInterface
{
return $this->db;
}
Signup or Login in order to comment.