KickJava   Java API By Example, From Geeks To Geeks.

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


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.apache.log4j;
17  
18 import java.io.File JavaDoc;
19 import java.io.Writer JavaDoc;
20 import java.io.FileWriter JavaDoc;
21 import java.io.BufferedWriter JavaDoc;
22 import org.apache.log4j.spi.LoggingEvent;
23 import org.apache.log4j.helpers.OptionConverter;
24 import org.apache.log4j.spi.ErrorHandler;
25
26 /**
27    TempFileAppender creates new unique file for each logging statement.
28  
29    @author <a HREF="mailto:leos.literak@12snap.com">Leos Literak</a>
30    @author Ceki G&uuml;lc&uuml;
31  
32 */

33 public class TempFileAppender extends AppenderSkeleton {
34  
35   /**
36      A string constant used in naming the option for setting the
37      directory where the log files will be created. Current value
38      of this string constant is <b>Path</b>. java.io.tmpdir directory
39      will be used, if ommited.
40    */

41   static final public String JavaDoc PATH_OPTION = "Path";
42   
43   /**
44      The default path is actual directory.
45   */

46   protected String JavaDoc path = null;
47  
48   /**
49      A string constant used in naming the option for setting the
50      prefix of the log files. It has to have at least 3 characters!
51      Current value of this string constant is <b>Prefix</b>.
52    */

53   static final public String JavaDoc PREFIX_OPTION = "Prefix";
54   
55   /**
56      The default path is actual directory.
57   */

58   protected String JavaDoc prefix = "l4j_";
59  
60   /**
61      A string constant used in naming the option for setting the
62      suffix of the log files. Current value of this string constant
63      is <b>Suffix</b>.
64    */

65   static final public String JavaDoc SUFFIX_OPTION = "Suffix";
66   
67   /**
68      The default path is actual directory.
69   */

70   protected String JavaDoc suffix = ".tmp";
71   
72   /**
73      Default dir
74   */

75   
76   protected File JavaDoc dir = null;
77
78
79
80
81   /**
82      The default constructor simply calls its parent's constructor.
83   */

84   public TempFileAppender() {
85       super();
86   }
87  
88   /**
89      Retuns the option names for this component
90   */

91   public String JavaDoc[] getOptionStrings() {
92       return OptionConverter.concatanateArrays(super.getOptionStrings(),
93                  new String JavaDoc[] {PATH_OPTION,PREFIX_OPTION,SUFFIX_OPTION});
94   }
95
96   /**
97      Set TempFileAppender specific options.
98  
99      The recognized options are <b>Path</b>, <b>Prefix</b> and <b>Suffix</b>,
100      i.e. the values of the string constants {@link #PATH_OPTION},
101      {@link #PREFIX_OPTION} and respectively {@link #SUFFIX_OPTION}.
102      The options of the super class {@link AppenderSkeleton} are also
103      recognized.
104   */

105   
106   public void setOption(String JavaDoc key, String JavaDoc value) {
107       super.setOption(key, value);
108       if(key.equalsIgnoreCase(PATH_OPTION)) {
109       path = value;
110       if(path==null) {
111               errorHandler.error("Path cannot be empty!",null,0);
112       }
113
114       dir = new File JavaDoc(path);
115       if(!(dir.exists() && dir.isDirectory() && dir.canWrite())) {
116               errorHandler.error("Cannot write to directory " + path + "!",null,0);
117       }
118       }
119       else if(key.equalsIgnoreCase(PREFIX_OPTION)) {
120           if(value!=null && value.length()>=3) {
121           prefix = value;
122       } else {
123               errorHandler.error("Prefix cannot be shorter than 3 characters!",
124                              null,0);
125       }
126       }
127       else if(key.equalsIgnoreCase(SUFFIX_OPTION)) {
128           if(value!=null && value.length()>=1) {
129           suffix = value;
130       } else {
131               errorHandler.error("Suffix cannot be empty!",null,0);
132       }
133       }
134   }
135
136   /**
137      This method is called by {@link AppenderSkeleton#doAppend}
138      method.
139  
140      <p>Whenever this method is called, new unique file is created
141      with specified prefix and suffix. The file is closed afterwards.
142  
143      <p>The format of the output will depend on this appender's
144      layout.
145  
146   */

147   public void append(LoggingEvent event) {
148       if(!checkEntryConditions()) {
149           return;
150       }
151       subAppend(event);
152   }
153  
154   /**
155      This method determines if there is a sense in attempting to append.
156   */

157   protected boolean checkEntryConditions() {
158       return true;
159   }
160
161   /**
162      This method does actual writing
163   */

164   protected void subAppend(LoggingEvent event) {
165       try {
166           File JavaDoc tmp = File.createTempFile(prefix,suffix,dir);
167       Writer JavaDoc out = new BufferedWriter JavaDoc(new FileWriter JavaDoc(tmp));
168       out.write(event.message);
169       out.close();
170  /* this Appender is not supposed to be used for logging of Exceptions */
171       } catch (Exception JavaDoc e) {
172           errorHandler.error("Error during creation of temporary File!",e,1);
173       }
174   }
175   
176   public boolean requiresLayout() {
177       return false;
178   }
179   
180   public void close() {
181   /* nothing to do */
182   }
183 }
184 /*
185  * @author $Author: mwomack $
186  * @version $Revision: 311380 $
187  * @since $Date: 2005-05-26 22:27:57 -0500 (Thu, 26 May 2005) $
188  *
189  * $Log$
190  * Revision 1.1.2.1 2005/05/27 03:27:54 mwomack
191  * Fix for #35032. Added license header to .java files that did not already have a license.
192  *
193  * Revision 1.1 2001/04/20 17:38:31 ceki
194  *
195  * Added LeosLiterak's TempFileAppender.java
196  *
197 */

198
Popular Tags