You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

126 lines
3.0 KiB

12 years ago
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
12 years ago
namespace yii\debug;
12 years ago
use Yii;
use yii\log\Target;
12 years ago
/**
* The debug LogTarget is used to store logs for later use in the debugger tool
*
12 years ago
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
12 years ago
class LogTarget extends Target
12 years ago
{
12 years ago
/**
* @var Module
*/
public $module;
public $tag;
12 years ago
/**
* @param \yii\debug\Module $module
* @param array $config
*/
public function __construct($module, $config = [])
12 years ago
{
parent::__construct($config);
$this->module = $module;
12 years ago
$this->tag = uniqid();
12 years ago
}
12 years ago
/**
* Exports log messages to a specific destination.
* Child classes must implement this method.
*/
12 years ago
public function export()
12 years ago
{
12 years ago
$path = $this->module->dataPath;
12 years ago
if (!is_dir($path)) {
mkdir($path);
}
11 years ago
$indexFile = "$path/index.data";
12 years ago
if (!is_file($indexFile)) {
$manifest = [];
12 years ago
} else {
$manifest = unserialize(file_get_contents($indexFile));
12 years ago
}
$request = Yii::$app->getRequest();
$response = Yii::$app->getResponse();
$manifest[$this->tag] = $summary = [
12 years ago
'tag' => $this->tag,
12 years ago
'url' => $request->getAbsoluteUrl(),
'ajax' => $request->getIsAjax(),
'method' => $request->getMethod(),
'ip' => $request->getUserIP(),
'time' => time(),
'statusCode' => $response->statusCode,
'sqlCount' => $this->getSqlTotalCount(),
];
$this->gc($manifest);
12 years ago
11 years ago
$dataFile = "$path/{$this->tag}.data";
$data = [];
12 years ago
foreach ($this->module->panels as $id => $panel) {
$data[$id] = $panel->save();
12 years ago
}
12 years ago
$data['summary'] = $summary;
file_put_contents($dataFile, serialize($data));
file_put_contents($indexFile, serialize($manifest));
12 years ago
}
/**
* 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).
* @param array $messages log messages to be processed. See [[Logger::messages]] for the structure
* of each message.
* @param boolean $final whether this method is called at the end of the current application
*/
public function collect($messages, $final)
{
12 years ago
$this->messages = array_merge($this->messages, $messages);
12 years ago
if ($final) {
12 years ago
$this->export($this->messages);
}
}
protected function gc(&$manifest)
{
12 years ago
if (count($manifest) > $this->module->historySize + 10) {
$n = count($manifest) - $this->module->historySize;
foreach (array_keys($manifest) as $tag) {
11 years ago
$file = $this->module->dataPath . "/$tag.data";
@unlink($file);
unset($manifest[$tag]);
if (--$n <= 0) {
break;
}
}
}
}
/**
* Returns total sql count executed in current request. If database panel is not configured
* returns 0.
* @return integer
*/
protected function getSqlTotalCount()
{
if (!isset($this->module->panels['db'])) {
return 0;
}
$profileLogs = $this->module->panels['db']->save();
# / 2 because messages are in couple (begin/end)
return count($profileLogs['messages']) / 2;
}
12 years ago
}