LogPanel.php 2.89 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
 * Debugger panel that collects and displays logs.
 *
Qiang Xue committed
19 20 21 22 23 24 25 26 27 28 29 30
 * @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
31 32
		$output = array('<span class="label">' . count($this->data['messages']) . '</span>');
		$title = 'Logged ' . count($this->data['messages']) . ' messages';
Qiang Xue committed
33
		$errorCount = count(Target::filterMessages($this->data['messages'], Logger::LEVEL_ERROR));
34
		if ($errorCount) {
Qiang Xue committed
35 36
			$output[] = '<span class="label label-important">' . $errorCount . '</span>';
			$title .= ", $errorCount errors";
Qiang Xue committed
37 38
		}
		$warningCount = count(Target::filterMessages($this->data['messages'], Logger::LEVEL_WARNING));
39
		if ($warningCount) {
Qiang Xue committed
40 41
			$output[] = '<span class="label label-warning">' . $warningCount . '</span>';
			$title .= ", $warningCount warnings";
Qiang Xue committed
42
		}
Qiang Xue committed
43 44 45
		$log = implode('&nbsp;', $output);
		$url = $this->getUrl();
		return <<<EOD
Qiang Xue committed
46
<div class="yii-debug-toolbar-block">
47
	<a href="$url" title="$title">Log $log</a>
Qiang Xue committed
48 49 50 51 52 53
</div>
EOD;
	}

	public function getDetail()
	{
Qiang Xue committed
54 55
		$rows = array();
		foreach ($this->data['messages'] as $log) {
56 57
			list ($message, $level, $category, $time, $traces) = $log;
			$time = date('H:i:s.', $time) . sprintf('%03d', (int)(($time - (int)$time) * 1000));
58
			$message = nl2br(Html::encode($message));
59 60 61 62 63 64 65 66 67
			if (!empty($traces)) {
				$message .= Html::ul($traces, array(
					'class' => 'trace',
					'item' => function ($trace) {
						return "<li>{$trace['file']}({$trace['line']})</li>";
					},
				));
			}
			if ($level == Logger::LEVEL_ERROR) {
68
				$class = ' class="danger"';
69
			} elseif ($level == Logger::LEVEL_WARNING) {
Qiang Xue committed
70
				$class = ' class="warning"';
71
			} elseif ($level == Logger::LEVEL_INFO) {
72
				$class = ' class="success"';
Qiang Xue committed
73 74 75
			} else {
				$class = '';
			}
76
			$level = Logger::getLevelName($level);
Qiang Xue committed
77
			$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>";
Qiang Xue committed
78 79 80
		}
		$rows = implode("\n", $rows);
		return <<<EOD
Qiang Xue committed
81 82
<h1>Log Messages</h1>

Qiang Xue committed
83 84 85 86
<table class="table table-condensed table-bordered table-striped table-hover" style="table-layout: fixed;">
<thead>
<tr>
	<th style="width: 100px;">Time</th>
87
	<th style="width: 65px;">Level</th>
Qiang Xue committed
88 89 90 91 92 93 94 95 96
	<th style="width: 250px;">Category</th>
	<th>Message</th>
</tr>
</thead>
<tbody>
$rows
</tbody>
</table>
EOD;
Qiang Xue committed
97 98 99 100
	}

	public function save()
	{
Qiang Xue committed
101 102
		$target = $this->module->logTarget;
		$messages = $target->filterMessages($target->messages, Logger::LEVEL_ERROR | Logger::LEVEL_INFO | Logger::LEVEL_WARNING | Logger::LEVEL_TRACE);
Qiang Xue committed
103
		return array(
Qiang Xue committed
104
			'messages' => $messages,
Qiang Xue committed
105 106 107
		);
	}
}