KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > protomatter > syslog > LengthRolloverLog


1 package com.protomatter.syslog;
2
3 /**
4  * {{{ The Protomatter Software License, Version 1.0
5  * derived from The Apache Software License, Version 1.1
6  *
7  * Copyright (c) 1998-2002 Nate Sammons. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution,
22  * if any, must include the following acknowledgment:
23  * "This product includes software developed for the
24  * Protomatter Software Project
25  * (http://protomatter.sourceforge.net/)."
26  * Alternately, this acknowledgment may appear in the software itself,
27  * if and wherever such third-party acknowledgments normally appear.
28  *
29  * 4. The names "Protomatter" and "Protomatter Software Project" must
30  * not be used to endorse or promote products derived from this
31  * software without prior written permission. For written
32  * permission, please contact support@protomatter.com.
33  *
34  * 5. Products derived from this software may not be called "Protomatter",
35  * nor may "Protomatter" appear in their name, without prior written
36  * permission of the Protomatter Software Project
37  * (support@protomatter.com).
38  *
39  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
40  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
41  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
42  * DISCLAIMED. IN NO EVENT SHALL THE PROTOMATTER SOFTWARE PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
45  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
46  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
47  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
48  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
49  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE. }}}
51  */

52
53 import java.io.*;
54 import java.net.*;
55 import java.util.*;
56 import java.util.zip.*;
57 import java.text.*;
58
59 import com.protomatter.util.*;
60
61 /**
62   * An implementation of an object that will log things using the Syslog
63   * facility, and roll it's log files after a certain number of bytes
64   * have been written to them.
65   *
66   * @see com.protomatter.syslog.xml.LengthRolloverLog_Helper XML configuration class
67   */

68 public class LengthRolloverLog
69 extends BasicLogger
70 {
71   private Writer out = null;
72   private Object JavaDoc syncObject = new Object JavaDoc();
73   private int rolllength = 1048576; // default roll to 1Mbyte
74
private int written;
75   private String JavaDoc basename;
76   private String JavaDoc extension;
77   private boolean append = true;
78   private boolean autoFlush = true;
79   private File currentFile = null;
80   private File previousFile = null;
81
82   /**
83    * Write log information to the given log, roll when specified.
84    * The file written to will actually be "basename.number" where
85    * number is from 1..N (1 being older log entries than N) When
86    * starting, if logfiles basename.1 through basename.N exist,
87    * it will begin writing to basename.N+1. By default, all channels
88    * are listened to.
89    */

90   public LengthRolloverLog(String JavaDoc basename, String JavaDoc extension, int roll, boolean append, boolean autoFlush)
91   {
92     this();
93     this.basename = basename;
94     this.extension = extension;
95     this.rolllength = roll;
96     this.append = append;
97     this.autoFlush = autoFlush;
98     rollover();
99   }
100
101   /**
102    * You will need to call the configure() method to configure this
103    * logger if you use this constructor.
104    */

105   public LengthRolloverLog()
106   {
107     super();
108   }
109
110   /**
111    * Set the max file size (in bytes). Default is 1MB.
112    */

113   public void setRollLength(int rolllength)
114   {
115     this.rolllength = rolllength;
116   }
117   /**
118    * Get the max file size (in bytes).
119    */

120   public int getRollLength()
121   {
122     return this.rolllength;
123   }
124
125   /**
126    * Set the base file name.
127    */

128   public void setBasename(String JavaDoc basename)
129   {
130     this.basename = basename;
131   }
132   /**
133    * Get the base file name.
134    */

135   public String JavaDoc getBasename()
136   {
137     return this.basename;
138   }
139
140   /**
141    * Set the file extension.
142    */

143   public void setExtension(String JavaDoc extension)
144   {
145     this.extension = extension;
146   }
147   /**
148    * Get the file extension.
149    */

150   public String JavaDoc getExtension()
151   {
152     return this.extension;
153   }
154
155   /**
156    * Set the append-to-file flag. Default is true.
157    */

158   public void setAppend(boolean append)
159   {
160     this.append = append;
161   }
162   /**
163    * Get the append-to-file flag.
164    */

165   public boolean getAppend()
166   {
167     return this.append;
168   }
169
170   /**
171    * Set the auto-flush flag. Default is false.
172    */

173   public void setAutoFlush(boolean autoFlush)
174   {
175     this.autoFlush = autoFlush;
176   }
177   /**
178    * Get the auto-flush flag.
179    */

180   public boolean getAutoFlush()
181   {
182     return this.autoFlush;
183   }
184
185   /**
186    * Get the file currently being written to.
187    */

188   public File getCurrentFile()
189   {
190     return this.currentFile;
191   }
192
193   /**
194    * Get the file that was being written to before we rolled.
195    */

196   public File getPreviousFile()
197   {
198     return this.previousFile;
199   }
200
201   /**
202    * Roll the logs now.
203    */

204   public void rollover()
205   {
206     synchronized(syncObject)
207     {
208       if (out != null)
209       {
210         try
211         {
212           out.write(formatter.getLogFooter());
213         }
214         catch (IOException x)
215         {
216           x.printStackTrace();
217         }
218       }
219       resetDateFormat();
220       Writer old = out;
221       try
222       {
223         int i;
224         File f = null;
225         for (i=1; (new File(basename + i + extension)).exists(); i++);
226         f = new File(basename + i + extension);
227         out = new BufferedWriter(
228           new FileWriter(f.getCanonicalPath(), append));
229         out.write(formatter.getLogHeader());
230         written = 0;
231
232         if (old != null)
233         {
234           old.flush();
235           old.close();
236         }
237         this.previousFile = this.currentFile;
238         this.currentFile = f;
239       }
240       catch (IOException x)
241       {
242         System.err.println(MessageFormat.format(
243           Syslog.getResourceString(MessageConstants.LENGTHRO_CANNOT_ROLL_MESSAGE),
244           new Object JavaDoc[] { "IOException" } ));
245         x.printStackTrace();
246         out = old;
247       }
248     }
249   }
250
251   public final void log(SyslogMessage message)
252   {
253     StringBuffer JavaDoc b = new StringBuffer JavaDoc(128);
254     formatLogEntry(b, message);
255     int length = b.length();
256     synchronized(syncObject)
257     {
258       if ((length + written) > rolllength)
259         rollover();
260       try
261       {
262         out.write(b.toString());
263         if (autoFlush)
264           out.flush();
265         written += length;
266       }
267       catch (IOException x)
268       {
269         x.printStackTrace();
270       }
271     }
272   }
273
274   public void flush()
275   {
276     try
277     {
278       if (out != null)
279         out.flush();
280     }
281     catch (IOException x)
282     {
283       x.printStackTrace();
284     }
285   }
286
287   public void shutdown()
288   {
289     if (out != null)
290     {
291       try
292       {
293         out.write(formatter.getLogFooter());
294         out.flush();
295         out.close();
296         out = null;
297       }
298       catch (IOException x)
299       {
300         x.printStackTrace();
301       }
302     }
303   }
304 }
305
Popular Tags