KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log4j > RollingFileAppender


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17
18
19 package org.apache.log4j;
20
21 import java.io.IOException JavaDoc;
22 import java.io.Writer JavaDoc;
23 import java.io.File JavaDoc;
24 import org.apache.log4j.helpers.OptionConverter;
25 import org.apache.log4j.helpers.LogLog;
26 import org.apache.log4j.helpers.CountingQuietWriter;
27 import org.apache.log4j.spi.LoggingEvent;
28
29 /**
30    RollingFileAppender extends FileAppender to backup the log files when
31    they reach a certain size.
32
33    @author Heinz Richter
34    @author Ceki Gülcü
35
36 */

37 public class RollingFileAppender extends FileAppender {
38
39   /**
40      The default maximum file size is 10MB.
41   */

42   protected long maxFileSize = 10*1024*1024;
43
44   /**
45      There is one backup file by default.
46    */

47   protected int maxBackupIndex = 1;
48
49   /**
50      The default constructor simply calls its {@link
51      FileAppender#FileAppender parents constructor}. */

52   public
53   RollingFileAppender() {
54     super();
55   }
56
57   /**
58     Instantiate a RollingFileAppender and open the file designated by
59     <code>filename</code>. The opened filename will become the ouput
60     destination for this appender.
61
62     <p>If the <code>append</code> parameter is true, the file will be
63     appended to. Otherwise, the file desginated by
64     <code>filename</code> will be truncated before being opened.
65   */

66   public
67   RollingFileAppender(Layout layout, String JavaDoc filename, boolean append)
68                                       throws IOException JavaDoc {
69     super(layout, filename, append);
70   }
71
72   /**
73      Instantiate a FileAppender and open the file designated by
74     <code>filename</code>. The opened filename will become the output
75     destination for this appender.
76
77     <p>The file will be appended to. */

78   public
79   RollingFileAppender(Layout layout, String JavaDoc filename) throws IOException JavaDoc {
80     super(layout, filename);
81   }
82
83   /**
84      Returns the value of the <b>MaxBackupIndex</b> option.
85    */

86   public
87   int getMaxBackupIndex() {
88     return maxBackupIndex;
89   }
90
91  /**
92     Get the maximum size that the output file is allowed to reach
93     before being rolled over to backup files.
94
95     @since 1.1
96  */

97   public
98   long getMaximumFileSize() {
99     return maxFileSize;
100   }
101
102   /**
103      Implements the usual roll over behaviour.
104
105      <p>If <code>MaxBackupIndex</code> is positive, then files
106      {<code>File.1</code>, ..., <code>File.MaxBackupIndex -1</code>}
107      are renamed to {<code>File.2</code>, ...,
108      <code>File.MaxBackupIndex</code>}. Moreover, <code>File</code> is
109      renamed <code>File.1</code> and closed. A new <code>File</code> is
110      created to receive further log output.
111
112      <p>If <code>MaxBackupIndex</code> is equal to zero, then the
113      <code>File</code> is truncated with no backup files created.
114
115    */

116   public // synchronization not necessary since doAppend is alreasy synched
117
void rollOver() {
118     File JavaDoc target;
119     File JavaDoc file;
120
121     LogLog.debug("rolling over count=" + ((CountingQuietWriter) qw).getCount());
122     LogLog.debug("maxBackupIndex="+maxBackupIndex);
123
124     // If maxBackups <= 0, then there is no file renaming to be done.
125
if(maxBackupIndex > 0) {
126       // Delete the oldest file, to keep Windows happy.
127
file = new File JavaDoc(fileName + '.' + maxBackupIndex);
128       if (file.exists())
129        file.delete();
130
131       // Map {(maxBackupIndex - 1), ..., 2, 1} to {maxBackupIndex, ..., 3, 2}
132
for (int i = maxBackupIndex - 1; i >= 1; i--) {
133     file = new File JavaDoc(fileName + "." + i);
134     if (file.exists()) {
135       target = new File JavaDoc(fileName + '.' + (i + 1));
136       LogLog.debug("Renaming file " + file + " to " + target);
137       file.renameTo(target);
138     }
139       }
140
141       // Rename fileName to fileName.1
142
target = new File JavaDoc(fileName + "." + 1);
143
144       this.closeFile(); // keep windows happy.
145

146       file = new File JavaDoc(fileName);
147       LogLog.debug("Renaming file " + file + " to " + target);
148       file.renameTo(target);
149     }
150
151     try {
152       // This will also close the file. This is OK since multiple
153
// close operations are safe.
154
this.setFile(fileName, false, bufferedIO, bufferSize);
155     }
156     catch(IOException JavaDoc e) {
157       LogLog.error("setFile("+fileName+", false) call failed.", e);
158     }
159   }
160
161   public
162   synchronized
163   void setFile(String JavaDoc fileName, boolean append, boolean bufferedIO, int bufferSize)
164                                                                  throws IOException JavaDoc {
165     super.setFile(fileName, append, this.bufferedIO, this.bufferSize);
166     if(append) {
167       File JavaDoc f = new File JavaDoc(fileName);
168       ((CountingQuietWriter) qw).setCount(f.length());
169     }
170   }
171
172
173   /**
174      Set the maximum number of backup files to keep around.
175
176      <p>The <b>MaxBackupIndex</b> option determines how many backup
177      files are kept before the oldest is erased. This option takes
178      a positive integer value. If set to zero, then there will be no
179      backup files and the log file will be truncated when it reaches
180      <code>MaxFileSize</code>.
181    */

182   public
183   void setMaxBackupIndex(int maxBackups) {
184     this.maxBackupIndex = maxBackups;
185   }
186
187   /**
188      Set the maximum size that the output file is allowed to reach
189      before being rolled over to backup files.
190
191      <p>This method is equivalent to {@link #setMaxFileSize} except
192      that it is required for differentiating the setter taking a
193      <code>long</code> argument from the setter taking a
194      <code>String</code> argument by the JavaBeans {@link
195      java.beans.Introspector Introspector}.
196
197      @see #setMaxFileSize(String)
198  */

199   public
200   void setMaximumFileSize(long maxFileSize) {
201     this.maxFileSize = maxFileSize;
202   }
203
204
205   /**
206      Set the maximum size that the output file is allowed to reach
207      before being rolled over to backup files.
208
209      <p>In configuration files, the <b>MaxFileSize</b> option takes an
210      long integer in the range 0 - 2^63. You can specify the value
211      with the suffixes "KB", "MB" or "GB" so that the integer is
212      interpreted being expressed respectively in kilobytes, megabytes
213      or gigabytes. For example, the value "10KB" will be interpreted
214      as 10240.
215    */

216   public
217   void setMaxFileSize(String JavaDoc value) {
218     maxFileSize = OptionConverter.toFileSize(value, maxFileSize + 1);
219   }
220
221   protected
222   void setQWForFiles(Writer JavaDoc writer) {
223      this.qw = new CountingQuietWriter(writer, errorHandler);
224   }
225
226   /**
227      This method differentiates RollingFileAppender from its super
228      class.
229
230      @since 0.9.0
231   */

232   protected
233   void subAppend(LoggingEvent event) {
234     super.subAppend(event);
235     if((fileName != null) &&
236                      ((CountingQuietWriter) qw).getCount() >= maxFileSize)
237       this.rollOver();
238    }
239 }
240
Popular Tags