LogTarget.php 4.06 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;
11
use yii\base\InvalidConfigException;
Qiang Xue committed
12
use yii\log\Target;
Qiang Xue committed
13 14

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

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

Qiang Xue committed
39 40 41 42
	/**
	 * Exports log messages to a specific destination.
	 * Child classes must implement this method.
	 */
Qiang Xue committed
43
	public function export()
Qiang Xue committed
44
	{
Qiang Xue committed
45
		$path = $this->module->dataPath;
Qiang Xue committed
46 47 48
		if (!is_dir($path)) {
			mkdir($path);
		}
49

Mark committed
50
		$summary = $this->collectSummary();
Qiang Xue committed
51
		$dataFile = "$path/{$this->tag}.data";
Alexander Makarov committed
52
		$data = [];
Qiang Xue committed
53 54
		foreach ($this->module->panels as $id => $panel) {
			$data[$id] = $panel->save();
Qiang Xue committed
55
		}
Qiang Xue committed
56
		$data['summary'] = $summary;
57
		file_put_contents($dataFile, serialize($data));
58 59 60 61 62

		$indexFile = "$path/index.data";
		$this->updateIndexFile($indexFile, $summary);
	}

63 64 65 66 67 68 69
	/**
	 * Updates index file with summary log data
	 *
	 * @param string $indexFile path to index file
	 * @param array $summary summary log data
	 * @throws \yii\base\InvalidConfigException
	 */
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
	private function updateIndexFile($indexFile, $summary)
	{
		touch($indexFile);
		if (($fp = @fopen($indexFile, 'r+')) === false) {
			throw new InvalidConfigException("Unable to open debug data index file: $indexFile");
		}
		@flock($fp, LOCK_EX);
		$manifest = '';
		while (($buffer = fgets($fp)) !== false) {
			$manifest .= $buffer;
		}
		if (!feof($fp) || empty($manifest)) {
			// error while reading index data, ignore and create new
			$manifest = [];
		} else {
			$manifest = unserialize($manifest);
		}

		$manifest[$this->tag] = $summary;
		$this->gc($manifest);

		ftruncate($fp, 0);
		rewind($fp);
		fwrite($fp, serialize($manifest));

		@flock($fp, LOCK_UN);
		@fclose($fp);
Qiang Xue committed
97 98 99 100 101 102 103 104 105 106 107 108
	}

	/**
	 * 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
109
		$this->messages = array_merge($this->messages, $messages);
Qiang Xue committed
110
		if ($final) {
Qiang Xue committed
111 112 113
			$this->export($this->messages);
		}
	}
Qiang Xue committed
114 115 116

	protected function gc(&$manifest)
	{
Qiang Xue committed
117
		if (count($manifest) > $this->module->historySize + 10) {
Qiang Xue committed
118 119
			$n = count($manifest) - $this->module->historySize;
			foreach (array_keys($manifest) as $tag) {
Qiang Xue committed
120
				$file = $this->module->dataPath . "/$tag.data";
Qiang Xue committed
121 122 123 124 125 126 127 128
				@unlink($file);
				unset($manifest[$tag]);
				if (--$n <= 0) {
					break;
				}
			}
		}
	}
129

Mark committed
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
	/**
	 * Collects summary data of current request.
	 * @return array
	 */
	protected function collectSummary()
	{
		$request = Yii::$app->getRequest();
		$response = Yii::$app->getResponse();
		$summary = [
			'tag' => $this->tag,
			'url' => $request->getAbsoluteUrl(),
			'ajax' => $request->getIsAjax(),
			'method' => $request->getMethod(),
			'ip' => $request->getUserIP(),
			'time' => time(),
			'statusCode' => $response->statusCode,
			'sqlCount' => $this->getSqlTotalCount(),
		];

		if (isset($this->module->panels['mail'])) {
			$summary['mailCount'] = count($this->module->panels['mail']->getMessages());
		}

		return $summary;
	}

156 157 158 159 160 161 162 163 164 165
	/**
	 * 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;
		}
Mark committed
166
		$profileLogs = $this->module->panels['db']->getProfileLogs();
167 168
		
		# / 2 because messages are in couple (begin/end)
Mark committed
169
		return count($profileLogs) / 2;
170
	}
Mark committed
171

Qiang Xue committed
172
}