Browse Source

Merge pull request #1693 from Ragazzo/debug_log_panel_improvements

debug module log panel improved
tags/2.0.0-beta
Qiang Xue 11 years ago
parent
commit
38cfdb9bd3
  1. 37
      extensions/yii/debug/models/search/Base.php
  2. 29
      extensions/yii/debug/models/search/Debug.php
  3. 79
      extensions/yii/debug/models/search/Log.php
  4. 103
      extensions/yii/debug/panels/LogPanel.php
  5. 73
      extensions/yii/debug/views/default/panels/log/detail.php
  6. 28
      extensions/yii/debug/views/default/panels/log/summary.php
  7. 2
      extensions/yii/debug/views/default/view.php

37
extensions/yii/debug/models/search/Base.php

@ -0,0 +1,37 @@
<?php
namespace yii\debug\models\search;
use yii\base\Model;
use yii\debug\components\search\Filter;
use yii\debug\components\search\matches;
class Base extends Model
{
/**
* @param Filter $filter
* @param string $attribute
* @param boolean $partial
*/
public function addCondition($filter, $attribute, $partial = false)
{
$value = $this->$attribute;
if (mb_strpos($value, '>') !== false) {
$value = intval(str_replace('>', '', $value));
$filter->addMatch($attribute, new matches\Greater(['value' => $value]));
} elseif (mb_strpos($value, '<') !== false) {
$value = intval(str_replace('<', '', $value));
$filter->addMatch($attribute, new matches\Lower(['value' => $value]));
} else {
$filter->addMatch($attribute, new matches\Exact(['value' => $value, 'partial' => $partial]));
}
}
}

29
extensions/yii/debug/models/search/Debug.php

@ -2,15 +2,13 @@
namespace yii\debug\models\search;
use yii\base\Model;
use yii\data\ArrayDataProvider;
use yii\debug\components\search\Filter;
use yii\debug\components\search\matches;
/**
* Debug represents the model behind the search form about requests manifest data.
*/
class Debug extends Model
class Debug extends Base
{
/**
* @var string tag attribute input search value
@ -121,29 +119,4 @@ class Debug extends Model
return in_array($code, $this->criticalCodes);
}
/**
* @param Filter $filter
* @param string $attribute
* @param boolean $partial
*/
public function addCondition($filter, $attribute, $partial = false)
{
$value = $this->$attribute;
if (mb_strpos($value, '>') !== false) {
$value = intval(str_replace('>', '', $value));
$filter->addMatch($attribute, new matches\Greater(['value' => $value]));
} elseif (mb_strpos($value, '<') !== false) {
$value = intval(str_replace('<', '', $value));
$filter->addMatch($attribute, new matches\Lower(['value' => $value]));
} else {
$filter->addMatch($attribute, new matches\Exact(['value' => $value, 'partial' => $partial]));
}
}
}

79
extensions/yii/debug/models/search/Log.php

@ -0,0 +1,79 @@
<?php
namespace yii\debug\models\search;
use yii\data\ArrayDataProvider;
use yii\debug\components\search\Filter;
/**
* Log represents the model behind the search form about current request log.
*/
class Log extends Base
{
/**
* @var string ip attribute input search value
*/
public $level;
/**
* @var string method attribute input search value
*/
public $category;
/**
* @var integer ajax attribute input search value
*/
public $message;
public function rules()
{
return [
[['level', 'message', 'category'], 'safe'],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'level' => 'Level',
'category' => 'Category',
'message' => 'Message',
];
}
/**
* Returns data provider with filled models. Filter applied if needed.
* @param array $params
* @param array $models
* @return \yii\data\ArrayDataProvider
*/
public function search($params, $models)
{
$dataProvider = new ArrayDataProvider([
'allModels' => $models,
'pagination' => [
'pageSize' => 10,
],
'sort' => [
'attributes' => ['time','level','category','message'],
],
]);
if (!($this->load($params) && $this->validate())) {
return $dataProvider;
}
$filter = new Filter();
$this->addCondition($filter, 'level');
$this->addCondition($filter, 'category', true);
$this->addCondition($filter, 'message', true);
$dataProvider->allModels = $filter->filter($models);
return $dataProvider;
}
}

