Commit 38cfdb9b by Qiang Xue

Merge pull request #1693 from Ragazzo/debug_log_panel_improvements

debug module log panel improved
parents 5da46c0c 7c70f5c3
<?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]));
}
}
}
...@@ -2,15 +2,13 @@ ...@@ -2,15 +2,13 @@
namespace yii\debug\models\search; namespace yii\debug\models\search;
use yii\base\Model;
use yii\data\ArrayDataProvider; use yii\data\ArrayDataProvider;
use yii\debug\components\search\Filter; use yii\debug\components\search\Filter;
use yii\debug\components\search\matches;
/** /**
* Debug represents the model behind the search form about requests manifest data. * 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 * @var string tag attribute input search value
...@@ -121,29 +119,4 @@ class Debug extends Model ...@@ -121,29 +119,4 @@ class Debug extends Model
return in_array($code, $this->criticalCodes); 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]));
}
}
} }
<?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;
}
}
...@@ -9,9 +9,8 @@ namespace yii\debug\panels; ...@@ -9,9 +9,8 @@ namespace yii\debug\panels;
use Yii; use Yii;
use yii\debug\Panel; use yii\debug\Panel;
use yii\helpers\Html;
use yii\log\Logger; use yii\log\Logger;
use yii\log\Target; use yii\debug\models\search\Log;
/** /**
* Debugger panel that collects and displays logs. * Debugger panel that collects and displays logs.
...@@ -21,6 +20,12 @@ use yii\log\Target; ...@@ -21,6 +20,12 @@ use yii\log\Target;
*/ */
class LogPanel extends Panel class LogPanel extends Panel
{ {
/**
* @var array log messages extracted to array as models, to use with data provider.
*/
private $_models ;
public function getName() public function getName()
{ {
return 'Logs'; return 'Logs';
...@@ -28,72 +33,19 @@ class LogPanel extends Panel ...@@ -28,72 +33,19 @@ class LogPanel extends Panel
public function getSummary() public function getSummary()
{ {
$output = ['<span class="label">' . count($this->data['messages']) . '</span>']; return Yii::$app->view->render('panels/log/summary',['data' => $this->data, 'panel' => $this]);
$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;
} }
public function getDetail() public function getDetail()
{ {
$rows = []; $searchModel = new Log();
foreach ($this->data['messages'] as $log) { $dataProvider = $searchModel->search($_GET, $this->getModels());
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>
<table class="table table-condensed table-bordered table-striped table-hover" style="table-layout: fixed;"> return Yii::$app->view->render('panels/log/detail',[
<thead> 'dataProvider' => $dataProvider,
<tr> 'panel' => $this,
<th style="width: 100px;">Time</th> 'searchModel' => $searchModel,
<th style="width: 65px;">Level</th> ]);
<th style="width: 250px;">Category</th>
<th>Message</th>
</tr>
</thead>
<tbody>
$rows
</tbody>
</table>
EOD;
} }
public function save() public function save()
...@@ -102,4 +54,29 @@ EOD; ...@@ -102,4 +54,29 @@ EOD;
$messages = $target->filterMessages($target->messages, Logger::LEVEL_ERROR | Logger::LEVEL_INFO | Logger::LEVEL_WARNING | Logger::LEVEL_TRACE); $messages = $target->filterMessages($target->messages, Logger::LEVEL_ERROR | Logger::LEVEL_INFO | Logger::LEVEL_WARNING | Logger::LEVEL_TRACE);
return ['messages' => $messages]; 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;
}
} }
<?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%',
],
],
],
]);
?>
\ No newline at end of file
<?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>
\ No newline at end of file
...@@ -18,7 +18,7 @@ $this->title = 'Yii Debugger'; ...@@ -18,7 +18,7 @@ $this->title = 'Yii Debugger';
<div class="default-view"> <div class="default-view">
<div id="yii-debug-toolbar"> <div id="yii-debug-toolbar">
<div class="yii-debug-toolbar-block title"> <div class="yii-debug-toolbar-block title">
Yii Debugger <?php echo Html::a('Yii Debugger', ['index'],['title' => 'Back to main debug page']);?>
</div> </div>
<?php foreach ($panels as $panel): ?> <?php foreach ($panels as $panel): ?>
<?= $panel->getSummary() ?> <?= $panel->getSummary() ?>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment