From f13161319a1887c189a5f836f7d9d0c13e60d6b9 Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Wed, 15 May 2013 08:40:14 -0400 Subject: [PATCH] debug toolbar WIP --- yii/logging/DebugTarget.php | 17 +++++++++++------ yii/logging/Logger.php | 26 ++++++++++++++++++++------ yii/logging/Target.php | 6 ++++-- 3 files changed, 35 insertions(+), 14 deletions(-) diff --git a/yii/logging/DebugTarget.php b/yii/logging/DebugTarget.php index 3392d47..1bc4771 100644 --- a/yii/logging/DebugTarget.php +++ b/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(); } } } diff --git a/yii/logging/Logger.php b/yii/logging/Logger.php index b557c5e..7a9a6af 100644 --- a/yii/logging/Logger.php +++ b/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 diff --git a/yii/logging/Target.php b/yii/logging/Target.php index 45e784a..fac8b53 100644 --- a/yii/logging/Target.php +++ b/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) {