LogPanel.php 2.04 KB
Newer Older
Qiang Xue committed
1 2 3 4 5 6 7 8 9 10 11
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yii\debug\panels;

use Yii;
use yii\debug\Panel;
Qiang Xue committed
12
use yii\log\Logger;
Mark committed
13
use yii\debug\models\search\Log;
Qiang Xue committed
14 15

/**
16 17
 * Debugger panel that collects and displays logs.
 *
Qiang Xue committed
18 19 20 21 22
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class LogPanel extends Panel
{
Mark committed
23 24 25
	/**
	 * @var array log messages extracted to array as models, to use with data provider.
	 */
26
	private $_models;
Mark committed
27

28 29 30
	/**
	 * @inheritdoc
	 */
Qiang Xue committed
31 32 33 34 35
	public function getName()
	{
		return 'Logs';
	}

36 37 38
	/**
	 * @inheritdoc
	 */
Qiang Xue committed
39 40
	public function getSummary()
	{
41
		return Yii::$app->view->render('panels/log/summary', ['data' => $this->data, 'panel' => $this]);
Qiang Xue committed
42 43
	}

44 45 46
	/**
	 * @inheritdoc
	 */
Qiang Xue committed
47 48
	public function getDetail()
	{
Mark committed
49
		$searchModel = new Log();
50
		$dataProvider = $searchModel->search(Yii::$app->request->getQueryParams(), $this->getModels());
Qiang Xue committed
51

52
		return Yii::$app->view->render('panels/log/detail', [
Tobias Munk committed
53
			'dataProvider' => $dataProvider,
Mark committed
54 55 56
			'panel' => $this,
			'searchModel' => $searchModel,
		]);
Qiang Xue committed
57 58
	}

59 60 61
	/**
	 * @inheritdoc
	 */
Qiang Xue committed
62 63
	public function save()
	{
Qiang Xue committed
64 65
		$target = $this->module->logTarget;
		$messages = $target->filterMessages($target->messages, Logger::LEVEL_ERROR | Logger::LEVEL_INFO | Logger::LEVEL_WARNING | Logger::LEVEL_TRACE);
Alexander Makarov committed
66
		return ['messages' => $messages];
Qiang Xue committed
67
	}
Mark committed
68 69

	/**
70 71 72 73
	 * Returns an array of models that represents logs of the current request.
	 * Can be used with data providers, such as \yii\data\ArrayDataProvider.
	 *
	 * @param boolean $refresh if need to build models from log messages and refresh them.
Mark committed
74 75
	 * @return array models
	 */
76
	protected function getModels($refresh = false)
Mark committed
77 78 79 80
	{
		if ($this->_models === null || $refresh) {
			$this->_models = [];

Tobias Munk committed
81
			foreach ($this->data['messages'] as $message) {
Mark committed
82 83 84 85
				$this->_models[] = 	[
					'message' => $message[0],
					'level' => $message[1],
					'category' => $message[2],
86
					'time' => ($message[3] * 1000), // time in milliseconds
Mark committed
87 88 89 90 91 92
					'trace' => $message[4]
				];
			}
		}
		return $this->_models;
	}
Qiang Xue committed
93
}