FileTarget.php 3.12 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/
 */

w  
Qiang Xue committed
8
namespace yii\logging;
Qiang Xue committed
9
use yii\base\InvalidConfigException;
w  
Qiang Xue committed
10

w  
Qiang Xue committed
11
/**
Qiang Xue committed
12
 * FileTarget records log messages in a file.
w  
Qiang Xue committed
13
 *
Qiang Xue committed
14 15 16 17 18
 * 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
19 20
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
w  
Qiang Xue committed
21
 * @since 2.0
w  
Qiang Xue committed
22
 */
w  
Qiang Xue committed
23
class FileTarget extends Target
w  
Qiang Xue committed
24
{
Qiang Xue committed
25 26 27 28 29 30
	/**
	 * @var string log file path or path alias. If not set, it means the 'application.log' file under
	 * the application runtime directory. Please make sure the directory containing
	 * the log file is writable by the Web server process.
	 */
	public $logFile;
w  
Qiang Xue committed
31
	/**
w  
Qiang Xue committed
32
	 * @var integer maximum log file size, in kilo-bytes. Defaults to 1024, meaning 1MB.
w  
Qiang Xue committed
33
	 */
w  
Qiang Xue committed
34
	public $maxFileSize = 1024; // in KB
w  
Qiang Xue committed
35
	/**
w  
Qiang Xue committed
36
	 * @var integer number of log files used for rotation. Defaults to 5.
w  
Qiang Xue committed
37
	 */
w  
Qiang Xue committed
38
	public $maxLogFiles = 5;
w  
Qiang Xue committed
39 40 41 42 43 44 45 46 47


	/**
	 * Initializes the route.
	 * This method is invoked after the route is created by the route manager.
	 */
	public function init()
	{
		parent::init();
Qiang Xue committed
48
		if ($this->logFile === null) {
Qiang Xue committed
49
			$this->logFile = \Yii::$app->getRuntimePath() . DIRECTORY_SEPARATOR . 'application.log';
Qiang Xue committed
50 51
		} else {
			$this->logFile = \Yii::getAlias($this->logFile);
w  
Qiang Xue committed
52
		}
Qiang Xue committed
53 54 55
		$logPath = dirname($this->logFile);
		if (!is_dir($logPath) || !is_writable($logPath)) {
			throw new InvalidConfigException("Directory '$logPath' does not exist or is not writable.");
w  
Qiang Xue committed
56 57 58 59 60 61 62
		}
		if ($this->maxLogFiles < 1) {
			$this->maxLogFiles = 1;
		}
		if ($this->maxFileSize < 1) {
			$this->maxFileSize = 1;
		}
w  
Qiang Xue committed
63 64 65
	}

	/**
Qiang Xue committed
66 67 68
	 * Sends log messages to specified email addresses.
	 * @param array $messages the messages to be exported. See [[Logger::messages]] for the structure
	 * of each message.
w  
Qiang Xue committed
69
	 */
Qiang Xue committed
70
	public function export($messages)
w  
Qiang Xue committed
71
	{
Qiang Xue committed
72 73 74 75 76 77
		$text = '';
		foreach ($messages as $message) {
			$text .= $this->formatMessage($message);
		}
		$fp = @fopen($this->logFile, 'a');
		@flock($fp, LOCK_EX);
Qiang Xue committed
78
		if (@filesize($this->logFile) > $this->maxFileSize * 1024) {
w  
Qiang Xue committed
79
			$this->rotateFiles();
Qiang Xue committed
80 81 82 83 84 85 86
			@flock($fp,LOCK_UN);
			@fclose($fp);
			@file_put_contents($this->logFile, $text, FILE_APPEND | LOCK_EX);
		} else {
			@fwrite($fp, $text);
			@flock($fp,LOCK_UN);
			@fclose($fp);
w  
Qiang Xue committed
87
		}
w  
Qiang Xue committed
88 89 90 91 92 93 94
	}

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