FileTarget.php 3.16 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 10

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

w  
Qiang Xue committed
13
/**
Qiang Xue committed
14
 * FileTarget records log messages in a file.
w  
Qiang Xue committed
15
 *
Qiang Xue committed
16 17 18 19 20
 * 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
21 22
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
w  
Qiang Xue committed
23
 * @since 2.0
w  
Qiang Xue committed
24
 */
w  
Qiang Xue committed
25
class FileTarget extends Target
w  
Qiang Xue committed
26
{
Qiang Xue committed
27
	/**
Qiang Xue committed
28 29
	 * @var string log file path or path alias. If not set, it will use the "runtime/logs/app.log" file.
	 * The directory containing the log files will be automatically created if not existing.
Qiang Xue committed
30 31
	 */
	public $logFile;
w  
Qiang Xue committed
32
	/**
Qiang Xue committed
33
	 * @var integer maximum log file size, in kilo-bytes. Defaults to 10240, meaning 10MB.
w  
Qiang Xue committed
34
	 */
Qiang Xue committed
35
	public $maxFileSize = 10240; // in KB
w  
Qiang Xue committed
36
	/**
w  
Qiang Xue committed
37
	 * @var integer number of log files used for rotation. Defaults to 5.
w  
Qiang Xue committed
38
	 */
w  
Qiang Xue committed
39
	public $maxLogFiles = 5;
w  
Qiang Xue committed
40 41 42 43 44 45 46 47 48


	/**
	 * Initializes the route.
	 * This method is invoked after the route is created by the route manager.
	 */
	public function init()
	{
		parent::init();
Qiang Xue committed
49
		if ($this->logFile === null) {
Qiang Xue committed
50
			$this->logFile = Yii::$app->getRuntimePath() . '/logs/app.log';
Qiang Xue committed
51
		} else {
Qiang Xue committed
52
			$this->logFile = Yii::getAlias($this->logFile);
w  
Qiang Xue committed
53
		}
Qiang Xue committed
54
		$logPath = dirname($this->logFile);
Qiang Xue committed
55 56
		if (!is_dir($logPath)) {
			@mkdir($logPath, 0777, true);
w  
Qiang Xue committed
57 58 59 60 61 62 63
		}
		if ($this->maxLogFiles < 1) {
			$this->maxLogFiles = 1;
		}
		if ($this->maxFileSize < 1) {
			$this->maxFileSize = 1;
		}
w  
Qiang Xue committed
64 65 66
	}

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

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