LoggerAppenderFile.php
5.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<?php
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* LoggerAppenderFile appends log events to a file.
*
* This appender uses a layout.
*
* ## Configurable parameters: ##
*
* - **file** - Path to the target file. Relative paths are resolved based on
* the working directory.
* - **append** - If set to true, the appender will append to the file,
* otherwise the file contents will be overwritten.
*
* @version $Revision: 1382274 $
* @package log4php
* @subpackage appenders
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
* @link http://logging.apache.org/log4php/docs/appenders/file.html Appender documentation
*/
class LoggerAppenderFile extends LoggerAppender {
/**
* If set to true, the file is locked before appending. This allows
* concurrent access. However, appending without locking is faster so
* it should be used where appropriate.
*
* TODO: make this a configurable parameter
*
* @var boolean
*/
protected $locking = true;
/**
* If set to true, appends to file. Otherwise overwrites it.
* @var boolean
*/
protected $append = true;
/**
* Path to the target file.
* @var string
*/
protected $file;
/**
* The file resource.
* @var resource
*/
protected $fp;
/**
* Helper function which can be easily overriden by daily file appender.
*/
protected function getTargetFile() {
return $this->file;
}
/**
* Acquires the target file resource, creates the destination folder if
* necessary. Writes layout header to file.
*
* @return boolean FALSE if opening failed
*/
protected function openFile() {
$file = $this->getTargetFile();
// Create the target folder if needed
if(!is_file($file)) {
$dir = dirname($file);
if(!is_dir($dir)) {
$success = mkdir($dir, 0777, true);
if ($success === false) {
$this->warn("Failed creating target directory [$dir]. Closing appender.");
$this->closed = true;
return false;
}
}
}
$mode = $this->append ? 'a' : 'w';
$this->fp = fopen($file, $mode);
if ($this->fp === false) {
$this->warn("Failed opening target file. Closing appender.");
$this->fp = null;
$this->closed = true;
return false;
}
// Required when appending with concurrent access
if($this->append) {
fseek($this->fp, 0, SEEK_END);
}
// Write the header
$this->write($this->layout->getHeader());
}
/**
* Writes a string to the target file. Opens file if not already open.
* @param string $string Data to write.
*/
protected function write($string) {
// Lazy file open
if(!isset($this->fp)) {
if ($this->openFile() === false) {
return; // Do not write if file open failed.
}
}
if ($this->locking) {
$this->writeWithLocking($string);
} else {
$this->writeWithoutLocking($string);
}
}
protected function writeWithLocking($string) {
if(flock($this->fp, LOCK_EX)) {
if(fwrite($this->fp, $string) === false) {
$this->warn("Failed writing to file. Closing appender.");
$this->closed = true;
}
flock($this->fp, LOCK_UN);
} else {
$this->warn("Failed locking file for writing. Closing appender.");
$this->closed = true;
}
}
protected function writeWithoutLocking($string) {
if(fwrite($this->fp, $string) === false) {
$this->warn("Failed writing to file. Closing appender.");
$this->closed = true;
}
}
public function activateOptions() {
if (empty($this->file)) {
$this->warn("Required parameter 'file' not set. Closing appender.");
$this->closed = true;
return;
}
}
public function close() {
if (is_resource($this->fp)) {
$this->write($this->layout->getFooter());
fclose($this->fp);
}
$this->fp = null;
$this->closed = true;
}
public function append(LoggerLoggingEvent $event) {
$this->write($this->layout->format($event));
}
/**
* Sets the 'file' parameter.
* @param string $file
*/
public function setFile($file) {
$this->setString('file', $file);
}
/**
* Returns the 'file' parameter.
* @return string
*/
public function getFile() {
return $this->file;
}
/**
* Returns the 'append' parameter.
* @return boolean
*/
public function getAppend() {
return $this->append;
}
/**
* Sets the 'append' parameter.
* @param boolean $append
*/
public function setAppend($append) {
$this->setBoolean('append', $append);
}
/**
* Sets the 'file' parmeter. Left for legacy reasons.
* @param string $fileName
* @deprecated Use setFile() instead.
*/
public function setFileName($fileName) {
$this->setFile($fileName);
}
/**
* Returns the 'file' parmeter. Left for legacy reasons.
* @return string
* @deprecated Use getFile() instead.
*/
public function getFileName() {
return $this->getFile();
}
}