KickJava   Java API By Example, From Geeks To Geeks.

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


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  * A logger that simply writes to a file.
63  *
64  * @see com.protomatter.syslog.xml.FileLog_Helper XML configuration class
65  */

66 public class FileLog
67 extends BasicLogger
68 {
69    private boolean append = true;
70
71    private File file;
72    private Writer out = null;
73    private boolean autoFlush = true;
74
75    /**
76     * Create a new file log attached to the given file.
77     */

78    public FileLog(File f)
79    {
80      this(f, true, false);
81    }
82
83    /**
84     * Create a new file log attached to the given file.
85     */

86    public FileLog(File f, boolean append, boolean autoFlush)
87    {
88      this();
89      setFile(f);
90      setAppend(append);
91      setAutoFlush(autoFlush);
92    }
93
94    /**
95     * You will need to call the configure() method if you use this constructor.
96     */

97    public FileLog()
98    {
99      super();
100    }
101
102    /**
103     * Set the file we're writing to.
104     */

105    public synchronized void setFile(File f)
106    {
107      cleanup();
108      file = f;
109    }
110
111    /**
112     * Get the file we're writing to.
113     */

114    public File getFile()
115    {
116      return this.file;
117    }
118
119    /**
120     * Set the file we're writing to.
121     */

122    public void setAppend(boolean append)
123    {
124      this.append = append;
125    }
126
127    /**
128     * Get the file we're writing to.
129     */

130    public boolean getAppend()
131    {
132      return this.append;
133    }
134
135    /**
136     * Should we auto-flush the buffer all the time?
137     */

138    public void setAutoFlush(boolean flush)
139    {
140      this.autoFlush = flush;
141    }
142
143    /**
144     * Determine if we should we auto-flush the buffer all the time.
145     */

146    public boolean getAutoFlush()
147    {
148      return this.autoFlush;
149    }
150
151    /**
152     * Log a message.
153     */

154    public final void log(SyslogMessage message)
155    {
156       StringBuffer JavaDoc b = null;
157       if (message.detail == null)
158         b = new StringBuffer JavaDoc(128);
159       else
160         b = new StringBuffer JavaDoc(256);
161       formatLogEntry(b, message);
162
163       try
164       {
165          if (out == null)
166          {
167            synchronized(file)
168            {
169              if (out == null)
170              {
171                out = new BufferedWriter(
172                  new FileWriter(file.getCanonicalPath(), append));
173                out.write(formatter.getLogHeader());
174              }
175            }
176          }
177          out.write(b.toString());
178          if (autoFlush)
179            out.flush();
180       }
181       catch (IOException x)
182       {
183         System.err.println(MessageFormat.format(
184           Syslog.getResourceString(MessageConstants.FILELOG_CANNOT_WRITE_MESSAGE),
185           new Object JavaDoc[] { x.toString() }));
186         x.printStackTrace();
187       }
188    }
189
190    private void cleanup()
191    {
192      if (out != null)
193      {
194        try
195        {
196          out.write(formatter.getLogFooter());
197          out.flush();
198          out.close();
199          out = null;
200        }
201        catch (IOException x)
202        {
203          x.printStackTrace();
204        }
205      }
206    }
207
208    /**
209     * Closes down the file and prepares for shutdown.
210     */

211    public synchronized void shutdown()
212    {
213      cleanup();
214    }
215
216    public void flush()
217    {
218      try
219      {
220        if (out != null)
221          out.flush();
222      }
223      catch (IOException x)
224      {
225        x.printStackTrace();
226      }
227    }
228 }
229
Popular Tags