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

w  
Qiang Xue committed
10 11
namespace yii\logging;

w  
Qiang Xue committed
12
/**
w  
Qiang Xue committed
13
 * FileTarget records log messages in files.
w  
Qiang Xue committed
14
 *
w  
Qiang Xue committed
15 16 17 18 19 20 21
 * 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
22 23
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
w  
Qiang Xue committed
24
 * @since 2.0
w  
Qiang Xue committed
25
 */
w  
Qiang Xue committed
26
class FileTarget extends Target
w  
Qiang Xue committed
27 28
{
	/**
w  
Qiang Xue committed
29
	 * @var integer maximum log file size, in kilo-bytes. Defaults to 1024, meaning 1MB.
w  
Qiang Xue committed
30
	 */
w  
Qiang Xue committed
31
	public $maxFileSize = 1024; // in KB
w  
Qiang Xue committed
32
	/**
w  
Qiang Xue committed
33
	 * @var integer number of log files used for rotation. Defaults to 5.
w  
Qiang Xue committed
34
	 */
w  
Qiang Xue committed
35
	public $maxLogFiles = 5;
w  
Qiang Xue committed
36
	/**
w  
Qiang Xue committed
37
	 * @var string directory storing log files. Defaults to the application runtime path.
w  
Qiang Xue committed
38
	 */
w  
Qiang Xue committed
39
	public $logPath;
w  
Qiang Xue committed
40
	/**
w  
Qiang Xue committed
41
	 * @var string log file name. Defaults to 'application.log'.
w  
Qiang Xue committed
42
	 */
w  
Qiang Xue committed
43
	public $logFile = 'application.log';
w  
Qiang Xue committed
44 45 46 47 48 49 50 51 52


	/**
	 * 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
53
		if ($this->logPath === null) {
Qiang Xue committed
54
			$this->logPath = \Yii::$application->getRuntimePath();
w  
Qiang Xue committed
55 56 57 58 59 60 61 62 63 64
		}
		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
65 66 67
	}

	/**
w  
Qiang Xue committed
68 69
	 * 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
70
	 */
w  
Qiang Xue committed
71
	public function exportMessages($final)
w  
Qiang Xue committed
72
	{
w  
Qiang Xue committed
73 74
		$logFile = $this->logPath . DIRECTORY_SEPARATOR . $this->logFile;
		if (@filesize($logFile) > $this->maxFileSize * 1024) {
w  
Qiang Xue committed
75
			$this->rotateFiles();
w  
Qiang Xue committed
76 77 78 79 80 81
		}
		$messages = array();
		foreach ($this->messages as $message) {
			$messages[] = $this->formatMessage($message);
		}
		@file_put_contents($logFile, implode('', $messages), FILE_APPEND | LOCK_EX);
w  
Qiang Xue committed
82 83 84 85 86 87 88
	}

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