You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					116 lines
				
				3.2 KiB
			
		
		
			
		
	
	
					116 lines
				
				3.2 KiB
			| 
								 
											14 years ago
										 
									 | 
							
								<?php
							 | 
						||
| 
								 | 
							
								/**
							 | 
						||
| 
								 | 
							
								 * @link http://www.yiiframework.com/
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								 * @copyright Copyright (c) 2008 Yii Software LLC
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
								 * @license http://www.yiiframework.com/license/
							 | 
						||
| 
								 | 
							
								 */
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
								namespace yii\logging;
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
								use Yii;
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								use yii\base\InvalidConfigException;
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
								/**
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								 * FileTarget records log messages in a file.
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
								 *
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								 * 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.
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
								 *
							 | 
						||
| 
								 | 
							
								 * @author Qiang Xue <qiang.xue@gmail.com>
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
								 * @since 2.0
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
								 */
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
								class FileTarget extends Target
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
								{
							 | 
						||
| 
								 | 
							
									/**
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									 * @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.
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									 */
							 | 
						||
| 
								 | 
							
									public $logFile;
							 | 
						||
| 
								 | 
							
									/**
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									 * @var integer maximum log file size, in kilo-bytes. Defaults to 10240, meaning 10MB.
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
									 */
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									public $maxFileSize = 10240; // in KB
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
									/**
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
									 * @var integer number of log files used for rotation. Defaults to 5.
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
									 */
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
									public $maxLogFiles = 5;
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									/**
							 | 
						||
| 
								 | 
							
									 * Initializes the route.
							 | 
						||
| 
								 | 
							
									 * This method is invoked after the route is created by the route manager.
							 | 
						||
| 
								 | 
							
									 */
							 | 
						||
| 
								 | 
							
									public function init()
							 | 
						||
| 
								 | 
							
									{
							 | 
						||
| 
								 | 
							
										parent::init();
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
										if ($this->logFile === null) {
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
											$this->logFile = Yii::$app->getRuntimePath() . '/logs/app.log';
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
										} else {
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
											$this->logFile = Yii::getAlias($this->logFile);
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
										}
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
										$logPath = dirname($this->logFile);
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
										if (!is_dir($logPath)) {
							 | 
						||
| 
								 | 
							
											@mkdir($logPath, 0777, true);
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
										}
							 | 
						||
| 
								 | 
							
										if ($this->maxLogFiles < 1) {
							 | 
						||
| 
								 | 
							
											$this->maxLogFiles = 1;
							 | 
						||
| 
								 | 
							
										}
							 | 
						||
| 
								 | 
							
										if ($this->maxFileSize < 1) {
							 | 
						||
| 
								 | 
							
											$this->maxFileSize = 1;
							 | 
						||
| 
								 | 
							
										}
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									/**
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									 * Sends log messages to specified email addresses.
							 | 
						||
| 
								 | 
							
									 * @param array $messages the messages to be exported. See [[Logger::messages]] for the structure
							 | 
						||
| 
								 | 
							
									 * of each message.
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									 * @throws InvalidConfigException if unable to open the log file for writing
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
									 */
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									public function export($messages)
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
									{
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
										$text = '';
							 | 
						||
| 
								 | 
							
										foreach ($messages as $message) {
							 | 
						||
| 
								 | 
							
											$text .= $this->formatMessage($message);
							 | 
						||
| 
								 | 
							
										}
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
										if (($fp = @fopen($this->logFile, 'a')) === false) {
							 | 
						||
| 
								 | 
							
											throw new InvalidConfigException("Unable to append to log file: {$this->logFile}");
							 | 
						||
| 
								 | 
							
										}
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
										@flock($fp, LOCK_EX);
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
										if (@filesize($this->logFile) > $this->maxFileSize * 1024) {
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
											$this->rotateFiles();
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
											@flock($fp, LOCK_UN);
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
											@fclose($fp);
							 | 
						||
| 
								 | 
							
											@file_put_contents($this->logFile, $text, FILE_APPEND | LOCK_EX);
							 | 
						||
| 
								 | 
							
										} else {
							 | 
						||
| 
								 | 
							
											@fwrite($fp, $text);
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
											@flock($fp, LOCK_UN);
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
											@fclose($fp);
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
										}
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									/**
							 | 
						||
| 
								 | 
							
									 * Rotates log files.
							 | 
						||
| 
								 | 
							
									 */
							 | 
						||
| 
								 | 
							
									protected function rotateFiles()
							 | 
						||
| 
								 | 
							
									{
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
										$file = $this->logFile;
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
										for ($i = $this->maxLogFiles; $i > 0; --$i) {
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
											$rotateFile = $file . '.' . $i;
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
											if (is_file($rotateFile)) {
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
												// suppress errors because it's possible multiple processes enter into this section
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
												if ($i === $this->maxLogFiles) {
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
													@unlink($rotateFile);
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
												} else {
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
													@rename($rotateFile, $file . '.' . ($i + 1));
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
												}
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
											}
							 | 
						||
| 
								 | 
							
										}
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
										if (is_file($file)) {
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
											@rename($file, $file . '.1'); // suppress errors because it's possible multiple processes enter into this section
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
										}
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
									}
							 | 
						||
| 
								 | 
							
								}
							 |