LoggerAppenderFile.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 . '/LoggerAppenderSkeleton.php');
  24. require_once(LOG4PHP_DIR . '/helpers/LoggerOptionConverter.php');
  25. require_once(LOG4PHP_DIR . '/LoggerLog.php');
  26. /**
  27. * FileAppender appends log events to a file.
  28. *
  29. * Parameters are ({@link $fileName} but option name is <b>file</b>),
  30. * {@link $append}.
  31. *
  32. * @author VxR <vxr@vxr.it>
  33. * @version $Revision: 1.1 $
  34. * @package log4php
  35. * @subpackage appenders
  36. */
  37. class LoggerAppenderFile extends LoggerAppenderSkeleton {
  38. /**
  39. * @var boolean if {@link $file} exists, appends events.
  40. */
  41. var $append = true;
  42. /**
  43. * @var string the file name used to append events
  44. */
  45. var $fileName;
  46. /**
  47. * @var mixed file resource
  48. * @access private
  49. */
  50. var $fp = false;
  51. /**
  52. * @access private
  53. */
  54. var $requiresLayout = true;
  55. /**
  56. * Constructor.
  57. *
  58. * @param string $name appender name
  59. */
  60. function LoggerAppenderFile($name)
  61. {
  62. $this->LoggerAppenderSkeleton($name);
  63. }
  64. function activateOptions()
  65. {
  66. $fileName = $this->getFile();
  67. LoggerLog::debug("LoggerAppenderFile::activateOptions() opening file '{$fileName}'");
  68. $this->fp = @fopen($fileName, ($this->getAppend()? 'a':'w'));
  69. if ($this->fp) {
  70. if ($this->getAppend())
  71. fseek($this->fp, 0, SEEK_END);
  72. @fwrite($this->fp, $this->layout->getHeader());
  73. $this->closed = false;
  74. } else {
  75. $this->closed = true;
  76. }
  77. }
  78. function close()
  79. {
  80. if ($this->fp and $this->layout !== null)
  81. @fwrite($this->fp, $this->layout->getFooter());
  82. $this->closeFile();
  83. $this->closed = true;
  84. }
  85. /**
  86. * Closes the previously opened file.
  87. */
  88. function closeFile()
  89. {
  90. if ($this->fp)
  91. @fclose($this->fp);
  92. }
  93. /**
  94. * @return boolean
  95. */
  96. function getAppend()
  97. {
  98. return $this->append;
  99. }
  100. /**
  101. * @return string
  102. */
  103. function getFile()
  104. {
  105. return $this->getFileName();
  106. }
  107. /**
  108. * @return string
  109. */
  110. function getFileName()
  111. {
  112. return $this->fileName;
  113. }
  114. /**
  115. * Close any previously opened file and call the parent's reset.
  116. */
  117. function reset()
  118. {
  119. $this->closeFile();
  120. $this->fileName = null;
  121. parent::reset();
  122. }
  123. function setAppend($flag)
  124. {
  125. $this->append = LoggerOptionConverter::toBoolean($flag, true);
  126. }
  127. /**
  128. * Sets and opens the file where the log output will go.
  129. *
  130. * This is an overloaded method. It can be called with:
  131. * - setFile(string $fileName) to set filename.
  132. * - setFile(string $fileName, boolean $append) to set filename and append.
  133. */
  134. function setFile()
  135. {
  136. $numargs = func_num_args();
  137. $args = func_get_args();
  138. if ($numargs == 1 and is_string($args[0])) {
  139. $this->setFileName($args[0]);
  140. } elseif ($numargs >=2 and is_string($args[0]) and is_bool($args[1])) {
  141. $this->setFile($args[0]);
  142. $this->setAppend($args[1]);
  143. }
  144. }
  145. function setFileName($fileName)
  146. {
  147. $this->fileName = $fileName;
  148. }
  149. function append($event)
  150. {
  151. if ($this->fp and $this->layout !== null) {
  152. LoggerLog::debug("LoggerAppenderFile::append()");
  153. @fwrite($this->fp, $this->layout->format($event));
  154. }
  155. }
  156. }
  157. ?>