Class yii\mongodb\log\MongoDbTarget

Inheritanceyii\mongodb\log\MongoDbTarget » yii\log\Target
Available since extension's version2.0
Source Code https://github.com/yiisoft/yii2-mongodb/blob/master/log/MongoDbTarget.php

MongoDbTarget stores log messages in a MongoDB collection.

By default, MongoDbTarget stores the log messages in a MongoDB collection named 'log'. The collection can be changed by setting the $logCollection property.

Public Properties

Hide inherited properties

Property Type Description Defined By
$db yii\mongodb\Connection|string The MongoDB connection object or the application component ID of the MongoDB connection. yii\mongodb\log\MongoDbTarget
$logCollection string|array The name of the MongoDB collection that stores the session data. yii\mongodb\log\MongoDbTarget

Public Methods

Hide inherited methods

Method Description Defined By
export() Stores log messages to MongoDB collection. yii\mongodb\log\MongoDbTarget
init() Initializes the MongoDbTarget component. yii\mongodb\log\MongoDbTarget

Property Details

Hide inherited properties

$db public property

The MongoDB connection object or the application component ID of the MongoDB connection. After the MongoDbTarget object is created, if you want to change this property, you should only assign it with a MongoDB connection object.

public yii\mongodb\Connection|string $db 'mongodb'
$logCollection public property

The name of the MongoDB collection that stores the session data. Please refer to yii\mongodb\Connection::getCollection() on how to specify this parameter. This collection is better to be pre-created with fields 'id' and 'expire' indexed.

public string|array $logCollection 'log'

Method Details

Hide inherited methods

export() public method

Stores log messages to MongoDB collection.

public void export ( )

                public function export()
{
    $rows = [];
    foreach ($this->messages as $message) {
        list($text, $level, $category, $timestamp) = $message;
        if (!is_string($text)) {
            // exceptions may not be serializable if in the call stack somewhere is a Closure
            if ($text instanceof \Throwable || $text instanceof \Exception) {
                $text = (string) $text;
            } else {
                $text = VarDumper::export($text);
            }
        }
        $rows[] = [
            'level' => $level,
            'category' => $category,
            'log_time' => $timestamp,
            'prefix' => $this->getMessagePrefix($message),
            'message' => $text,
        ];
    }
    $this->db->getCollection($this->logCollection)->batchInsert($rows);
}

            
init() public method

Initializes the MongoDbTarget component.

This method will initialize the $db property to make sure it refers to a valid MongoDB connection.

public void init ( )
throws \yii\base\InvalidConfigException

if $db is invalid.

                public function init()
{
    parent::init();
    $this->db = Instance::ensure($this->db, Connection::className());
}