"Fossies" - the Fresh Open Source Software Archive

Member "yii-1.1.24.a5ab20/framework/logging/CFileLogRoute.php" (7 Jun 2021, 5877 Bytes) of package /linux/www/yii-1.1.24.a5ab20.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "CFileLogRoute.php" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 1.1.23.445827_vs_1.1.24.a5ab20.

    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      * @var integer the permission to be set for newly created log files.
   60      * This value will be used by PHP chmod() function. No umask will be applied.
   61      * If not set, the permission will be determined by the current environment.
   62      */
   63     public $chmod;
   64     /**
   65      * Initializes the route.
   66      * This method is invoked after the route is created by the route manager.
   67      */
   68     public function init()
   69     {
   70         parent::init();
   71         if($this->getLogPath()===null)
   72             $this->setLogPath(Yii::app()->getRuntimePath());
   73     }
   74 
   75     /**
   76      * @return string directory storing log files. Defaults to application runtime path.
   77      */
   78     public function getLogPath()
   79     {
   80         return $this->_logPath;
   81     }
   82 
   83     /**
   84      * @param string $value directory for storing log files.
   85      * @throws CException if the path is invalid
   86      */
   87     public function setLogPath($value)
   88     {
   89         $this->_logPath=realpath($value);
   90         if($this->_logPath===false || !is_dir($this->_logPath) || !is_writable($this->_logPath))
   91             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.',
   92                 array('{path}'=>$value)));
   93     }
   94 
   95     /**
   96      * @return string log file name. Defaults to 'application.log'.
   97      */
   98     public function getLogFile()
   99     {
  100         return $this->_logFile;
  101     }
  102 
  103     /**
  104      * @param string $value log file name
  105      */
  106     public function setLogFile($value)
  107     {
  108         $this->_logFile=$value;
  109     }
  110 
  111     /**
  112      * @return integer maximum log file size in kilo-bytes (KB). Defaults to 1024 (1MB).
  113      */
  114     public function getMaxFileSize()
  115     {
  116         return $this->_maxFileSize;
  117     }
  118 
  119     /**
  120      * @param integer $value maximum log file size in kilo-bytes (KB).
  121      */
  122     public function setMaxFileSize($value)
  123     {
  124         if(($this->_maxFileSize=(int)$value)<1)
  125             $this->_maxFileSize=1;
  126     }
  127 
  128     /**
  129      * @return integer number of files used for rotation. Defaults to 5.
  130      */
  131     public function getMaxLogFiles()
  132     {
  133         return $this->_maxLogFiles;
  134     }
  135 
  136     /**
  137      * @param integer $value number of files used for rotation.
  138      */
  139     public function setMaxLogFiles($value)
  140     {
  141         if(($this->_maxLogFiles=(int)$value)<1)
  142             $this->_maxLogFiles=1;
  143     }
  144 
  145     /**
  146      * Saves log messages in files.
  147      * @param array $logs list of log messages
  148      */
  149     protected function processLogs($logs)
  150     {
  151         $text='';
  152         foreach($logs as $log)
  153             $text.=$this->formatLogMessage($log[0],$log[1],$log[2],$log[3]);
  154 
  155         $logFile=$this->getLogPath().DIRECTORY_SEPARATOR.$this->getLogFile();
  156         $fp=@fopen($logFile,'a');
  157         @flock($fp,LOCK_EX);
  158         if(@filesize($logFile)>$this->getMaxFileSize()*1024)
  159         {
  160             $this->rotateFiles();
  161             @flock($fp,LOCK_UN);
  162             @fclose($fp);
  163             @file_put_contents($logFile,$text,FILE_APPEND|LOCK_EX);
  164         }
  165         else
  166         {
  167             @fwrite($fp,$text);
  168             @flock($fp,LOCK_UN);
  169             @fclose($fp);
  170         }
  171         if($this->chmod !== null)
  172             @chmod($logFile, $this->chmod);
  173     }
  174 
  175     /**
  176      * Rotates log files.
  177      */
  178     protected function rotateFiles()
  179     {
  180         $file=$this->getLogPath().DIRECTORY_SEPARATOR.$this->getLogFile();
  181         $max=$this->getMaxLogFiles();
  182         for($i=$max;$i>0;--$i)
  183         {
  184             $rotateFile=$file.'.'.$i;
  185             if(is_file($rotateFile))
  186             {
  187                 // suppress errors because it's possible multiple processes enter into this section
  188                 if($i===$max)
  189                     @unlink($rotateFile);
  190                 else
  191                     @rename($rotateFile,$file.'.'.($i+1));
  192             }
  193         }
  194         if(is_file($file))
  195         {
  196             // suppress errors because it's possible multiple processes enter into this section
  197             if($this->rotateByCopy)
  198             {
  199                 @copy($file,$file.'.1');
  200                 if($fp=@fopen($file,'a'))
  201                 {
  202                     @ftruncate($fp,0);
  203                     @fclose($fp);
  204                 }
  205             }
  206             else
  207                 @rename($file,$file.'.1');
  208         }
  209         // clear stat cache after moving files so later file size check is not cached
  210         clearstatcache();
  211     }
  212 }