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