LoggerAppenderRollingFile.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. /**
  3. * log4php is a PHP port of the log4j java logging package.
  4. *
  5. * <p>This framework is based on log4j (see {@link http://jakarta.apache.org/log4j log4j} for details).</p>
  6. * <p>Design, strategies and part of the methods documentation are developed by log4j team
  7. * (Ceki Gülcü as log4j project founder and
  8. * {@link http://jakarta.apache.org/log4j/docs/contributors.html contributors}).</p>
  9. *
  10. * <p>PHP port, extensions and modifications by VxR. All rights reserved.<br>
  11. * For more information, please see {@link http://www.vxr.it/log4php/}.</p>
  12. *
  13. * <p>This software is published under the terms of the LGPL License
  14. * a copy of which has been included with this distribution in the LICENSE file.</p>
  15. *
  16. * @package log4php
  17. * @subpackage appenders
  18. */
  19. /**
  20. * @ignore
  21. */
  22. if (!defined('LOG4PHP_DIR')) define('LOG4PHP_DIR', dirname(__FILE__) . '/..');
  23. require_once(LOG4PHP_DIR . '/appenders/LoggerAppenderFile.php');
  24. /**
  25. * LoggerAppenderRollingFile extends LoggerAppenderFile to backup the log files
  26. * when they reach a certain size.
  27. *
  28. * <p>Parameters are {@link $maxFileSize}, {@link $maxBackupIndex}.</p>
  29. *
  30. * <p>Contributors: Sergio Strampelli.</p>
  31. *
  32. * @author VxR <vxr@vxr.it>
  33. * @version $Revision: 1.1 $
  34. * @package log4php
  35. * @subpackage appenders
  36. */
  37. class LoggerAppenderRollingFile extends LoggerAppenderFile {
  38. /**
  39. * Set the maximum size that the output file is allowed to reach
  40. * before being rolled over to backup files.
  41. *
  42. * <p>In configuration files, the <var>MaxFileSize</var> option takes a
  43. * long integer in the range 0 - 2^63. You can specify the value
  44. * with the suffixes "KB", "MB" or "GB" so that the integer is
  45. * interpreted being expressed respectively in kilobytes, megabytes
  46. * or gigabytes. For example, the value "10KB" will be interpreted
  47. * as 10240.</p>
  48. * <p>The default maximum file size is 10MB.</p>
  49. *
  50. * <p>Note that MaxFileSize cannot exceed <b>2 GB</b>.</p>
  51. *
  52. * @var integer
  53. */
  54. var $maxFileSize = 10485760;
  55. /**
  56. * Set the maximum number of backup files to keep around.
  57. *
  58. * <p>The <var>MaxBackupIndex</var> option determines how many backup
  59. * files are kept before the oldest is erased. This option takes
  60. * a positive integer value. If set to zero, then there will be no
  61. * backup files and the log file will be truncated when it reaches
  62. * MaxFileSize.</p>
  63. * <p>There is one backup file by default.</p>
  64. *
  65. * @var integer
  66. */
  67. var $maxBackupIndex = 1;
  68. /**
  69. * @var string the filename expanded
  70. * @access private
  71. */
  72. var $expandedFileName = null;
  73. /**
  74. * Constructor.
  75. *
  76. * @param string $name appender name
  77. */
  78. function LoggerAppenderRollingFile($name)
  79. {
  80. $this->LoggerAppenderFile($name);
  81. }
  82. /**
  83. * Returns the value of the MaxBackupIndex option.
  84. * @return integer
  85. */
  86. function getExpandedFileName() {
  87. return $this->expandedFileName;
  88. }
  89. /**
  90. * Returns the value of the MaxBackupIndex option.
  91. * @return integer
  92. */
  93. function getMaxBackupIndex() {
  94. return $this->maxBackupIndex;
  95. }
  96. /**
  97. * Get the maximum size that the output file is allowed to reach
  98. * before being rolled over to backup files.
  99. * @return integer
  100. */
  101. function getMaximumFileSize() {
  102. return $this->maxFileSize;
  103. }
  104. /**
  105. * Implements the usual roll over behaviour.
  106. *
  107. * <p>If MaxBackupIndex is positive, then files File.1, ..., File.MaxBackupIndex -1 are renamed to File.2, ..., File.MaxBackupIndex.
  108. * Moreover, File is renamed File.1 and closed. A new File is created to receive further log output.
  109. *
  110. * <p>If MaxBackupIndex is equal to zero, then the File is truncated with no backup files created.
  111. */
  112. function rollOver()
  113. {
  114. // If maxBackups <= 0, then there is no file renaming to be done.
  115. if($this->maxBackupIndex > 0) {
  116. $fileName = $this->getExpandedFileName();
  117. // Delete the oldest file, to keep Windows happy.
  118. $file = $fileName . '.' . $this->maxBackupIndex;
  119. if (is_writable($file))
  120. unlink($file);
  121. // Map {(maxBackupIndex - 1), ..., 2, 1} to {maxBackupIndex, ..., 3, 2}
  122. for ($i = $this->maxBackupIndex - 1; $i >= 1; $i--) {
  123. $file = $fileName . "." . $i;
  124. if (is_readable($file)) {
  125. $target = $fileName . '.' . ($i + 1);
  126. rename($file, $target);
  127. }
  128. }
  129. // Rename fileName to fileName.1
  130. $target = $fileName . ".1";
  131. $this->closeFile(); // keep windows happy.
  132. $file = $fileName;
  133. rename($file, $target);
  134. }
  135. $this->setFile($fileName, false);
  136. unset($this->fp);
  137. $this->activateOptions();
  138. }
  139. function setFileName($fileName)
  140. {
  141. $this->fileName = $fileName;
  142. $this->expandedFileName = realpath($fileName);
  143. LoggerLog::debug("LoggerAppenderRollingFile::setFileName():filename=[{$fileName}]:expandedFileName=[{$this->expandedFileName}]");
  144. }
  145. /**
  146. * Set the maximum number of backup files to keep around.
  147. *
  148. * <p>The <b>MaxBackupIndex</b> option determines how many backup
  149. * files are kept before the oldest is erased. This option takes
  150. * a positive integer value. If set to zero, then there will be no
  151. * backup files and the log file will be truncated when it reaches
  152. * MaxFileSize.
  153. *
  154. * @param mixed $maxBackups
  155. */
  156. function setMaxBackupIndex($maxBackups)
  157. {
  158. if (is_numeric($maxBackups))
  159. $this->maxBackupIndex = abs((int)$maxBackups);
  160. }
  161. /**
  162. * Set the maximum size that the output file is allowed to reach
  163. * before being rolled over to backup files.
  164. *
  165. * @param mixed $maxFileSize
  166. * @see setMaxFileSize()
  167. */
  168. function setMaximumFileSize($maxFileSize)
  169. {
  170. $this->setMaxFileSize($maxFileSize);
  171. }
  172. /**
  173. * Set the maximum size that the output file is allowed to reach
  174. * before being rolled over to backup files.
  175. * <p>In configuration files, the <b>MaxFileSize</b> option takes an
  176. * long integer in the range 0 - 2^63. You can specify the value
  177. * with the suffixes "KB", "MB" or "GB" so that the integer is
  178. * interpreted being expressed respectively in kilobytes, megabytes
  179. * or gigabytes. For example, the value "10KB" will be interpreted
  180. * as 10240.
  181. *
  182. * @param mixed $value
  183. */
  184. function setMaxFileSize($value)
  185. {
  186. $maxFileSize = null;
  187. $numpart = substr($value,0, strlen($value) -2);
  188. $suffix = strtoupper(substr($value, -2));
  189. switch ($suffix) {
  190. case 'KB': $maxFileSize = (int)((int)$numpart * 1024); break;
  191. case 'MB': $maxFileSize = (int)((int)$numpart * 1024 * 1024); break;
  192. case 'GB': $maxFileSize = (int)((int)$numpart * 1024 * 1024 * 1024); break;
  193. default:
  194. if (is_numeric($value)) {
  195. $maxFileSize = (int)$value;
  196. }
  197. }
  198. if ($maxFileSize === null) {
  199. LoggerLog::debug("LoggerAppenderRollingFile::setMaxFileSize():value=[$value] wrong declaration");
  200. } else {
  201. $this->maxFileSize = abs($maxFileSize);
  202. }
  203. }
  204. /**
  205. * @param LoggerLoggingEvent $event
  206. */
  207. function append($event)
  208. {
  209. if ($this->fp) {
  210. parent::append($event);
  211. if (ftell($this->fp) > $this->getMaximumFileSize())
  212. $this->rollOver();
  213. }
  214. }
  215. }
  216. ?>