LogPanel.php 2.44 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 13
use yii\helpers\Html;
use yii\log\Logger;
Qiang Xue committed
14
use yii\log\Target;
Qiang Xue committed
15 16 17 18 19 20 21 22 23 24 25 26 27 28

/**
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class LogPanel extends Panel
{
	public function getName()
	{
		return 'Logs';
	}

	public function getSummary()
	{
Qiang Xue committed
29 30
		$output = array();
		$errorCount = count(Target::filterMessages($this->data['messages'], Logger::LEVEL_ERROR));
31
		if ($errorCount) {
Qiang Xue committed
32
			$output[] = '<span class="label label-important">' . $errorCount . '</span> ' . ($errorCount > 1 ? 'errors' : 'error');
Qiang Xue committed
33 34
		}
		$warningCount = count(Target::filterMessages($this->data['messages'], Logger::LEVEL_WARNING));
35
		if ($warningCount) {
Qiang Xue committed
36
			$output[] = '<span class="label label-warning">' . $warningCount . '</span> ' . ($warningCount > 1 ? 'warnings' : 'warning');
Qiang Xue committed
37 38 39 40
		}
		if (!empty($output)) {
			$log = implode(', ', $output);
			return <<<EOD
Qiang Xue committed
41
<div class="yii-debug-toolbar-block">
Qiang Xue committed
42
$log
Qiang Xue committed
43 44
</div>
EOD;
Qiang Xue committed
45 46 47
		} else {
			return '';
		}
Qiang Xue committed
48 49 50 51
	}

	public function getDetail()
	{
Qiang Xue committed
52 53 54 55 56
		$rows = array();
		foreach ($this->data['messages'] as $log) {
			$time = date('H:i:s.', $log[3]) . sprintf('%03d', (int)(($log[3] - (int)$log[3]) * 1000));
			$level = Logger::getLevelName($log[1]);
			$message = Html::encode(wordwrap($log[0]));
Qiang Xue committed
57 58 59 60 61 62 63 64 65 66
			if ($log[1] == Logger::LEVEL_ERROR) {
				$class = ' class="error"';
			} elseif ($log[1] == Logger::LEVEL_WARNING) {
				$class = ' class="warning"';
			} elseif ($log[1] == Logger::LEVEL_INFO) {
				$class = ' class="info"';
			} else {
				$class = '';
			}
			$rows[] = "<tr$class><td style=\"width: 100px;\">$time</td><td style=\"width: 100px;\">$level</td><td style=\"width: 250px;\">{$log[2]}</td><td>$message</td></tr>";
Qiang Xue committed
67 68 69
		}
		$rows = implode("\n", $rows);
		return <<<EOD
Qiang Xue committed
70 71
<h1>Log Messages</h1>

Qiang Xue committed
72 73 74 75 76 77 78 79 80 81 82 83 84 85
<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: 100px;">Level</th>
	<th style="width: 250px;">Category</th>
	<th>Message</th>
</tr>
</thead>
<tbody>
$rows
</tbody>
</table>
EOD;
Qiang Xue committed
86 87 88 89
	}

	public function save()
	{
Qiang Xue committed
90 91
		$target = $this->module->logTarget;
		$messages = $target->filterMessages($target->messages, Logger::LEVEL_ERROR | Logger::LEVEL_INFO | Logger::LEVEL_WARNING | Logger::LEVEL_TRACE);
Qiang Xue committed
92
		return array(
Qiang Xue committed
93
			'messages' => $messages,
Qiang Xue committed
94 95 96
		);
	}
}