3 * log4php is a PHP port of the log4j java logging package.
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>
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>
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>
17 * @subpackage appenders
23 if (!defined('LOG4PHP_DIR')) define('LOG4PHP_DIR', dirname(__FILE__) . '/..');
25 require_once(LOG4PHP_DIR . '/LoggerAppenderSkeleton.php');
26 require_once(LOG4PHP_DIR . '/helpers/LoggerOptionConverter.php');
27 require_once(LOG4PHP_DIR . '/LoggerLog.php');
30 * FileAppender appends log events to a file.
32 * Parameters are ({@link $fileName} but option name is <b>file</b>),
35 * @author VxR <vxr@vxr.it>
36 * @version $Revision: 2 $
38 * @subpackage appenders
40 class LoggerAppenderFile extends LoggerAppenderSkeleton {
43 * @var boolean if {@link $file} exists, appends events.
48 * @var string the file name used to append events
53 * @var mixed file resource
61 var $requiresLayout = true;
66 * @param string $name appender name
68 function LoggerAppenderFile($name)
70 $this->LoggerAppenderSkeleton($name);
73 function activateOptions()
75 $fileName = $this->getFile();
76 LoggerLog::debug("LoggerAppenderFile::activateOptions() opening file '{$fileName}'");
77 $this->fp = @fopen($fileName, ($this->getAppend()? 'a':'w'));
79 if ($this->getAppend())
80 fseek($this->fp, 0, SEEK_END);
81 @fwrite($this->fp, $this->layout->getHeader());
82 $this->closed = false;
90 if ($this->fp and $this->layout !== null)
91 @fwrite($this->fp, $this->layout->getFooter());
98 * Closes the previously opened file.
111 return $this->append;
119 return $this->getFileName();
125 function getFileName()
127 return $this->fileName;
131 * Close any previously opened file and call the parent's reset.
136 $this->fileName = null;
140 function setAppend($flag)
142 $this->append = LoggerOptionConverter::toBoolean($flag, true);
146 * Sets and opens the file where the log output will go.
148 * This is an overloaded method. It can be called with:
149 * - setFile(string $fileName) to set filename.
150 * - setFile(string $fileName, boolean $append) to set filename and append.
154 $numargs = func_num_args();
155 $args = func_get_args();
157 if ($numargs == 1 and is_string($args[0])) {
158 $this->setFileName($args[0]);
159 } elseif ($numargs >=2 and is_string($args[0]) and is_bool($args[1])) {
160 $this->setFile($args[0]);
161 $this->setAppend($args[1]);
165 function setFileName($fileName)
167 $this->fileName = $fileName;
170 function append($event)
172 if ($this->fp and $this->layout !== null) {
174 LoggerLog::debug("LoggerAppenderFile::append()");
176 @fwrite($this->fp, $this->layout->format($event));