PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/log4php/appenders/LoggerAppenderRollingFile.php

http://sabre-zarafa.googlecode.com/
PHP | 231 lines | 65 code | 22 blank | 144 comment | 9 complexity | 5febb976a2a6ab9edd436717d9094d87 MD5 | raw file
  1. <?php
  2. /**
  3. * Licensed to the Apache Software Foundation (ASF) under one or more
  4. * contributor license agreements. See the NOTICE file distributed with
  5. * this work for additional information regarding copyright ownership.
  6. * The ASF licenses this file to You under the Apache License, Version 2.0
  7. * (the "License"); you may not use this file except in compliance with
  8. * the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. * @package log4php
  19. */
  20. /**
  21. * LoggerAppenderRollingFile extends LoggerAppenderFile to backup the log files
  22. * when they reach a certain size.
  23. *
  24. * This appender uses a layout.
  25. *
  26. * Parameters are:
  27. * - file - The target file to write to
  28. * - filename - The target file to write to (deprecated, use "file" instead).
  29. * - append - Sets if the appender should append to the end of the file or overwrite content ("true" or "false")
  30. * - maxBackupIndex - Set the maximum number of backup files to keep around (int)
  31. * - maxFileSize - Set the maximum size that the output file is allowed to
  32. * reach before being rolled over to backup files.
  33. * Suffixes like "KB", "MB" or "GB" are allowed, f. e. "10KB" is interpreted as 10240
  34. * - maximumFileSize - Alias to maxFileSize (deprecated, use "maxFileSize" instead)
  35. *
  36. * <p>Contributors: Sergio Strampelli.</p>
  37. *
  38. * An example:
  39. *
  40. * {@example ../../examples/php/appender_socket.php 19}
  41. *
  42. * {@example ../../examples/resources/appender_socket.properties 18}
  43. *
  44. * @version $Revision: 31 $
  45. * @package log4php
  46. * @subpackage appenders
  47. */
  48. class LoggerAppenderRollingFile extends LoggerAppenderFile {
  49. /**
  50. * Set the maximum size that the output file is allowed to reach
  51. * before being rolled over to backup files.
  52. *
  53. * <p>In configuration files, the <var>MaxFileSize</var> option takes a
  54. * long integer in the range 0 - 2^63. You can specify the value
  55. * with the suffixes "KB", "MB" or "GB" so that the integer is
  56. * interpreted being expressed respectively in kilobytes, megabytes
  57. * or gigabytes. For example, the value "10KB" will be interpreted
  58. * as 10240.</p>
  59. * <p>The default maximum file size is 10MB.</p>
  60. *
  61. * <p>Note that MaxFileSize cannot exceed <b>2 GB</b>.</p>
  62. *
  63. * @var integer
  64. */
  65. protected $maxFileSize = 10485760;
  66. /**
  67. * Set the maximum number of backup files to keep around.
  68. *
  69. * <p>The <var>MaxBackupIndex</var> option determines how many backup
  70. * files are kept before the oldest is erased. This option takes
  71. * a positive integer value. If set to zero, then there will be no
  72. * backup files and the log file will be truncated when it reaches
  73. * MaxFileSize.</p>
  74. * <p>There is one backup file by default.</p>
  75. *
  76. * @var integer
  77. */
  78. protected $maxBackupIndex = 1;
  79. /**
  80. * @var string the filename expanded
  81. */
  82. private $expandedFileName = null;
  83. /**
  84. * Returns the value of the MaxBackupIndex option.
  85. * @return integer
  86. */
  87. private function getExpandedFileName() {
  88. return $this->expandedFileName;
  89. }
  90. /**
  91. * Get the maximum size that the output file is allowed to reach
  92. * before being rolled over to backup files.
  93. * @return integer
  94. */
  95. public function getMaximumFileSize() {
  96. return $this->maxFileSize;
  97. }
  98. /**
  99. * Implements the usual roll over behaviour.
  100. *
  101. * <p>If MaxBackupIndex is positive, then files File.1, ..., File.MaxBackupIndex -1 are renamed to File.2, ..., File.MaxBackupIndex.
  102. * Moreover, File is renamed File.1 and closed. A new File is created to receive further log output.
  103. *
  104. * <p>If MaxBackupIndex is equal to zero, then the File is truncated with no backup files created.
  105. *
  106. * Rollover must be called while the file is locked so that it is safe for concurrent access.
  107. */
  108. private function rollOver() {
  109. // If maxBackups <= 0, then there is no file renaming to be done.
  110. if($this->maxBackupIndex > 0) {
  111. $fileName = $this->getExpandedFileName();
  112. // Delete the oldest file, to keep Windows happy.
  113. $file = $fileName . '.' . $this->maxBackupIndex;
  114. if(is_writable($file))
  115. unlink($file);
  116. // Map {(maxBackupIndex - 1), ..., 2, 1} to {maxBackupIndex, ..., 3, 2}
  117. for($i = $this->maxBackupIndex - 1; $i >= 1; $i--) {
  118. $file = $fileName . "." . $i;
  119. if(is_readable($file)) {
  120. $target = $fileName . '.' . ($i + 1);
  121. rename($file, $target);
  122. }
  123. }
  124. // Backup the active file
  125. copy($fileName, "$fileName.1");
  126. }
  127. // Truncate the active file
  128. ftruncate($this->fp, 0);
  129. rewind($this->fp);
  130. }
  131. public function setFile($fileName) {
  132. $this->file = $fileName;
  133. // As LoggerAppenderFile does not create the directory, it has to exist.
  134. // realpath() fails if the argument does not exist so the filename is separated.
  135. $this->expandedFileName = realpath(dirname($fileName));
  136. if ($this->expandedFileName === false) throw new Exception("Directory of $fileName does not exist!");
  137. $this->expandedFileName .= DIRECTORY_SEPARATOR . basename($fileName);
  138. }
  139. /**
  140. * Set the maximum number of backup files to keep around.
  141. *
  142. * <p>The <b>MaxBackupIndex</b> option determines how many backup
  143. * files are kept before the oldest is erased. This option takes
  144. * a positive integer value. If set to zero, then there will be no
  145. * backup files and the log file will be truncated when it reaches
  146. * MaxFileSize.
  147. *
  148. * @param mixed $maxBackups
  149. */
  150. public function setMaxBackupIndex($maxBackups) {
  151. $this->setPositiveInteger('maxBackupIndex', $maxBackups);
  152. }
  153. /**
  154. * Set the maximum size that the output file is allowed to reach
  155. * before being rolled over to backup files.
  156. *
  157. * @param mixed $maxFileSize
  158. * @see setMaxFileSize()
  159. * @deprecated
  160. */
  161. public function setMaximumFileSize($maxFileSize) {
  162. return $this->setMaxFileSize($maxFileSize);
  163. }
  164. /**
  165. * Set the maximum size that the output file is allowed to reach
  166. * before being rolled over to backup files.
  167. * <p>In configuration files, the <b>maxFileSize</b> option takes an
  168. * long integer in the range 0 - 2^63. You can specify the value
  169. * with the suffixes "KB", "MB" or "GB" so that the integer is
  170. * interpreted being expressed respectively in kilobytes, megabytes
  171. * or gigabytes. For example, the value "10KB" will be interpreted
  172. * as 10240.
  173. *
  174. * @param mixed $value
  175. * @return the actual file size set
  176. */
  177. public function setMaxFileSize($value) {
  178. $this->setFileSize('maxFileSize', $value);
  179. }
  180. public function append(LoggerLoggingEvent $event) {
  181. if($this->fp and $this->layout !== null) {
  182. if(flock($this->fp, LOCK_EX)) {
  183. fwrite($this->fp, $this->layout->format($event));
  184. // Stats cache must be cleared, otherwise filesize() returns cached results
  185. clearstatcache();
  186. // Rollover if needed
  187. if (filesize($this->expandedFileName) > $this->maxFileSize) {
  188. $this->rollOver();
  189. }
  190. flock($this->fp, LOCK_UN);
  191. } else {
  192. $this->closed = true;
  193. }
  194. }
  195. }
  196. /**
  197. * @return Returns the maximum number of backup files to keep around.
  198. */
  199. public function getMaxBackupIndex() {
  200. return $this->maxBackupIndex;
  201. }
  202. /**
  203. * @return Returns the maximum size that the output file is allowed to reach
  204. * before being rolled over to backup files.
  205. */
  206. public function getMaxFileSize() {
  207. return $this->maxFileSize;
  208. }
  209. }