KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > logging > jdk > handlers > FileHandler


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 package org.jboss.logging.jdk.handlers;
17
18 import java.io.IOException JavaDoc;
19 import java.io.FileOutputStream JavaDoc;
20 import java.io.FileNotFoundException JavaDoc;
21 import java.io.File JavaDoc;
22 import java.io.Writer JavaDoc;
23 import java.io.BufferedWriter JavaDoc;
24 import java.util.logging.Formatter JavaDoc;
25 import java.util.logging.ErrorManager JavaDoc;
26
27 /**
28  * FileAppender appends log events to a file.
29  *
30  * @author Ceki Gülcü
31  * @author Scott.Stark@jboss.org
32  * @version $Revision: 1958 $
33  */

34 public class FileHandler extends WriterHandler
35 {
36    /**
37     * Controls file truncatation. The default value for this variable
38     * is <code>true</code>, meaning that by default a
39     * <code>FileAppender</code> will append to an existing file and not
40     * truncate it.
41     * <p/>
42     * <p>This option is meaningful only if the FileAppender opens the
43     * file.
44     */

45    protected boolean fileAppend = true;
46
47    /**
48     * The name of the log file.
49     */

50    protected String JavaDoc fileName = null;
51
52    /**
53     * The default constructor does not do anything.
54     */

55    public FileHandler()
56    {
57    }
58
59    /**
60     * Instantiate a <code>FileHandler</code> and open the file
61     * designated by <code>filename</code>. The opened filename will
62     * become the output destination for this appender.
63     * <p/>
64     * <p>If the <code>append</code> parameter is true, the file will be
65     * appended to. Otherwise, the file designated by
66     * <code>filename</code> will be truncated before being opened.
67     * <p/>
68     * <p>If the <code>bufferedIO</code> parameter is <code>true</code>,
69     * then buffered IO will be used to write to the output file.
70     */

71    public FileHandler(Formatter JavaDoc layout, String JavaDoc filename, boolean append, boolean bufferedIO,
72       int bufferSize)
73       throws IOException JavaDoc
74    {
75       super.setFormatter(layout);
76       this.setFile(filename, append, bufferedIO, bufferSize);
77    }
78
79    /**
80     * Instantiate a FileHandler and open the file designated by
81     * <code>filename</code>. The opened filename will become the output
82     * destination for this appender.
83     * <p/>
84     * <p>If the <code>append</code> parameter is true, the file will be
85     * appended to. Otherwise, the file designated by
86     * <code>filename</code> will be truncated before being opened.
87     */

88    public FileHandler(Formatter JavaDoc layout, String JavaDoc filename, boolean append)
89       throws IOException JavaDoc
90    {
91       this(layout, filename, append, true, 2048);
92    }
93
94    /**
95     * Instantiate a FileHandler and open the file designated by
96     * <code>filename</code>. The opened filename will become the output
97     * destination for this appender.
98     * <p/>
99     * <p>The file will be appended to.
100     */

101    public FileHandler(Formatter JavaDoc layout, String JavaDoc filename) throws IOException JavaDoc
102    {
103       this(layout, filename, true);
104    }
105
106    /**
107     * The <b>File</b> property takes a string value which should be the
108     * name of the file to append to.
109     * <p/>
110     * <p>Note: Actual opening of the file is made when {@link
111     * #activateOptions} is called, not when the options are set.
112     */

113    public void setFile(String JavaDoc file)
114    {
115       // Trim spaces from both ends. The users probably does not want
116
// trailing spaces in file names.
117
String JavaDoc val = file.trim();
118       fileName = val;
119    }
120
121    /**
122     * Returns the value of the <b>Append</b> option.
123     */

124    public boolean getAppend()
125    {
126       return fileAppend;
127    }
128
129
130    /**
131     * Returns the value of the <b>File</b> option.
132     */

133    public String JavaDoc getFile()
134    {
135       return fileName;
136    }
137
138    /**
139     * If the value of <b>File</b> is not <code>null</code>, then {@link
140     * #setFile} is called with the values of <b>File</b> and
141     * <b>Append</b> properties.
142     *
143     */

144    public void activateOptions()
145    {
146       if (fileName != null)
147       {
148          try
149          {
150             setFile(fileName, fileAppend, bufferedIO, bufferSize);
151          }
152          catch (java.io.IOException JavaDoc e)
153          {
154             reportError("setFile(" + fileName + "," + fileAppend + ") call failed.",
155                e, ErrorManager.OPEN_FAILURE);
156          }
157       }
158       else
159       {
160          reportError("File option not set for appender [" + name + "]."
161             +" Are you using FileHandler instead of ConsoleAppender?",
162             null, ErrorManager.OPEN_FAILURE);
163       }
164    }
165
166    /**
167     * The <b>Append</b> option takes a boolean value. It is set to
168     * <code>true</code> by default. If true, then <code>File</code>
169     * will be opened in append mode by {@link #setFile setFile} (see
170     * above). Otherwise, {@link #setFile setFile} will open
171     * <code>File</code> in truncate mode.
172     * <p/>
173     * <p>Note: Actual opening of the file is made when {@link
174     * #activateOptions} is called, not when the options are set.
175     */

176    public void setAppend(boolean flag)
177    {
178       fileAppend = flag;
179    }
180
181    /**
182     * <p>Sets and <i>opens</i> the file where the log output will
183     * go. The specified file must be writable.
184     * <p/>
185     * <p>If there was already an opened file, then the previous file
186     * is closed first.
187     * <p/>
188     * <p><b>Do not use this method directly. To configure a FileHandler
189     * or one of its subclasses, set its properties one by one and then
190     * call activateOptions.</b>
191     *
192     * @param fileName The path to the log file.
193     * @param append If true will append to fileName. Otherwise will
194     * truncate fileName.
195     */

196    public synchronized void setFile(String JavaDoc fileName, boolean append, boolean bufferedIO, int bufferSize)
197       throws IOException JavaDoc
198    {
199 // reportError("setFile called: " + fileName + ", " + append, null, ErrorManager.GENERIC_FAILURE);
200
super.setBufferedIO(bufferedIO);
201       super.setBufferSize(bufferSize);
202       FileOutputStream JavaDoc ostream = null;
203       try
204       {
205          // attempt to create file
206
ostream = new FileOutputStream JavaDoc(fileName, append);
207       }
208       catch (FileNotFoundException JavaDoc ex)
209       {
210          // if parent directory does not exist then
211
// attempt to create it and try to create file
212
String JavaDoc parentName = new File JavaDoc(fileName).getParent();
213          if (parentName != null)
214          {
215             File JavaDoc parentDir = new File JavaDoc(parentName);
216             if (!parentDir.exists() && parentDir.mkdirs())
217             {
218                ostream = new FileOutputStream JavaDoc(fileName, append);
219             }
220             else
221             {
222                throw ex;
223             }
224          }
225          else
226          {
227             throw ex;
228          }
229       }
230       super.setOutputStream(ostream);
231       this.fileName = fileName;
232       this.fileAppend = append;
233       // LogLog.debug("setFile ended");
234
}
235
236 }
237
238
Popular Tags