LoggerAppenderFile.php
4.34 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
<?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.
*
*
* @package log4php
* @subpackage appenders
*/
/**
* @ignore
*/
if (!defined('LOG4PHP_DIR')) define('LOG4PHP_DIR', dirname(__FILE__) . '/..');
require_once(LOG4PHP_DIR . '/LoggerAppenderSkeleton.php');
require_once(LOG4PHP_DIR . '/helpers/LoggerOptionConverter.php');
require_once(LOG4PHP_DIR . '/LoggerLog.php');
/**
* FileAppender appends log events to a file.
*
* Parameters are ({@link $fileName} but option name is <b>file</b>),
* {@link $append}.
*
* @author Marco Vassura
* @author Knut Urdalen <knut.urdalen@gmail.com>
* @version $Revision: 640255 $
* @package log4php
* @subpackage appenders
*/
class LoggerAppenderFile extends LoggerAppenderSkeleton {
/**
* @var boolean if {@link $file} exists, appends events.
*/
private $append = true;
/**
* @var string the file name used to append events
*/
protected $fileName;
/**
* @var mixed file resource
*/
protected $fp = false;
public function __construct($name) {
parent::__construct($name);
$this->requiresLayout = true;
}
public function activateOptions() {
$fileName = $this->getFile();
LoggerLog::debug("LoggerAppenderFile::activateOptions() opening file '{$fileName}'");
$this->fp = fopen($fileName, ($this->getAppend()? 'a':'w'));
if ($this->fp) {
if ($this->getAppend())
fseek($this->fp, 0, SEEK_END);
fwrite($this->fp, $this->layout->getHeader());
$this->closed = false;
} else {
$this->closed = true;
}
}
public function close() {
if($this->fp and $this->layout !== null) {
fwrite($this->fp, $this->layout->getFooter());
}
$this->closeFile();
$this->closed = true;
}
/**
* Closes the previously opened file.
*/
public function closeFile() {
if ($this->fp)
fclose($this->fp);
}
/**
* @return boolean
*/
public function getAppend() {
return $this->append;
}
/**
* @return string
*/
public function getFile() {
return $this->getFileName();
}
/**
* @return string
*/
public function getFileName() {
return $this->fileName;
}
/**
* Close any previously opened file and call the parent's reset.
*/
public function reset() {
$this->closeFile();
$this->fileName = null;
parent::reset();
}
public function setAppend($flag) {
$this->append = LoggerOptionConverter::toBoolean($flag, true);
}
/**
* Sets and opens the file where the log output will go.
*
* This is an overloaded method. It can be called with:
* - setFile(string $fileName) to set filename.
* - setFile(string $fileName, boolean $append) to set filename and append.
*/
public function setFile() {
$numargs = func_num_args();
$args = func_get_args();
if ($numargs == 1 and is_string($args[0])) {
$this->setFileName($args[0]);
} elseif ($numargs >=2 and is_string($args[0]) and is_bool($args[1])) {
$this->setFile($args[0]);
$this->setAppend($args[1]);
}
}
public function setFileName($fileName) {
$this->fileName = $fileName;
}
public function append($event) {
if ($this->fp and $this->layout !== null) {
LoggerLog::debug("LoggerAppenderFile::append()");
fwrite($this->fp, $this->layout->format($event));
}
}
}