1: <?php
2: /**
3: * CFileLogRoute class file.
4: *
5: * @author Qiang Xue <qiang.xue@gmail.com>
6: * @link http://www.yiiframework.com/
7: * @copyright 2008-2013 Yii Software LLC
8: * @license http://www.yiiframework.com/license/
9: */
10:
11: /**
12: * CFileLogRoute records log messages in files.
13: *
14: * The log files are stored under {@link setLogPath logPath} and the file name
15: * is specified by {@link setLogFile logFile}. If the size of the log file is
16: * greater than {@link setMaxFileSize maxFileSize} (in kilo-bytes), a rotation
17: * is performed, which renames the current log file by suffixing the file name
18: * with '.1'. All existing log files are moved backwards one place, i.e., '.2'
19: * to '.3', '.1' to '.2'. The property {@link setMaxLogFiles maxLogFiles}
20: * specifies how many files to be kept.
21: * If the property {@link rotateByCopy} is true, the primary log file will be
22: * rotated by a copy and truncated (to be more compatible with log tailers)
23: * otherwise it will be rotated by being renamed.
24: *
25: * @property string $logPath Directory storing log files. Defaults to application runtime path.
26: * @property string $logFile Log file name. Defaults to 'application.log'.
27: * @property integer $maxFileSize Maximum log file size in kilo-bytes (KB). Defaults to 1024 (1MB).
28: * @property integer $maxLogFiles Number of files used for rotation. Defaults to 5.
29: *
30: * @author Qiang Xue <qiang.xue@gmail.com>
31: * @package system.logging
32: * @since 1.0
33: */
34: class CFileLogRoute extends CLogRoute
35: {
36: /**
37: * @var integer maximum log file size
38: */
39: private $_maxFileSize=1024; // in KB
40: /**
41: * @var integer number of log files used for rotation
42: */
43: private $_maxLogFiles=5;
44: /**
45: * @var string directory storing log files
46: */
47: private $_logPath;
48: /**
49: * @var string log file name
50: */
51: private $_logFile='application.log';
52: /**
53: * @var boolean Whether to rotate primary log by copy and truncate
54: * which is more compatible with log tailers. Defaults to false.
55: * @since 1.1.14
56: */
57: public $rotateByCopy=false;
58:
59: /**
60: * Initializes the route.
61: * This method is invoked after the route is created by the route manager.
62: */
63: public function init()
64: {
65: parent::init();
66: if($this->getLogPath()===null)
67: $this->setLogPath(Yii::app()->getRuntimePath());
68: }
69:
70: /**
71: * @return string directory storing log files. Defaults to application runtime path.
72: */
73: public function getLogPath()
74: {
75: return $this->_logPath;
76: }
77:
78: /**
79: * @param string $value directory for storing log files.
80: * @throws CException if the path is invalid
81: */
82: public function setLogPath($value)
83: {
84: $this->_logPath=realpath($value);
85: if($this->_logPath===false || !is_dir($this->_logPath) || !is_writable($this->_logPath))
86: throw new CException(Yii::t('yii','CFileLogRoute.logPath "{path}" does not point to a valid directory. Make sure the directory exists and is writable by the Web server process.',
87: array('{path}'=>$value)));
88: }
89:
90: /**
91: * @return string log file name. Defaults to 'application.log'.
92: */
93: public function getLogFile()
94: {
95: return $this->_logFile;
96: }
97:
98: /**
99: * @param string $value log file name
100: */
101: public function setLogFile($value)
102: {
103: $this->_logFile=$value;
104: }
105:
106: /**
107: * @return integer maximum log file size in kilo-bytes (KB). Defaults to 1024 (1MB).
108: */
109: public function getMaxFileSize()
110: {
111: return $this->_maxFileSize;
112: }
113:
114: /**
115: * @param integer $value maximum log file size in kilo-bytes (KB).
116: */
117: public function setMaxFileSize($value)
118: {
119: if(($this->_maxFileSize=(int)$value)<1)
120: $this->_maxFileSize=1;
121: }
122:
123: /**
124: * @return integer number of files used for rotation. Defaults to 5.
125: */
126: public function getMaxLogFiles()
127: {
128: return $this->_maxLogFiles;
129: }
130:
131: /**
132: * @param integer $value number of files used for rotation.
133: */
134: public function setMaxLogFiles($value)
135: {
136: if(($this->_maxLogFiles=(int)$value)<1)
137: $this->_maxLogFiles=1;
138: }
139:
140: /**
141: * Saves log messages in files.
142: * @param array $logs list of log messages
143: */
144: protected function processLogs($logs)
145: {
146: $text='';
147: foreach($logs as $log)
148: $text.=$this->formatLogMessage($log[0],$log[1],$log[2],$log[3]);
149:
150: $logFile=$this->getLogPath().DIRECTORY_SEPARATOR.$this->getLogFile();
151: $fp=@fopen($logFile,'a');
152: @flock($fp,LOCK_EX);
153: if(@filesize($logFile)>$this->getMaxFileSize()*1024)
154: {
155: $this->rotateFiles();
156: @flock($fp,LOCK_UN);
157: @fclose($fp);
158: @file_put_contents($logFile,$text,FILE_APPEND|LOCK_EX);
159: }
160: else
161: {
162: @fwrite($fp,$text);
163: @flock($fp,LOCK_UN);
164: @fclose($fp);
165: }
166: }
167:
168: /**
169: * Rotates log files.
170: */
171: protected function rotateFiles()
172: {
173: $file=$this->getLogPath().DIRECTORY_SEPARATOR.$this->getLogFile();
174: $max=$this->getMaxLogFiles();
175: for($i=$max;$i>0;--$i)
176: {
177: $rotateFile=$file.'.'.$i;
178: if(is_file($rotateFile))
179: {
180: // suppress errors because it's possible multiple processes enter into this section
181: if($i===$max)
182: @unlink($rotateFile);
183: else
184: @rename($rotateFile,$file.'.'.($i+1));
185: }
186: }
187: if(is_file($file))
188: {
189: // suppress errors because it's possible multiple processes enter into this section
190: if($this->rotateByCopy)
191: {
192: @copy($file,$file.'.1');
193: if($fp=@fopen($file,'a'))
194: {
195: @ftruncate($fp,0);
196: @fclose($fp);
197: }
198: }
199: else
200: @rename($file,$file.'.1');
201: }
202: // clear stat cache after moving files so later file size check is not cached
203: clearstatcache();
204: }
205: }
206: