diff --git a/apps/bootstrap/protected/config/main.php b/apps/bootstrap/protected/config/main.php index f19dead..a1e3678 100644 --- a/apps/bootstrap/protected/config/main.php +++ b/apps/bootstrap/protected/config/main.php @@ -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', +// ) ), ), ), diff --git a/yii/assets/yii.debug.js b/yii/assets/yii.debug.js index 4e32d89..e0d30f6 100644 --- a/yii/assets/yii.debug.js +++ b/yii/assets/yii.debug.js @@ -18,7 +18,7 @@ yii.debug = (function ($) { //dataType: 'json', success: function(data) { var $e = $('#' + id); - $e.html(data); + $e.html(data).show(); } }); } diff --git a/yii/debug/Toolbar.php b/yii/debug/Toolbar.php index 84b55c8..591f0f6 100644 --- a/yii/debug/Toolbar.php +++ b/yii/debug/Toolbar.php @@ -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, diff --git a/yii/debug/controllers/DefaultController.php b/yii/debug/controllers/DefaultController.php index ca90920..4d686ee 100644 --- a/yii/debug/controllers/DefaultController.php +++ b/yii/debug/controllers/DefaultController.php @@ -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'."; + } + } } \ No newline at end of file diff --git a/yii/debug/views/default/toolbar.php b/yii/debug/views/default/toolbar.php new file mode 100644 index 0000000..0b08d4b --- /dev/null +++ b/yii/debug/views/default/toolbar.php @@ -0,0 +1,39 @@ + + +
+ + + + + + + + + diff --git a/yii/logging/DebugTarget.php b/yii/logging/DebugTarget.php index 1bc4771..92a74d6 100644 --- a/yii/logging/DebugTarget.php +++ b/yii/logging/DebugTarget.php @@ -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; + } + } } } }