Qiang Xue
11 years ago
7 changed files with 259 additions and 92 deletions
@ -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])); |
||||
} |
||||
|
||||
} |
||||
|
||||
} |
@ -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; |
||||
} |
||||
|
||||
} |
@ -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%', |
||||
], |
||||
], |
||||
], |
||||
]); |
||||
?> |
@ -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(' ', $output); ?> |
||||
</a> |
||||
</div> |
Loading…
Reference in new issue