FileTarget.php 3.02 KB
Newer Older
w  
Qiang Xue committed
1 2
<?php
/**
w  
Qiang Xue committed
3
 * FileTarget class file.
w  
Qiang Xue committed
4 5 6
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @link http://www.yiiframework.com/
w  
Qiang Xue committed
7
 * @copyright Copyright &copy; 2008-2012 Yii Software LLC
w  
Qiang Xue committed
8 9 10
 * @license http://www.yiiframework.com/license/
 */

w  
Qiang Xue committed
11 12
namespace yii\logging;

w  
Qiang Xue committed
13
/**
w  
Qiang Xue committed
14
 * FileTarget records log messages in files.
w  
Qiang Xue committed
15
 *
w  
Qiang Xue committed
16 17 18 19 20 21 22
 * The log files are stored under [[logPath]] and their name
 * is specified by [[logFile]]. If the size of the log file exceeds
 * [[maxFileSize]] (in kilo-bytes), a rotation will be performed,
 * which renames the current log file by suffixing the file name
 * with '.1'. All existing log files are moved backwards one place,
 * i.e., '.2' to '.3', '.1' to '.2', and so on. The property
 * [[maxLogFiles]] specifies how many files to keep.
w  
Qiang Xue committed
23 24
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
w  
Qiang Xue committed
25
 * @since 2.0
w  
Qiang Xue committed
26
 */
w  
Qiang Xue committed
27
class FileTarget extends Target
w  
Qiang Xue committed
28 29
{
	/**
w  
Qiang Xue committed
30
	 * @var integer maximum log file size, in kilo-bytes. Defaults to 1024, meaning 1MB.
w  
Qiang Xue committed
31
	 */
w  
Qiang Xue committed
32
	public $maxFileSize = 1024; // in KB
w  
Qiang Xue committed
33
	/**
w  
Qiang Xue committed
34
	 * @var integer number of log files used for rotation. Defaults to 5.
w  
Qiang Xue committed
35
	 */
w  
Qiang Xue committed
36
	public $maxLogFiles = 5;
w  
Qiang Xue committed
37
	/**
w  
Qiang Xue committed
38
	 * @var string directory storing log files. Defaults to the application runtime path.
w  
Qiang Xue committed
39
	 */
w  
Qiang Xue committed
40
	public $logPath;
w  
Qiang Xue committed
41
	/**
w  
Qiang Xue committed
42
	 * @var string log file name. Defaults to 'application.log'.
w  
Qiang Xue committed
43
	 */
w  
Qiang Xue committed
44
	public $logFile = 'application.log';
w  
Qiang Xue committed
45 46 47 48 49 50 51 52 53


	/**
	 * Initializes the route.
	 * This method is invoked after the route is created by the route manager.
	 */
	public function init()
	{
		parent::init();
w  
Qiang Xue committed
54
		if ($this->logPath === null) {
Qiang Xue committed
55
			$this->logPath = \Yii::$application->getRuntimePath();
w  
Qiang Xue committed
56 57 58 59 60 61 62 63 64 65
		}
		if (!is_dir($this->logPath) || !is_writable($this->logPath)) {
			throw new \yii\base\Exception("Directory '{$this->logPath}' does not exist or is not writable.");
		}
		if ($this->maxLogFiles < 1) {
			$this->maxLogFiles = 1;
		}
		if ($this->maxFileSize < 1) {
			$this->maxFileSize = 1;
		}
w  
Qiang Xue committed
66 67 68
	}

	/**
w  
Qiang Xue committed
69 70
	 * Sends log [[messages]] to specified email addresses.
	 * @param boolean $final whether this method is called at the end of the current application
w  
Qiang Xue committed
71
	 */
w  
Qiang Xue committed
72
	public function exportMessages($final)
w  
Qiang Xue committed
73
	{
w  
Qiang Xue committed
74 75
		$logFile = $this->logPath . DIRECTORY_SEPARATOR . $this->logFile;
		if (@filesize($logFile) > $this->maxFileSize * 1024) {
w  
Qiang Xue committed
76
			$this->rotateFiles();
w  
Qiang Xue committed
77 78 79 80 81 82 83 84
		}
		$messages = array();
		foreach ($this->messages as $message) {
			$messages[] = $this->formatMessage($message);
		}
		@file_put_contents($logFile, implode('', $messages), FILE_APPEND | LOCK_EX);

		$this->messages = array();
w  
Qiang Xue committed
85 86 87 88 89 90 91
	}

	/**
	 * Rotates log files.
	 */
	protected function rotateFiles()
	{
w  
Qiang Xue committed
92 93
		$file = $this->logPath . DIRECTORY_SEPARATOR . $this->logFile;
		for ($i = $this->maxLogFiles; $i > 0; --$i) {
w  
Qiang Xue committed
94
			$rotateFile = $file . '.' . $i;
w  
Qiang Xue committed
95
			if (is_file($rotateFile)) {
w  
Qiang Xue committed
96
				// suppress errors because it's possible multiple processes enter into this section
w  
Qiang Xue committed
97
				if ($i === $this->maxLogFiles) {
w  
Qiang Xue committed
98
					@unlink($rotateFile);
Qiang Xue committed
99
				} else {
w  
Qiang Xue committed
100
					@rename($rotateFile, $file . '.' . ($i + 1));
w  
Qiang Xue committed
101
				}
w  
Qiang Xue committed
102 103
			}
		}
w  
Qiang Xue committed
104
		if (is_file($file)) {
w  
Qiang Xue committed
105
			@rename($file, $file . '.1'); // suppress errors because it's possible multiple processes enter into this section
w  
Qiang Xue committed
106
		}
w  
Qiang Xue committed
107 108
	}
}