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

Qiang Xue committed
8
namespace yii\debug;
Qiang Xue committed
9 10

use Yii;
Qiang Xue committed
11
use yii\log\Target;
Qiang Xue committed
12 13

/**
14 15
 * The debug LogTarget is used to store logs for later use in the debugger tool
 *
Qiang Xue committed
16 17 18
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
Qiang Xue committed
19
class LogTarget extends Target
Qiang Xue committed
20
{
Qiang Xue committed
21 22 23 24
	/**
	 * @var Module
	 */
	public $module;
Qiang Xue committed
25
	public $tag;
Qiang Xue committed
26

Qiang Xue committed
27 28 29 30
	/**
	 * @param \yii\debug\Module $module
	 * @param array $config
	 */
Alexander Makarov committed
31
	public function __construct($module, $config = [])
Qiang Xue committed
32 33 34
	{
		parent::__construct($config);
		$this->module = $module;
Qiang Xue committed
35
		$this->tag = uniqid();
Qiang Xue committed
36 37
	}

Qiang Xue committed
38 39 40 41
	/**
	 * Exports log messages to a specific destination.
	 * Child classes must implement this method.
	 */
Qiang Xue committed
42
	public function export()
Qiang Xue committed
43
	{
Qiang Xue committed
44
		$path = $this->module->dataPath;
Qiang Xue committed
45 46 47
		if (!is_dir($path)) {
			mkdir($path);
		}
Qiang Xue committed
48
		$indexFile = "$path/index.data";
Qiang Xue committed
49
		if (!is_file($indexFile)) {
Alexander Makarov committed
50
			$manifest = [];
Qiang Xue committed
51
		} else {
52
			$manifest = unserialize(file_get_contents($indexFile));
Qiang Xue committed
53 54
		}
		$request = Yii::$app->getRequest();
55
		$response = Yii::$app->getResponse();
Alexander Makarov committed
56
		$manifest[$this->tag] = $summary = [
Qiang Xue committed
57
			'tag' => $this->tag,
Qiang Xue committed
58 59 60 61 62
			'url' => $request->getAbsoluteUrl(),
			'ajax' => $request->getIsAjax(),
			'method' => $request->getMethod(),
			'ip' => $request->getUserIP(),
			'time' => time(),
63 64
			'statusCode' => $response->statusCode,
			'sqlCount' => $this->getSqlTotalCount(),
Alexander Makarov committed
65
		];
Qiang Xue committed
66
		$this->gc($manifest);
Qiang Xue committed
67

Qiang Xue committed
68
		$dataFile = "$path/{$this->tag}.data";
Alexander Makarov committed
69
		$data = [];
Qiang Xue committed
70 71
		foreach ($this->module->panels as $id => $panel) {
			$data[$id] = $panel->save();
Qiang Xue committed
72
		}
Qiang Xue committed
73
		$data['summary'] = $summary;
74 75
		file_put_contents($dataFile, serialize($data));
		file_put_contents($indexFile, serialize($manifest));
Qiang Xue committed
76 77 78 79 80 81 82 83 84 85 86 87
	}

	/**
	 * Processes the given log messages.
	 * This method will filter the given messages with [[levels]] and [[categories]].
	 * And if requested, it will also export the filtering result to specific medium (e.g. email).
	 * @param array $messages log messages to be processed. See [[Logger::messages]] for the structure
	 * of each message.
	 * @param boolean $final whether this method is called at the end of the current application
	 */
	public function collect($messages, $final)
	{
Qiang Xue committed
88
		$this->messages = array_merge($this->messages, $messages);
Qiang Xue committed
89
		if ($final) {
Qiang Xue committed
90 91 92
			$this->export($this->messages);
		}
	}
Qiang Xue committed
93 94 95

	protected function gc(&$manifest)
	{
Qiang Xue committed
96
		if (count($manifest) > $this->module->historySize + 10) {
Qiang Xue committed
97 98
			$n = count($manifest) - $this->module->historySize;
			foreach (array_keys($manifest) as $tag) {
Qiang Xue committed
99
				$file = $this->module->dataPath . "/$tag.data";
Qiang Xue committed
100 101 102 103 104 105 106 107
				@unlink($file);
				unset($manifest[$tag]);
				if (--$n <= 0) {
					break;
				}
			}
		}
	}
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124

	/**
	 * Returns total sql count executed in current request. If database panel is not configured
	 * returns 0.
	 * @return integer
	 */
	protected function getSqlTotalCount()
	{
		if (!isset($this->module->panels['db'])) {
			return 0;
		}
		$profileLogs = $this->module->panels['db']->save();
		
		# / 2 because messages are in couple (begin/end)
		return count($profileLogs['messages']) / 2;
	}

Qiang Xue committed
125
}