103
extensions/yii/debug/panels/LogPanel.php

@ -9,9 +9,8 @@ namespace yii\debug\panels;
use Yii;
use yii\debug\Panel;
use yii\helpers\Html;
use yii\log\Logger;
use yii\log\Target;
use yii\debug\models\search\Log;
/**
* Debugger panel that collects and displays logs.
@ -21,6 +20,12 @@ use yii\log\Target;
*/
class LogPanel extends Panel
{
/**
* @var array log messages extracted to array as models, to use with data provider.
*/
private $_models ;
public function getName()
{
return 'Logs';
@ -28,72 +33,19 @@ class LogPanel extends Panel
public function getSummary()
{
$output = ['<span class="label">' . count($this->data['messages']) . '</span>'];
$title = 'Logged ' . count($this->data['messages']) . ' messages';
$errorCount = count(Target::filterMessages($this->data['messages'], Logger::LEVEL_ERROR));
if ($errorCount) {
$output[] = '<span class="label label-important">' . $errorCount . '</span>';
$title .= ", $errorCount errors";
}
$warningCount = count(Target::filterMessages($this->data['messages'], Logger::LEVEL_WARNING));
if ($warningCount) {
$output[] = '<span class="label label-warning">' . $warningCount . '</span>';
$title .= ", $warningCount warnings";
}
$log = implode('&nbsp;', $output);
$url = $this->getUrl();
return <<<EOD
<div class="yii-debug-toolbar-block">
<a href="$url" title="$title">Log $log</a>
</div>
EOD;
return Yii::$app->view->render('panels/log/summary',['data' => $this->data, 'panel' => $this]);
}
public function getDetail()
{
$rows = [];
foreach ($this->data['messages'] as $log) {
list ($message, $level, $category, $time, $traces) = $log;
$time = date('H:i:s.', $time) . sprintf('%03d', (int)(($time - (int)$time) * 1000));
$message = nl2br(Html::encode($message));
if (!empty($traces)) {
$message .= Html::ul($traces, [
'class' => 'trace',
'item' => function ($trace) {
return "<li>{$trace['file']}({$trace['line']})</li>";
},
]);
}
if ($level == Logger::LEVEL_ERROR) {
$class = ' class="danger"';
} elseif ($level == Logger::LEVEL_WARNING) {
$class = ' class="warning"';
} elseif ($level == Logger::LEVEL_INFO) {
$class = ' class="success"';
} else {
$class = '';
}
$level = Logger::getLevelName($level);
$rows[] = "<tr$class><td style=\"width: 100px;\">$time</td><td style=\"width: 100px;\">$level</td><td style=\"width: 250px;\">$category</td><td><div>$message</div></td></tr>";
}
$rows = implode("\n", $rows);
return <<<EOD
<h1>Log Messages</h1>
$searchModel = new Log();
$dataProvider = $searchModel->search($_GET, $this->getModels());
<table class="table table-condensed table-bordered table-striped table-hover" style="table-layout: fixed;">
<thead>
<tr>
<th style="width: 100px;">Time</th>
<th style="width: 65px;">Level</th>
<th style="width: 250px;">Category</th>
<th>Message</th>
</tr>
</thead>
<tbody>
$rows
</tbody>
</table>
EOD;
return Yii::$app->view->render('panels/log/detail',[
'dataProvider' => $dataProvider,
'panel' => $this,
'searchModel' => $searchModel,
]);
}
public function save()
@ -102,4 +54,29 @@ EOD;
$messages = $target->filterMessages($target->messages, Logger::LEVEL_ERROR | Logger::LEVEL_INFO | Logger::LEVEL_WARNING | Logger::LEVEL_TRACE);
return ['messages' => $messages];
}
/**
* Returns array of models that represents logs of the current request. Can be used with data providers,
* like yii\data\ArrayDataProvider.
* @param boolean $refresh if needed to build models from log messages and refresh them.
* @return array models
*/
protected function getModels($refresh=false)
{
if ($this->_models === null || $refresh) {
$this->_models = [];
foreach($this->data['messages'] as $message) {
$this->_models[] = [
'message' => $message[0],
'level' => $message[1],
'category' => $message[2],
'time' => ($message[3] * 1000), #time in milliseconds
'trace' => $message[4]
];
}
}
return $this->_models;
}
}

