Browse Source

debug toolbar WIP

tags/2.0.0-beta
Qiang Xue 12 years ago
parent
commit
f13161319a
  1. 17
      yii/logging/DebugTarget.php
  2. 26
      yii/logging/Logger.php
  3. 6
      yii/logging/Target.php

17
yii/logging/DebugTarget.php

@ -23,6 +23,16 @@ class DebugTarget extends Target
*/
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
public function collect($messages, $final)
{
$this->messages = array_merge($this->messages, $this->filterMessages($messages));
$count = count($this->messages);
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);
}
if ($final) {
$this->export($this->messages);
$this->messages = array();
}
}
}

26
yii/logging/Logger.php

@ -82,11 +82,7 @@ class Logger extends Component
* @var Router the log target router registered with this logger.
*/
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.
@ -94,7 +90,6 @@ class Logger extends Component
public function init()
{
parent::init();
$this->tag = date('Ymd-His', microtime(true));
register_shutdown_function(array($this, 'flush'), true);
}
@ -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.
* This method calculates the difference between now and the timestamp
* defined by constant `YII_BEGIN_TIME` which is evaluated at the beginning

6
yii/logging/Target.php

@ -7,6 +7,7 @@
namespace yii\logging;
use Yii;
use yii\base\Component;
use yii\base\InvalidConfigException;
@ -109,8 +110,9 @@ abstract class Target extends Component
protected function getContextMessage()
{
$context = array();
if ($this->logUser && ($user = \Yii::$app->getComponent('user', false)) !== null) {
$context[] = 'User: ' . $user->getName() . ' (ID: ' . $user->getId() . ')';
if ($this->logUser && ($user = Yii::$app->getComponent('user', false)) !== null) {
/** @var $user \yii\web\User */
$context[] = 'User: ' . $user->getId();
}
foreach ($this->logVars as $name) {

Loading…
Cancel
Save