LogTarget.php 2.34 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 16

/**
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
Qiang Xue committed
17
class LogTarget extends Target
Qiang Xue committed
18
{
Qiang Xue committed
19 20
	public $maxLogFiles = 20;

Qiang Xue committed
21 22 23 24 25 26 27 28
	/**
	 * Exports log messages to a specific destination.
	 * Child classes must implement this method.
	 * @param array $messages the messages to be exported. See [[Logger::messages]] for the structure
	 * of each message.
	 */
	public function export($messages)
	{
Qiang Xue committed
29 30 31 32
		$path = Yii::$app->getRuntimePath() . '/debug';
		if (!is_dir($path)) {
			mkdir($path);
		}
Qiang Xue committed
33
		$file = $path . '/' . Yii::$app->getLog()->getTag() . '.log';
Qiang Xue committed
34 35
		$data = array(
			'messages' => $messages,
Qiang Xue committed
36 37 38 39 40 41 42 43
			'_SERVER' => $_SERVER,
			'_GET' => $_GET,
			'_POST' => $_POST,
			'_COOKIE' => $_COOKIE,
			'_FILES' => empty($_FILES) ? array() : $_FILES,
			'_SESSION' => empty($_SESSION) ? array() : $_SESSION,
			'memory' => memory_get_peak_usage(),
			'time' => microtime(true) - YII_BEGIN_TIME,
Qiang Xue committed
44 45
		);
		file_put_contents($file, json_encode($data));
Qiang Xue committed
46 47 48 49 50 51 52 53 54 55 56 57 58
	}

	/**
	 * 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)
	{
		$this->messages = array_merge($this->messages, $this->filterMessages($messages));
Qiang Xue committed
59
		if ($final) {
Qiang Xue committed
60
			$this->export($this->messages);
Qiang Xue committed
61 62 63 64 65 66 67 68 69 70 71 72
			$this->gc();
		}
	}

	protected function gc()
	{
		if (mt_rand(0, 10000) > 100) {
			return;
		}
		$iterator = new \DirectoryIterator(Yii::$app->getRuntimePath() . '/debug');
		$files = array();
		foreach ($iterator as $file) {
Qiang Xue committed
73
			/** @var \DirectoryIterator $file */
Qiang Xue committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87
			if (preg_match('/^[\d\-]+\.log$/', $file->getFileName()) && $file->isFile()) {
				$files[] = $file->getPathname();
			}
		}
		sort($files);
		if (count($files) > $this->maxLogFiles) {
			$n = count($files) - $this->maxLogFiles;
			foreach ($files as $i => $file) {
				if ($i < $n) {
					unlink($file);
				} else {
					break;
				}
			}
Qiang Xue committed
88 89 90
		}
	}
}