FileTarget.php 3.68 KB
Newer Older
w  
Qiang Xue committed
1 2 3
<?php
/**
 * @link http://www.yiiframework.com/
Qiang Xue committed
4
 * @copyright Copyright (c) 2008 Yii Software LLC
w  
Qiang Xue committed
5 6 7
 * @license http://www.yiiframework.com/license/
 */

Qiang Xue committed
8
namespace yii\log;
Qiang Xue committed
9 10

use Yii;
Qiang Xue committed
11
use yii\base\InvalidConfigException;
12
use yii\helpers\FileHelper;
w  
Qiang Xue committed
13

w  
Qiang Xue committed
14
/**
Qiang Xue committed
15
 * FileTarget records log messages in a file.
w  
Qiang Xue committed
16
 *
Qiang Xue committed
17 18 19 20 21
 * The log file is specified via [[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 by 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
{
Qiang Xue committed
28
	/**
peeradach001 committed
29
	 * @var string log file path or path alias. If not set, it will use the "@runtime/logs/app.log" file.
Qiang Xue committed
30
	 * The directory containing the log files will be automatically created if not existing.
Qiang Xue committed
31 32
	 */
	public $logFile;
w  
Qiang Xue committed
33
	/**
Qiang Xue committed
34
	 * @var integer maximum log file size, in kilo-bytes. Defaults to 10240, meaning 10MB.
w  
Qiang Xue committed
35
	 */
Qiang Xue committed
36
	public $maxFileSize = 10240; // in KB
w  
Qiang Xue committed
37
	/**
w  
Qiang Xue committed
38
	 * @var integer number of log files used for rotation. Defaults to 5.
w  
Qiang Xue committed
39
	 */
w  
Qiang Xue committed
40
	public $maxLogFiles = 5;
41 42 43 44 45 46 47 48 49 50 51 52 53
	/**
	 * @var integer the permission to be set for newly created log files.
	 * This value will be used by PHP chmod() function. No umask will be applied.
	 * If not set, the permission will be determined by the current environment.
	 */
	public $fileMode;
	/**
	 * @var integer the permission to be set for newly created directories.
	 * This value will be used by PHP chmod() function. No umask will be applied.
	 * Defaults to 0775, meaning the directory is read-writable by owner and group,
	 * but read-only for other users.
	 */
	public $dirMode = 0775;
w  
Qiang Xue committed
54 55 56 57 58 59 60 61 62


	/**
	 * Initializes the route.
	 * This method is invoked after the route is created by the route manager.
	 */
	public function init()
	{
		parent::init();
Qiang Xue committed
63
		if ($this->logFile === null) {
Qiang Xue committed
64
			$this->logFile = Yii::$app->getRuntimePath() . '/logs/app.log';
Qiang Xue committed
65
		} else {
Qiang Xue committed
66
			$this->logFile = Yii::getAlias($this->logFile);
w  
Qiang Xue committed
67
		}
Qiang Xue committed
68
		$logPath = dirname($this->logFile);
Qiang Xue committed
69
		if (!is_dir($logPath)) {
70
			FileHelper::createDirectory($logPath, $this->dirMode, true);
w  
Qiang Xue committed
71 72 73 74 75 76 77
		}
		if ($this->maxLogFiles < 1) {
			$this->maxLogFiles = 1;
		}
		if ($this->maxFileSize < 1) {
			$this->maxFileSize = 1;
		}
w  
Qiang Xue committed
78 79 80
	}

	/**
Qiang Xue committed
81
	 * Writes log messages to a file.
Qiang Xue committed
82
	 * @throws InvalidConfigException if unable to open the log file for writing
w  
Qiang Xue committed
83
	 */
Qiang Xue committed
84
	public function export()
w  
Qiang Xue committed
85
	{
86
		$text = implode("\n", array_map([$this, 'formatMessage'], $this->messages)) . "\n";
Qiang Xue committed
87 88 89
		if (($fp = @fopen($this->logFile, 'a')) === false) {
			throw new InvalidConfigException("Unable to append to log file: {$this->logFile}");
		}
Qiang Xue committed
90
		@flock($fp, LOCK_EX);
Qiang Xue committed
91
		if (@filesize($this->logFile) > $this->maxFileSize * 1024) {
w  
Qiang Xue committed
92
			$this->rotateFiles();
resurtm committed
93
			@flock($fp, LOCK_UN);
Qiang Xue committed
94 95 96 97
			@fclose($fp);
			@file_put_contents($this->logFile, $text, FILE_APPEND | LOCK_EX);
		} else {
			@fwrite($fp, $text);
resurtm committed
98
			@flock($fp, LOCK_UN);
Qiang Xue committed
99
			@fclose($fp);
w  
Qiang Xue committed
100
		}
101 102 103
		if ($this->fileMode !== null) {
			@chmod($this->logFile, $this->fileMode);
		}
w  
Qiang Xue committed
104 105 106 107 108 109 110
	}

	/**
	 * Rotates log files.
	 */
	protected function rotateFiles()
	{
Qiang Xue committed
111
		$file = $this->logFile;
w  
Qiang Xue committed
112
		for ($i = $this->maxLogFiles; $i > 0; --$i) {
w  
Qiang Xue committed
113
			$rotateFile = $file . '.' . $i;
w  
Qiang Xue committed
114
			if (is_file($rotateFile)) {
w  
Qiang Xue committed
115
				// suppress errors because it's possible multiple processes enter into this section
w  
Qiang Xue committed
116
				if ($i === $this->maxLogFiles) {
w  
Qiang Xue committed
117
					@unlink($rotateFile);
Qiang Xue committed
118
				} else {
w  
Qiang Xue committed
119
					@rename($rotateFile, $file . '.' . ($i + 1));
w  
Qiang Xue committed
120
				}
w  
Qiang Xue committed
121 122
			}
		}
w  
Qiang Xue committed
123
		if (is_file($file)) {
w  
Qiang Xue committed
124
			@rename($file, $file . '.1'); // suppress errors because it's possible multiple processes enter into this section
w  
Qiang Xue committed
125
		}
w  
Qiang Xue committed
126 127
	}
}