73
extensions/yii/debug/views/default/panels/log/detail.php

@ -0,0 +1,73 @@
<?php
use yii\helpers\Html;
use yii\grid\GridView;
use yii\data\ArrayDataProvider;
use yii\log\Logger;
?>
<h1>Log Messages</h1>
<?php
echo GridView::widget([
'dataProvider' => $dataProvider,
'id' => 'log-panel-detailed-grid',
'filterModel' => $searchModel,
'filterUrl' => $panel->getUrl(),
'rowOptions' => function ($model, $key, $index, $grid){
switch($model['level']) {
case Logger::LEVEL_ERROR : return ['class' => 'danger'];
case Logger::LEVEL_WARNING : return ['class' => 'warning'];
case Logger::LEVEL_INFO : return ['class' => 'success'];
default: return [];
}
},
'columns' => [
['class' => 'yii\grid\SerialColumn'],
[
'attribute' => 'time',
'value' => function ($data)
{
$timeInSeconds = $data['time'] / 1000;
$millisecondsDiff = (int)(($timeInSeconds - (int)$timeInSeconds) * 1000);
return date('H:i:s.',$timeInSeconds) . sprintf('%03d',$millisecondsDiff);
},
],
[
'attribute' => 'level',
'value' => function ($data)
{
return Logger::getLevelName($data['level']);
},
'filter' => [
Logger::LEVEL_TRACE => ' Trace ',
Logger::LEVEL_PROFILE => ' Profile ',
Logger::LEVEL_INFO => ' Info ',
Logger::LEVEL_ERROR => ' Error ',
],
],
'category',
[
'attribute' => 'message',
'value' => function ($data)
{
$message = nl2br(Html::encode($data['message']));
if (!empty($data['trace'])) {
$message .= Html::ul($data['trace'], [
'class' => 'trace',
'item' => function ($trace)
{
return "<li>{$trace['file']} ({$trace['line']})</li>";
}
]);
};
return $message;
},
'format' => 'html',
'options' => [
'width' => '50%',
],
],
],
]);
?>

28
extensions/yii/debug/views/default/panels/log/summary.php

@ -0,0 +1,28 @@
<?php
use yii\log\Target;
use yii\log\Logger;
?>
<?php
$title = 'Logged ' . count($data['messages']) . ' messages';
$errorCount = count(Target::filterMessages($data['messages'], Logger::LEVEL_ERROR));
$warningCount = count(Target::filterMessages($data['messages'], Logger::LEVEL_WARNING));
$output = [];
if ($errorCount) {
$output[] = "<span class=\"label label-important\">$errorCount</span>";
$title .= ", $errorCount errors";
}
if ($warningCount) {
$output[] = "<span class=\"label label-warning\">$warningCount</span>";
$title .= ", $warningCount warnings";
}
?>
<div class="yii-debug-toolbar-block">
<a href="<?php echo $panel->getUrl(); ?>" title="<?php echo $title ?>">Log
<span class="label"><?php echo count($data['messages']); ?></span>
<?php echo implode('&nbsp;', $output); ?>
</a>
</div>

2
extensions/yii/debug/views/default/view.php

@ -18,7 +18,7 @@ $this->title = 'Yii Debugger';
<div class="default-view">
<div id="yii-debug-toolbar">
<div class="yii-debug-toolbar-block title">
Yii Debugger
<?php echo Html::a('Yii Debugger', ['index'],['title' => 'Back to main debug page']);?>
</div>
<?php foreach ($panels as $panel): ?>
<?= $panel->getSummary() ?>

Loading…
Cancel
Save