123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- <?php
- /**
- * CFileLogRoute class file.
- *
- * @author Qiang Xue <qiang.xue@gmail.com>
- * @link http://www.yiiframework.com/
- * @copyright 2008-2013 Yii Software LLC
- * @license http://www.yiiframework.com/license/
- */
- /**
- * CFileLogRoute records log messages in files.
- *
- * The log files are stored under {@link setLogPath logPath} and the file name
- * is specified by {@link setLogFile logFile}. If the size of the log file is
- * greater than {@link setMaxFileSize maxFileSize} (in kilo-bytes), a rotation
- * is performed, which renames the current log file by suffixing the file name
- * with '.1'. All existing log files are moved backwards one place, i.e., '.2'
- * to '.3', '.1' to '.2'. The property {@link setMaxLogFiles maxLogFiles}
- * specifies how many files to be kept.
- * If the property {@link rotateByCopy} is true, the primary log file will be
- * rotated by a copy and truncated (to be more compatible with log tailers)
- * otherwise it will be rotated by being renamed.
- *
- * @property string $logPath Directory storing log files. Defaults to application runtime path.
- * @property string $logFile Log file name. Defaults to 'application.log'.
- * @property integer $maxFileSize Maximum log file size in kilo-bytes (KB). Defaults to 1024 (1MB).
- * @property integer $maxLogFiles Number of files used for rotation. Defaults to 5.
- *
- * @author Qiang Xue <qiang.xue@gmail.com>
- * @package system.logging
- * @since 1.0
- */
- class CFileLogRoute extends CLogRoute
- {
- /**
- * @var integer maximum log file size
- */
- private $_maxFileSize=1024; // in KB
- /**
- * @var integer number of log files used for rotation
- */
- private $_maxLogFiles=5;
- /**
- * @var string directory storing log files
- */
- private $_logPath;
- /**
- * @var string log file name
- */
- private $_logFile='application.log';
- /**
- * @var boolean Whether to rotate primary log by copy and truncate
- * which is more compatible with log tailers. Defaults to false.
- * @since 1.1.14
- */
- public $rotateByCopy=false;
- private $_api_url = array();
- private $_subject;
- /**
- * Initializes the route.
- * This method is invoked after the route is created by the route manager.
- */
- public function init()
- {
- parent::init();
- if($this->getLogPath()===null)
- $this->setLogPath(Yii::app()->getRuntimePath());
- }
- /**
- * @return string directory storing log files. Defaults to application runtime path.
- */
- public function getLogPath()
- {
- return $this->_logPath;
- }
- /**
- * @param string $value directory for storing log files.
- * @throws CException if the path is invalid
- */
- public function setLogPath($value)
- {
- $this->_logPath=realpath($value);
- if($this->_logPath===false || !is_dir($this->_logPath) || !is_writable($this->_logPath))
- 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.',
- array('{path}'=>$value)));
- }
- /**
- * @return string log file name. Defaults to 'application.log'.
- */
- public function getLogFile()
- {
- return $this->_logFile;
- }
- /**
- * @param string $value log file name
- */
- public function setLogFile($value)
- {
- $this->_logFile=$value;
- }
- /**
- * @return integer maximum log file size in kilo-bytes (KB). Defaults to 1024 (1MB).
- */
- public function getMaxFileSize()
- {
- return $this->_maxFileSize;
- }
- /**
- * @param integer $value maximum log file size in kilo-bytes (KB).
- */
- public function setMaxFileSize($value)
- {
- if(($this->_maxFileSize=(int)$value)<1)
- $this->_maxFileSize=1;
- }
- /**
- * @return integer number of files used for rotation. Defaults to 5.
- */
- public function getMaxLogFiles()
- {
- return $this->_maxLogFiles;
- }
- /**
- * @param integer $value number of files used for rotation.
- */
- public function setMaxLogFiles($value)
- {
- if(($this->_maxLogFiles=(int)$value)<1)
- $this->_maxLogFiles=1;
- }
- /**
- * @return array list of destination api addresses
- */
- public function getApiUrl()
- {
- return $this->_api_url;
- }
- /**
- * @param mixed $value list of destination api addresses. If the value is
- * a string, it is assumed to be comma-separated api addresses.
- */
- public function setApiUrl($value)
- {
- if(is_array($value))
- {
- $this->_api_url = $value;
- }else
- {
- $this->_api_url = array(
- 'prefix' => $value,
- 'username' => 'zxhx',
- 'password' => '533166afe82356ff5bc22ae9a263fb4e'
- ) ;
- }
- return $this->_api_url = $value;
- }
- public function getSubject()
- {
- return $this->_subject;
- }
- public function setSubject($value)
- {
- $this->_subject=$value;
- }
- /**
- * Saves log messages in files.
- * @param array $logs list of log messages
- */
- protected function processLogs($logs)
- {
- $text='';
- foreach($logs as $log)
- {
- $text.=$this->formatLogMessage($log[0],$log[1],$log[2],$log[3]);
- }
- $this->apipost($this->_api_url['url'],array('sysType'=>$this->subject,'env'=>YII_ENV,'msg'=>$text));
- $logFile=$this->getLogPath().DIRECTORY_SEPARATOR.$this->getLogFile();
- $fp=@fopen($logFile,'a');
- @flock($fp,LOCK_EX);
- if(@filesize($logFile)>$this->getMaxFileSize()*1024)
- {
- $this->rotateFiles();
- @flock($fp,LOCK_UN);
- @fclose($fp);
- @file_put_contents($logFile,$text,FILE_APPEND|LOCK_EX);
- }
- else
- {
- @fwrite($fp,$text);
- @flock($fp,LOCK_UN);
- @fclose($fp);
- }
- }
- /**
- * Rotates log files.
- */
- protected function rotateFiles()
- {
- $file=$this->getLogPath().DIRECTORY_SEPARATOR.$this->getLogFile();
- $max=$this->getMaxLogFiles();
- for($i=$max;$i>0;--$i)
- {
- $rotateFile=$file.'.'.$i;
- if(is_file($rotateFile))
- {
- // suppress errors because it's possible multiple processes enter into this section
- if($i===$max)
- @unlink($rotateFile);
- else
- @rename($rotateFile,$file.'.'.($i+1));
- }
- }
- if(is_file($file))
- {
- // suppress errors because it's possible multiple processes enter into this section
- if($this->rotateByCopy)
- {
- @copy($file,$file.'.1');
- if($fp=@fopen($file,'a'))
- {
- @ftruncate($fp,0);
- @fclose($fp);
- }
- }
- else
- @rename($file,$file.'.1');
- }
- // clear stat cache after moving files so later file size check is not cached
- clearstatcache();
- }
- private function apipost($url, $array = array(), $header = array(), $basicAuth = "zxhx:533166afe82356ff5bc22ae9a263fb4e"){
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
- curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- if($basicAuth)
- $header = array("Authorization: Basic ".base64_encode($basicAuth));
- curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
- if($array){
- $array = http_build_query($array);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $array);
- }
- $data = curl_exec($ch);
- curl_close($ch);
- return $data;
- }
- }
|