mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-17 06:48:59 +08:00
debug tool bar WIP
This commit is contained in:
@@ -5,9 +5,9 @@ return array(
|
||||
'basePath' => dirname(__DIR__),
|
||||
'preload' => array('log'),
|
||||
'modules' => array(
|
||||
'debug' => array(
|
||||
'class' => 'yii\debug\Module',
|
||||
)
|
||||
// 'debug' => array(
|
||||
// 'class' => 'yii\debug\Module',
|
||||
// )
|
||||
),
|
||||
'components' => array(
|
||||
'cache' => array(
|
||||
@@ -23,10 +23,13 @@ return array(
|
||||
'log' => array(
|
||||
'class' => 'yii\logging\Router',
|
||||
'targets' => array(
|
||||
'file' => array(
|
||||
array(
|
||||
'class' => 'yii\logging\FileTarget',
|
||||
'levels' => array('error', 'warning'),
|
||||
),
|
||||
// array(
|
||||
// 'class' => 'yii\logging\DebugTarget',
|
||||
// )
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
@@ -18,7 +18,7 @@ yii.debug = (function ($) {
|
||||
//dataType: 'json',
|
||||
success: function(data) {
|
||||
var $e = $('#' + id);
|
||||
$e.html(data);
|
||||
$e.html(data).show();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -17,12 +17,11 @@ use yii\helpers\Html;
|
||||
*/
|
||||
class Toolbar extends Widget
|
||||
{
|
||||
public $debugAction = 'debug';
|
||||
public $enabled = YII_DEBUG;
|
||||
public $debugAction = 'debug/default/toolbar';
|
||||
|
||||
public function run()
|
||||
{
|
||||
if ($this->enabled) {
|
||||
if (Yii::$app->hasModule('debug')) {
|
||||
$id = 'yii-debug-toolbar';
|
||||
$url = Yii::$app->getUrlManager()->createUrl($this->debugAction, array(
|
||||
'tag' => Yii::getLogger()->tag,
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
|
||||
namespace yii\debug\controllers;
|
||||
|
||||
use Yii;
|
||||
use yii\web\Controller;
|
||||
|
||||
/**
|
||||
@@ -19,4 +20,15 @@ class DefaultController extends Controller
|
||||
{
|
||||
echo $tag;
|
||||
}
|
||||
|
||||
public function actionToolbar($tag)
|
||||
{
|
||||
$file = Yii::$app->getRuntimePath() . "/debug/$tag.log";
|
||||
if (preg_match('/^[\w\-]+$/', $tag) && is_file($file)) {
|
||||
$data = json_decode(file_get_contents($file), true);
|
||||
echo $this->renderPartial('toolbar', $data);
|
||||
} else {
|
||||
echo "Unable to find debug data tagged with '$tag'.";
|
||||
}
|
||||
}
|
||||
}
|
||||
39
yii/debug/views/default/toolbar.php
Normal file
39
yii/debug/views/default/toolbar.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php use yii\helpers\Html;
|
||||
|
||||
echo Html::style("
|
||||
#yii-debug-toolbar {
|
||||
position: fixed;
|
||||
left: 0;
|
||||
right: 0;
|
||||
bottom: 0;
|
||||
background-color: #eee;
|
||||
border-top: 1px solid #ccc;
|
||||
margin: 0;
|
||||
padding: 5px 10px;
|
||||
z-index: 1000000;
|
||||
font: 11px Verdana, Arial, sans-serif;
|
||||
text-align: left;
|
||||
}
|
||||
.yii-debug-toolbar-block {
|
||||
float: left;
|
||||
margin: 0 10px;
|
||||
");
|
||||
?>
|
||||
|
||||
<div class="yii-debug-toolbar-block">
|
||||
</div>
|
||||
|
||||
<div class="yii-debug-toolbar-block">
|
||||
Peak memory: <?php echo sprintf('%.2fMB', $memory / 1048576); ?>
|
||||
</div>
|
||||
|
||||
<div class="yii-debug-toolbar-block">
|
||||
Time spent: <?php echo sprintf('%.3fs', $time); ?>
|
||||
</div>
|
||||
|
||||
<div class="yii-debug-toolbar-block">
|
||||
</div>
|
||||
|
||||
<div class="yii-debug-toolbar-block">
|
||||
</div>
|
||||
|
||||
@@ -15,6 +15,8 @@ use Yii;
|
||||
*/
|
||||
class DebugTarget extends Target
|
||||
{
|
||||
public $maxLogFiles = 20;
|
||||
|
||||
/**
|
||||
* Exports log messages to a specific destination.
|
||||
* Child classes must implement this method.
|
||||
@@ -30,7 +32,14 @@ class DebugTarget extends Target
|
||||
$file = $path . '/' . Yii::getLogger()->getTag() . '.log';
|
||||
$data = array(
|
||||
'messages' => $messages,
|
||||
'globals' => $GLOBALS,
|
||||
'_SERVER' => $_SERVER,
|
||||
'_GET' => $_GET,
|
||||
'_POST' => $_POST,
|
||||
'_COOKIE' => $_COOKIE,
|
||||
'_FILES' => empty($_FILES) ? array() : $_FILES,
|
||||
'_SESSION' => empty($_SESSION) ? array() : $_SESSION,
|
||||
'memory' => memory_get_peak_usage(),
|
||||
'time' => microtime(true) - YII_BEGIN_TIME,
|
||||
);
|
||||
file_put_contents($file, json_encode($data));
|
||||
}
|
||||
@@ -45,9 +54,38 @@ class DebugTarget extends Target
|
||||
*/
|
||||
public function collect($messages, $final)
|
||||
{
|
||||
if (Yii::$app->getModule('debug', false) !== null) {
|
||||
return;
|
||||
}
|
||||
$this->messages = array_merge($this->messages, $this->filterMessages($messages));
|
||||
if ($final) {
|
||||
$this->export($this->messages);
|
||||
$this->gc();
|
||||
}
|
||||
}
|
||||
|
||||
protected function gc()
|
||||
{
|
||||
if (mt_rand(0, 10000) > 100) {
|
||||
return;
|
||||
}
|
||||
$iterator = new \DirectoryIterator(Yii::$app->getRuntimePath() . '/debug');
|
||||
$files = array();
|
||||
foreach ($iterator as $file) {
|
||||
if (preg_match('/^[\d\-]+\.log$/', $file->getFileName()) && $file->isFile()) {
|
||||
$files[] = $file->getPathname();
|
||||
}
|
||||
}
|
||||
sort($files);
|
||||
if (count($files) > $this->maxLogFiles) {
|
||||
$n = count($files) - $this->maxLogFiles;
|
||||
foreach ($files as $i => $file) {
|
||||
if ($i < $n) {
|
||||
unlink($file);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user