Commit f1316131 by Qiang Xue

debug toolbar WIP

parent 909bbe62
...@@ -23,6 +23,16 @@ class DebugTarget extends Target ...@@ -23,6 +23,16 @@ class DebugTarget extends Target
*/ */
public function export($messages) public function export($messages)
{ {
$path = Yii::$app->getRuntimePath() . '/debug';
if (!is_dir($path)) {
mkdir($path);
}
$file = $path . '/' . Yii::getLogger()->getTag() . '.log';
$data = array(
'messages' => $messages,
'globals' => $GLOBALS,
);
file_put_contents($file, json_encode($data));
} }
/** /**
...@@ -36,13 +46,8 @@ class DebugTarget extends Target ...@@ -36,13 +46,8 @@ class DebugTarget extends Target
public function collect($messages, $final) public function collect($messages, $final)
{ {
$this->messages = array_merge($this->messages, $this->filterMessages($messages)); $this->messages = array_merge($this->messages, $this->filterMessages($messages));
$count = count($this->messages); if ($final) {
if ($count > 0 && ($final || $this->exportInterval > 0 && $count >= $this->exportInterval)) {
if (($context = $this->getContextMessage()) !== '') {
$this->messages[] = array($context, Logger::LEVEL_INFO, 'application', YII_BEGIN_TIME);
}
$this->export($this->messages); $this->export($this->messages);
$this->messages = array();
} }
} }
} }
...@@ -82,11 +82,7 @@ class Logger extends Component ...@@ -82,11 +82,7 @@ class Logger extends Component
* @var Router the log target router registered with this logger. * @var Router the log target router registered with this logger.
*/ */
public $router; public $router;
/**
* @var string a tag that uniquely identifies the current request. This can be used
* to differentiate the log messages for different requests.
*/
public $tag;
/** /**
* Initializes the logger by registering [[flush()]] as a shutdown function. * Initializes the logger by registering [[flush()]] as a shutdown function.
...@@ -94,7 +90,6 @@ class Logger extends Component ...@@ -94,7 +90,6 @@ class Logger extends Component
public function init() public function init()
{ {
parent::init(); parent::init();
$this->tag = date('Ymd-His', microtime(true));
register_shutdown_function(array($this, 'flush'), true); register_shutdown_function(array($this, 'flush'), true);
} }
...@@ -143,6 +138,25 @@ class Logger extends Component ...@@ -143,6 +138,25 @@ class Logger extends Component
} }
/** /**
* @return string a tag that uniquely identifies the current request.
*/
public function getTag()
{
if ($this->_tag === null) {
$this->_tag = date('Ymd-His', microtime(true));
}
return $this->_tag;
}
/**
* @param string $tag a tag that uniquely identifies the current request.
*/
public function setTag($tag)
{
$this->_tag = $tag;
}
/**
* Returns the total elapsed time since the start of the current request. * Returns the total elapsed time since the start of the current request.
* This method calculates the difference between now and the timestamp * This method calculates the difference between now and the timestamp
* defined by constant `YII_BEGIN_TIME` which is evaluated at the beginning * defined by constant `YII_BEGIN_TIME` which is evaluated at the beginning
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
namespace yii\logging; namespace yii\logging;
use Yii;
use yii\base\Component; use yii\base\Component;
use yii\base\InvalidConfigException; use yii\base\InvalidConfigException;
...@@ -109,8 +110,9 @@ abstract class Target extends Component ...@@ -109,8 +110,9 @@ abstract class Target extends Component
protected function getContextMessage() protected function getContextMessage()
{ {
$context = array(); $context = array();
if ($this->logUser && ($user = \Yii::$app->getComponent('user', false)) !== null) { if ($this->logUser && ($user = Yii::$app->getComponent('user', false)) !== null) {
$context[] = 'User: ' . $user->getName() . ' (ID: ' . $user->getId() . ')'; /** @var $user \yii\web\User */
$context[] = 'User: ' . $user->getId();
} }
foreach ($this->logVars as $name) { foreach ($this->logVars as $name) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment