Class yii\debug\LogTarget

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

The debug LogTarget is used to store logs for later use in the debugger tool

Public Methods

Hide inherited methods

Method Description Defined By
__construct() yii\debug\LogTarget
collect() Processes the given log messages. yii\debug\LogTarget
export() Exports log messages to a specific destination. yii\debug\LogTarget
loadManifest() yii\debug\LogTarget
loadTagToPanels() yii\debug\LogTarget

Protected Methods

Hide inherited methods

Method Description Defined By
collectSummary() Collects summary data of current request. yii\debug\LogTarget
gc() Removes obsolete data files yii\debug\LogTarget
getExcessiveDbCallersCount() Get the number of excessive Database caller(s). yii\debug\LogTarget
getSqlTotalCount() Returns total sql count executed in current request. If database panel is not configured returns 0. yii\debug\LogTarget
removeStaleDataFiles() Remove staled data files i.e. files that are not in the current index file (may happen because of corrupted or rotated index file) yii\debug\LogTarget

Property Details

Hide inherited properties

$module public property
public yii\debug\Module $module null
$tag public property
public string $tag null

Method Details

Hide inherited methods

__construct() public method

public void __construct ( $module, $config = [] )
$module yii\debug\Module
$config array

                public function __construct($module, $config = [])
{
    parent::__construct($config);
    $this->module = $module;
    $this->tag = uniqid();
}

            
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).

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

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

$final boolean

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

throws \yii\base\Exception

                public function collect($messages, $final)
{
    $this->messages = array_merge($this->messages, $messages);
    if ($final) {
        $this->export();
    }
}

            
collectSummary() protected method

Collects summary data of current request.

protected array collectSummary ( )

                protected function collectSummary()
{
    if (Yii::$app === null) {
        return [];
    }
    $request = Yii::$app->getRequest();
    $response = Yii::$app->getResponse();
    $summary = [
        'tag' => $this->tag,
        'url' => $request instanceof yii\console\Request ? "php yii " . implode(' ', $request->getParams()): $request->getAbsoluteUrl(),
        'ajax' => $request instanceof yii\console\Request ? 0 : (int) $request->getIsAjax(),
        'method' => $request instanceof yii\console\Request ? 'COMMAND' : $request->getMethod(),
        'ip' => $request instanceof yii\console\Request ? exec('whoami') : $request->getUserIP(),
        'time' => $_SERVER['REQUEST_TIME_FLOAT'],
        'statusCode' => $response instanceof yii\console\Response ? $response->exitStatus : $response->statusCode,
        'sqlCount' => $this->getSqlTotalCount(),
        'excessiveCallersCount' => $this->getExcessiveDbCallersCount(),
    ];
    if (isset($this->module->panels['mail'])) {
        $mailFiles = $this->module->panels['mail']->getMessagesFileName();
        $summary['mailCount'] = count($mailFiles);
        $summary['mailFiles'] = $mailFiles;
    }
    return $summary;
}

            
export() public method

Exports log messages to a specific destination.

Child classes must implement this method.

public void export ( )
throws \yii\base\Exception

                public function export()
{
    $path = $this->module->dataPath;
    FileHelper::createDirectory($path, $this->module->dirMode);
    $summary = $this->collectSummary();
    $dataFile = "$path/{$this->tag}.data";
    $data = [];
    $exceptions = [];
    foreach ($this->module->panels as $id => $panel) {
        try {
            $panelData = $panel->save();
            if ($id === 'profiling') {
                $summary['peakMemory'] = $panelData['memory'];
                $summary['processingTime'] = $panelData['time'];
            }
            $data[$id] = serialize($panelData);
        } catch (\Exception $exception) {
            $exceptions[$id] = new FlattenException($exception);
        }
    }
    $data['summary'] = $summary;
    $data['exceptions'] = $exceptions;
    file_put_contents($dataFile, serialize($data));
    if ($this->module->fileMode !== null) {
        @chmod($dataFile, $this->module->fileMode);
    }
    $indexFile = "$path/index.data";
    $this->updateIndexFile($indexFile, $summary);
}

            
gc() protected method

Removes obsolete data files

protected void gc ( &$manifest )
$manifest array

                protected function gc(&$manifest)
{
    if (count($manifest) > $this->module->historySize + 10) {
        $n = count($manifest) - $this->module->historySize;
        foreach (array_keys($manifest) as $tag) {
            $file = $this->module->dataPath . "/$tag.data";
            @unlink($file);
            if (isset($manifest[$tag]['mailFiles'])) {
                foreach ($manifest[$tag]['mailFiles'] as $mailFile) {
                    @unlink(Yii::getAlias($this->module->panels['mail']->mailPath) . "/$mailFile");
                }
            }
            unset($manifest[$tag]);
            if (--$n <= 0) {
                break;
            }
        }
        $this->removeStaleDataFiles($manifest);
    }
}

            
getExcessiveDbCallersCount() protected method (available since version 2.1.23)

Get the number of excessive Database caller(s).

protected integer getExcessiveDbCallersCount ( )

                protected function getExcessiveDbCallersCount()
{
    if (!isset($this->module->panels['db'])) {
        return 0;
    }
    /** @var DbPanel $dbPanel */
    $dbPanel = $this->module->panels['db'];
    return $dbPanel->getExcessiveCallersCount();
}

            
getSqlTotalCount() protected method

Returns total sql count executed in current request. If database panel is not configured returns 0.

protected integer getSqlTotalCount ( )

                protected function getSqlTotalCount()
{
    if (!isset($this->module->panels['db'])) {
        return 0;
    }
    $profileLogs = $this->module->panels['db']->getProfileLogs();
    # / 2 because messages are in couple (begin/end)
    return count($profileLogs) / 2;
}

            
loadManifest() public method

See also \yii\debug\DefaultController.

public array loadManifest ( )

                public function loadManifest()
{
    $indexFile = $this->module->dataPath . '/index.data';
    $content = '';
    $fp = @fopen($indexFile, 'r');
    if ($fp !== false) {
        @flock($fp, LOCK_SH);
        $content = fread($fp, filesize($indexFile));
        @flock($fp, LOCK_UN);
        fclose($fp);
    }
    if ($content !== '') {
        return array_reverse(unserialize($content), true);
    }
    return [];
}

            
loadTagToPanels() public method

See also \yii\debug\DefaultController.

public array loadTagToPanels ( $tag )
$tag

                public function loadTagToPanels($tag)
{
    $dataFile = $this->module->dataPath . "/$tag.data";
    $data = unserialize(file_get_contents($dataFile));
    $exceptions = $data['exceptions'];
    foreach ($this->module->panels as $id => $panel) {
        if (isset($data[$id])) {
            $panel->tag = $tag;
            $panel->load(unserialize($data[$id]));
        } else {
            unset($this->module->panels[$id]);
        }
        if (isset($exceptions[$id])) {
            $panel->setError($exceptions[$id]);
        }
    }
    return $data;
}

            
removeStaleDataFiles() protected method (available since version 2.0.11)

Remove staled data files i.e. files that are not in the current index file (may happen because of corrupted or rotated index file)

protected void removeStaleDataFiles ( $manifest )
$manifest array

                protected function removeStaleDataFiles($manifest)
{
    $storageTags = array_map(
        function ($file) {
            return pathinfo($file, PATHINFO_FILENAME);
        },
        FileHelper::findFiles($this->module->dataPath, ['except' => ['index.data']])
    );
    $staledTags = array_diff($storageTags, array_keys($manifest));
    foreach ($staledTags as $tag) {
        @unlink($this->module->dataPath . "/$tag.data");
    }
}