1 | <?php |
---|
2 | /** |
---|
3 | * Licensed to the Apache Software Foundation (ASF) under one or more |
---|
4 | * contributor license agreements. See the NOTICE file distributed with |
---|
5 | * this work for additional information regarding copyright ownership. |
---|
6 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
---|
7 | * (the "License"); you may not use this file except in compliance with |
---|
8 | * the License. You may obtain a copy of the License at |
---|
9 | * |
---|
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
11 | * |
---|
12 | * Unless required by applicable law or agreed to in writing, software |
---|
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
15 | * See the License for the specific language governing permissions and |
---|
16 | * limitations under the License. |
---|
17 | * |
---|
18 | * |
---|
19 | * @package log4php |
---|
20 | * @subpackage appenders |
---|
21 | */ |
---|
22 | |
---|
23 | /** |
---|
24 | * @ignore |
---|
25 | */ |
---|
26 | if (!defined('LOG4PHP_DIR')) define('LOG4PHP_DIR', dirname(__FILE__) . '/..'); |
---|
27 | |
---|
28 | require_once(LOG4PHP_DIR . '/LoggerAppenderSkeleton.php'); |
---|
29 | require_once(LOG4PHP_DIR . '/helpers/LoggerOptionConverter.php'); |
---|
30 | require_once(LOG4PHP_DIR . '/LoggerLog.php'); |
---|
31 | |
---|
32 | /** |
---|
33 | * FileAppender appends log events to a file. |
---|
34 | * |
---|
35 | * Parameters are ({@link $fileName} but option name is <b>file</b>), |
---|
36 | * {@link $append}. |
---|
37 | * |
---|
38 | * @author Marco Vassura |
---|
39 | * @author Knut Urdalen <knut.urdalen@gmail.com> |
---|
40 | * @version $Revision: 640255 $ |
---|
41 | * @package log4php |
---|
42 | * @subpackage appenders |
---|
43 | */ |
---|
44 | class LoggerAppenderFile extends LoggerAppenderSkeleton { |
---|
45 | |
---|
46 | /** |
---|
47 | * @var boolean if {@link $file} exists, appends events. |
---|
48 | */ |
---|
49 | private $append = true; |
---|
50 | /** |
---|
51 | * @var string the file name used to append events |
---|
52 | */ |
---|
53 | protected $fileName; |
---|
54 | /** |
---|
55 | * @var mixed file resource |
---|
56 | */ |
---|
57 | protected $fp = false; |
---|
58 | |
---|
59 | public function __construct($name) { |
---|
60 | parent::__construct($name); |
---|
61 | $this->requiresLayout = true; |
---|
62 | } |
---|
63 | |
---|
64 | public function activateOptions() { |
---|
65 | $fileName = $this->getFile(); |
---|
66 | LoggerLog::debug("LoggerAppenderFile::activateOptions() opening file '{$fileName}'"); |
---|
67 | $this->fp = fopen($fileName, ($this->getAppend()? 'a':'w')); |
---|
68 | if ($this->fp) { |
---|
69 | if ($this->getAppend()) |
---|
70 | fseek($this->fp, 0, SEEK_END); |
---|
71 | fwrite($this->fp, $this->layout->getHeader()); |
---|
72 | $this->closed = false; |
---|
73 | } else { |
---|
74 | $this->closed = true; |
---|
75 | } |
---|
76 | } |
---|
77 | |
---|
78 | public function close() { |
---|
79 | if($this->fp and $this->layout !== null) { |
---|
80 | fwrite($this->fp, $this->layout->getFooter()); |
---|
81 | } |
---|
82 | |
---|
83 | $this->closeFile(); |
---|
84 | $this->closed = true; |
---|
85 | } |
---|
86 | |
---|
87 | /** |
---|
88 | * Closes the previously opened file. |
---|
89 | */ |
---|
90 | public function closeFile() { |
---|
91 | if ($this->fp) |
---|
92 | fclose($this->fp); |
---|
93 | } |
---|
94 | |
---|
95 | /** |
---|
96 | * @return boolean |
---|
97 | */ |
---|
98 | public function getAppend() { |
---|
99 | return $this->append; |
---|
100 | } |
---|
101 | |
---|
102 | /** |
---|
103 | * @return string |
---|
104 | */ |
---|
105 | public function getFile() { |
---|
106 | return $this->getFileName(); |
---|
107 | } |
---|
108 | |
---|
109 | /** |
---|
110 | * @return string |
---|
111 | */ |
---|
112 | public function getFileName() { |
---|
113 | return $this->fileName; |
---|
114 | } |
---|
115 | |
---|
116 | /** |
---|
117 | * Close any previously opened file and call the parent's reset. |
---|
118 | */ |
---|
119 | public function reset() { |
---|
120 | $this->closeFile(); |
---|
121 | $this->fileName = null; |
---|
122 | parent::reset(); |
---|
123 | } |
---|
124 | |
---|
125 | public function setAppend($flag) { |
---|
126 | $this->append = LoggerOptionConverter::toBoolean($flag, true); |
---|
127 | } |
---|
128 | |
---|
129 | /** |
---|
130 | * Sets and opens the file where the log output will go. |
---|
131 | * |
---|
132 | * This is an overloaded method. It can be called with: |
---|
133 | * - setFile(string $fileName) to set filename. |
---|
134 | * - setFile(string $fileName, boolean $append) to set filename and append. |
---|
135 | */ |
---|
136 | public function setFile() { |
---|
137 | $numargs = func_num_args(); |
---|
138 | $args = func_get_args(); |
---|
139 | |
---|
140 | if ($numargs == 1 and is_string($args[0])) { |
---|
141 | $this->setFileName($args[0]); |
---|
142 | } elseif ($numargs >=2 and is_string($args[0]) and is_bool($args[1])) { |
---|
143 | $this->setFile($args[0]); |
---|
144 | $this->setAppend($args[1]); |
---|
145 | } |
---|
146 | } |
---|
147 | |
---|
148 | public function setFileName($fileName) { |
---|
149 | $this->fileName = $fileName; |
---|
150 | } |
---|
151 | |
---|
152 | public function append($event) { |
---|
153 | if ($this->fp and $this->layout !== null) { |
---|
154 | LoggerLog::debug("LoggerAppenderFile::append()"); |
---|
155 | fwrite($this->fp, $this->layout->format($event)); |
---|
156 | } |
---|
157 | } |
---|
158 | } |
---|