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 : * @package log4php
19 : */
20 :
21 : /**
22 : * FileAppender appends log events to a file.
23 : *
24 : * This appender uses a layout.
25 : *
26 : * Configurable parameters for this appender are:
27 : * - file - The target file to write to
28 : * - filename - The target file to write to (deprecated, use "file" instead)
29 : * - append - Sets if the appender should append to the end of the file or overwrite content ("true" or "false")
30 : *
31 : * An example php file:
32 : *
33 : * {@example ../../examples/php/appender_file.php 19}
34 : *
35 : * An example configuration file:
36 : *
37 : * {@example ../../examples/resources/appender_file.properties 18}
38 : *
39 : * @version $Revision: 1213663 $
40 : * @package log4php
41 : * @subpackage appenders
42 : */
43 : class LoggerAppenderFile extends LoggerAppender {
44 :
45 : /**
46 : * @var boolean if {@link $file} exists, appends events.
47 : */
48 : protected $append = true;
49 :
50 : /**
51 : * @var string the file name used to append events
52 : */
53 : protected $file;
54 :
55 : /**
56 : * @var mixed file resource
57 : */
58 : protected $fp = false;
59 :
60 : public function activateOptions() {
61 5 : $fileName = $this->getFile();
62 :
63 5 : if (empty($fileName)) {
64 0 : $this->warn("Required parameter 'fileName' not set. Closing appender.");
65 0 : $this->closed = true;
66 0 : return;
67 : }
68 :
69 5 : if(!is_file($fileName)) {
70 5 : $dir = dirname($fileName);
71 5 : if(!is_dir($dir)) {
72 0 : mkdir($dir, 0777, true);
73 0 : }
74 5 : }
75 :
76 5 : $this->fp = fopen($fileName, ($this->getAppend()? 'a':'w'));
77 5 : if($this->fp) {
78 5 : if(flock($this->fp, LOCK_EX)) {
79 5 : if($this->getAppend()) {
80 4 : fseek($this->fp, 0, SEEK_END);
81 4 : }
82 5 : fwrite($this->fp, $this->layout->getHeader());
83 5 : flock($this->fp, LOCK_UN);
84 5 : $this->closed = false;
85 5 : } else {
86 : // TODO: should we take some action in this case?
87 0 : $this->closed = true;
88 : }
89 5 : } else {
90 0 : $this->closed = true;
91 : }
92 5 : }
93 :
94 : public function close() {
95 6 : if($this->closed != true) {
96 6 : if($this->fp and $this->layout !== null) {
97 6 : if(flock($this->fp, LOCK_EX)) {
98 6 : fwrite($this->fp, $this->layout->getFooter());
99 6 : flock($this->fp, LOCK_UN);
100 6 : }
101 6 : fclose($this->fp);
102 6 : }
103 6 : $this->closed = true;
104 6 : }
105 6 : }
106 :
107 : public function append(LoggerLoggingEvent $event) {
108 5 : if($this->fp and $this->layout !== null) {
109 5 : if(flock($this->fp, LOCK_EX)) {
110 5 : fwrite($this->fp, $this->layout->format($event));
111 5 : flock($this->fp, LOCK_UN);
112 5 : } else {
113 0 : $this->closed = true;
114 : }
115 5 : }
116 5 : }
117 :
118 : /**
119 : * Sets the file where the log output will go.
120 : * @param string $file
121 : */
122 : public function setFile($file) {
123 5 : $this->setString('file', $file);
124 5 : }
125 :
126 : /**
127 : * @return string
128 : */
129 : public function getFile() {
130 7 : return $this->file;
131 : }
132 :
133 : /**
134 : * @return boolean
135 : */
136 : public function getAppend() {
137 7 : return $this->append;
138 : }
139 :
140 : public function setAppend($append) {
141 2 : $this->setBoolean('append', $append);
142 2 : }
143 :
144 : /**
145 : * Sets the file where the log output will go.
146 : * @param string $fileName
147 : * @deprecated Use setFile() instead.
148 : */
149 : public function setFileName($fileName) {
150 0 : $this->setFile($fileName);
151 0 : }
152 :
153 : /**
154 : * @return string
155 : * @deprecated Use getFile() instead.
156 : */
157 : public function getFileName() {
158 0 : return $this->getFile();
159 : }
160 :
161 :
162 : }
|