LogTarget.php 2.52 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();
Alexander Makarov committed
55
		$manifest[$this->tag] = $summary = [
Qiang Xue committed
56
			'tag' => $this->tag,
Qiang Xue committed
57 58 59 60 61
			'url' => $request->getAbsoluteUrl(),
			'ajax' => $request->getIsAjax(),
			'method' => $request->getMethod(),
			'ip' => $request->getUserIP(),
			'time' => time(),
Alexander Makarov committed
62
		];
Qiang Xue committed
63
		$this->gc($manifest);
Qiang Xue committed
64

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

	/**
	 * 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
85
		$this->messages = array_merge($this->messages, $messages);
Qiang Xue committed
86
		if ($final) {
Qiang Xue committed
87 88 89
			$this->export($this->messages);
		}
	}
Qiang Xue committed
90 91 92

	protected function gc(&$manifest)
	{
Qiang Xue committed
93
		if (count($manifest) > $this->module->historySize + 10) {
Qiang Xue committed
94 95
			$n = count($manifest) - $this->module->historySize;
			foreach (array_keys($manifest) as $tag) {
Qiang Xue committed
96
				$file = $this->module->dataPath . "/$tag.data";
Qiang Xue committed
97 98 99 100 101 102 103 104
				@unlink($file);
				unset($manifest[$tag]);
				if (--$n <= 0) {
					break;
				}
			}
		}
	}
Qiang Xue committed
105
}