Class yii\elasticsearch\ElasticsearchTarget

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

ElasticsearchTarget stores log messages in a Elasticsearch index.

Public Properties

Hide inherited properties

Property Type Description Defined By
$_contextMessage string Context message cache (can be used multiple times if context is appended to every message) yii\elasticsearch\ElasticsearchTarget
$cacheContext boolean If true, context message will cached once it's been created. yii\elasticsearch\ElasticsearchTarget
$db yii\elasticsearch\Connection|array|string The Elasticsearch connection object or the application component ID of the Elasticsearch connection. yii\elasticsearch\ElasticsearchTarget
$includeContext boolean If true, context will be included in every message. yii\elasticsearch\ElasticsearchTarget
$index string Elasticsearch index name. yii\elasticsearch\ElasticsearchTarget
$logContext boolean If true, context will be logged as a separate message after all other messages. yii\elasticsearch\ElasticsearchTarget
$options array URL options. yii\elasticsearch\ElasticsearchTarget
$type string Elasticsearch type name. yii\elasticsearch\ElasticsearchTarget

Protected Properties

Hide inherited properties

Property Type Description Defined By

Public Methods

Hide inherited methods

Method Description Defined By
collect() Processes the given log messages. yii\elasticsearch\ElasticsearchTarget
export() yii\elasticsearch\ElasticsearchTarget
init() This method will initialize the elasticsearch property to make sure it refers to a valid Elasticsearch connection. yii\elasticsearch\ElasticsearchTarget
prepareMessage() Prepares a log message. yii\elasticsearch\ElasticsearchTarget

Protected Methods

Hide inherited methods

Method Description Defined By
getContextMessage() If $includeContext property is false, returns context message normally. yii\elasticsearch\ElasticsearchTarget

Property Details

Hide inherited properties

$_contextMessage protected property

Context message cache (can be used multiple times if context is appended to every message)

protected string $_contextMessage null
$cacheContext public property

If true, context message will cached once it's been created. Makes sense to use with $includeContext.

public boolean $cacheContext false
$db public property

The Elasticsearch connection object or the application component ID of the Elasticsearch connection.

public yii\elasticsearch\Connection|array|string $db 'elasticsearch'
$includeContext public property

If true, context will be included in every message. This is convenient if you log application errors and analyze them with tools like Kibana.

public boolean $includeContext false
$index public property

Elasticsearch index name.

public string $index 'yii'
$logContext public property

If true, context will be logged as a separate message after all other messages.

public boolean $logContext true
$options public property

URL options.

public array $options = []
$type public property

Elasticsearch type name.

public string $type 'log'

Method Details

Hide inherited methods

collect() public method

Processes the given log messages.

This method will filter the given messages with levels and categories. And if requested, it will also export the filtering result to specific medium (e.g. email). Depending on the $includeContext attribute, a context message will be either created or ignored.

public void collect ( $messages, $final )
$messages array

Log messages to be processed. See Logger::messages for the structure of each message.

$final boolean

Whether this method is called at the end of the current application

                public function collect($messages, $final)
{
    $this->messages = array_merge($this->messages, static::filterMessages($messages, $this->getLevels(), $this->categories, $this->except));
    $count = count($this->messages);
    if ($count > 0 && ($final || $this->exportInterval > 0 && $count >= $this->exportInterval)) {
        if (!$this->includeContext && $this->logContext) {
            $context = $this->getContextMessage();
            if (!empty($context)) {
                $this->messages[] = [$context, Logger::LEVEL_INFO, 'application', YII_BEGIN_TIME];
            }
        }
        // set exportInterval to 0 to avoid triggering export again while exporting
        $oldExportInterval = $this->exportInterval;
        $this->exportInterval = 0;
        $this->export();
        $this->exportInterval = $oldExportInterval;
        $this->messages = [];
    }
}

            
export() public method

public void export ( )

                public function export()
{
    $messages = array_map([$this, 'prepareMessage'], $this->messages);
    $body = implode("\n", $messages) . "\n";
    if ($this->db->dslVersion >= 7) {
        $this->db->post([$this->index, '_bulk'], $this->options, $body);
    } else {
        $this->db->post([$this->index, $this->type, '_bulk'], $this->options, $body);
    }
}

            
getContextMessage() protected method

If $includeContext property is false, returns context message normally.

If $includeContext is true, returns an empty string (so that context message in collect() is not generated), expecting that context will be appended to every message in prepareMessage().

protected array getContextMessage ( )
return array

The context information

                protected function getContextMessage()
{
    if (null === $this->_contextMessage || !$this->cacheContext) {
        $this->_contextMessage = ArrayHelper::filter($GLOBALS, $this->logVars);
    }
    return $this->_contextMessage;
}

            
init() public method

This method will initialize the elasticsearch property to make sure it refers to a valid Elasticsearch connection.

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

if elasticsearch is invalid.

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

            
prepareMessage() public method

Prepares a log message.

public string prepareMessage ( $message )
$message array

The log message to be formatted.

                public function prepareMessage($message)
{
    list($text, $level, $category, $timestamp) = $message;
    $result = [
        'category' => $category,
        'level' => Logger::getLevelName($level),
        '@timestamp' => date('c', $timestamp),
    ];
    if (isset($message[4])) {
        $result['trace'] = $message[4];
    }
    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);
        }
    }
    $result['message'] = $text;
    if ($this->includeContext) {
        $result['context'] = $this->getContextMessage();
    }
    $message = implode("\n", [
        Json::encode([
            'index' => new \stdClass()
        ]),
        Json::encode($result)
    ]);
    return $message;
}