/yii-1.1.4/framework/logging/CFileLogRoute.php

https://github.com/sassman/django-benchmark · PHP · 162 lines · 77 code · 13 blank · 72 comment · 11 complexity · ff6f3034ef177ed1a5bc8f2f99e504a3 MD5 · raw file

  1. <?php
  2. /**
  3. * CFileLogRoute class file.
  4. *
  5. * @author Qiang Xue <qiang.xue@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright Copyright &copy; 2008-2010 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. /**
  11. * CFileLogRoute records log messages in files.
  12. *
  13. * The log files are stored under {@link setLogPath logPath} and the file name
  14. * is specified by {@link setLogFile logFile}. If the size of the log file is
  15. * greater than {@link setMaxFileSize maxFileSize} (in kilo-bytes), a rotation
  16. * is performed, which renames the current log file by suffixing the file name
  17. * with '.1'. All existing log files are moved backwards one place, i.e., '.2'
  18. * to '.3', '.1' to '.2'. The property {@link setMaxLogFiles maxLogFiles}
  19. * specifies how many files to be kept.
  20. *
  21. * @author Qiang Xue <qiang.xue@gmail.com>
  22. * @version $Id: CFileLogRoute.php 1678 2010-01-07 21:02:00Z qiang.xue $
  23. * @package system.logging
  24. * @since 1.0
  25. */
  26. class CFileLogRoute extends CLogRoute
  27. {
  28. /**
  29. * @var integer maximum log file size
  30. */
  31. private $_maxFileSize=1024; // in KB
  32. /**
  33. * @var integer number of log files used for rotation
  34. */
  35. private $_maxLogFiles=5;
  36. /**
  37. * @var string directory storing log files
  38. */
  39. private $_logPath;
  40. /**
  41. * @var string log file name
  42. */
  43. private $_logFile='application.log';
  44. /**
  45. * Initializes the route.
  46. * This method is invoked after the route is created by the route manager.
  47. */
  48. public function init()
  49. {
  50. parent::init();
  51. if($this->getLogPath()===null)
  52. $this->setLogPath(Yii::app()->getRuntimePath());
  53. }
  54. /**
  55. * @return string directory storing log files. Defaults to application runtime path.
  56. */
  57. public function getLogPath()
  58. {
  59. return $this->_logPath;
  60. }
  61. /**
  62. * @param string directory for storing log files.
  63. * @throws CException if the path is invalid
  64. */
  65. public function setLogPath($value)
  66. {
  67. $this->_logPath=realpath($value);
  68. if($this->_logPath===false || !is_dir($this->_logPath) || !is_writable($this->_logPath))
  69. 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.',
  70. array('{path}'=>$value)));
  71. }
  72. /**
  73. * @return string log file name. Defaults to 'application.log'.
  74. */
  75. public function getLogFile()
  76. {
  77. return $this->_logFile;
  78. }
  79. /**
  80. * @param string log file name
  81. */
  82. public function setLogFile($value)
  83. {
  84. $this->_logFile=$value;
  85. }
  86. /**
  87. * @return integer maximum log file size in kilo-bytes (KB). Defaults to 1024 (1MB).
  88. */
  89. public function getMaxFileSize()
  90. {
  91. return $this->_maxFileSize;
  92. }
  93. /**
  94. * @param integer maximum log file size in kilo-bytes (KB).
  95. */
  96. public function setMaxFileSize($value)
  97. {
  98. if(($this->_maxFileSize=(int)$value)<1)
  99. $this->_maxFileSize=1;
  100. }
  101. /**
  102. * @return integer number of files used for rotation. Defaults to 5.
  103. */
  104. public function getMaxLogFiles()
  105. {
  106. return $this->_maxLogFiles;
  107. }
  108. /**
  109. * @param integer number of files used for rotation.
  110. */
  111. public function setMaxLogFiles($value)
  112. {
  113. if(($this->_maxLogFiles=(int)$value)<1)
  114. $this->_maxLogFiles=1;
  115. }
  116. /**
  117. * Saves log messages in files.
  118. * @param array list of log messages
  119. */
  120. protected function processLogs($logs)
  121. {
  122. $logFile=$this->getLogPath().DIRECTORY_SEPARATOR.$this->getLogFile();
  123. if(@filesize($logFile)>$this->getMaxFileSize()*1024)
  124. $this->rotateFiles();
  125. foreach($logs as $log)
  126. error_log($this->formatLogMessage($log[0],$log[1],$log[2],$log[3]),3,$logFile);
  127. }
  128. /**
  129. * Rotates log files.
  130. */
  131. protected function rotateFiles()
  132. {
  133. $file=$this->getLogPath().DIRECTORY_SEPARATOR.$this->getLogFile();
  134. $max=$this->getMaxLogFiles();
  135. for($i=$max;$i>0;--$i)
  136. {
  137. $rotateFile=$file.'.'.$i;
  138. if(is_file($rotateFile))
  139. {
  140. if($i===$max)
  141. unlink($rotateFile);
  142. else
  143. rename($rotateFile,$file.'.'.($i+1));
  144. }
  145. }
  146. if(is_file($file))
  147. rename($file,$file.'.1');
  148. }
  149. }