0 follower

Final Class Yiisoft\Log\Target\Db\DbTarget

InheritanceYiisoft\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

Hide inherited 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

Hide inherited methods

Method Description Defined By
export() Stores log messages to the database. Yiisoft\Log\Target\Db\DbTarget

Method Details

Hide inherited methods

__construct() public method

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);
}

            
export() protected method

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);
    }
}

            
getDb() public method

Gets an instance of a database connection.

public \Yiisoft\Db\Connection\ConnectionInterface getDb ( )

                public function getDb(): ConnectionInterface
{
    return $this->db;
}

            
getTable() public method

Gets the name of the database table to store the log messages.

public string getTable ( )

                public function getTable(): string
{
    return $this->table;
}