KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > snaq > util > LogUtil


1 package snaq.util;
2
3 import java.io.*;
4 import java.text.*;
5 import java.util.*;
6
7 /**
8  * Base class providing simple logging and debug functionality, which can
9  * either be instantiated and used as a logging object, or can be sub-classed
10  * to be used as an integral logging facility.
11  * <p>When specifying an OutputStream for use by the Logger, make sure that none
12  * of the &quot;standard&quot; streams are used (System.out, System.err) as
13  * they will be closed either when the close() method is called or the Logger
14  * terminates. To send log to the standard output stream simply call
15  * setLogging(true) and leave the actual log unspecified.
16  * @author Giles Winstanley
17  */

18 public class LogUtil
19 {
20     protected DateFormat dateFormat, ddf;
21     protected PrintStream log;
22     protected boolean logging = false;
23     protected boolean debug = false;
24
25
26     /**
27      * Creates a new Logger with logging disabled.
28      */

29     public LogUtil()
30     {
31     }
32
33     /**
34      * Creates a new Logger which writes to the specified file.
35      * @throws FileNotFoundException if specified file is a directory, or cannot
36      * be opened for some reason.
37      */

38     public LogUtil(File f) throws FileNotFoundException
39     {
40         setLog(new FileOutputStream(f, true));
41     }
42
43     /**
44      * Sets the date formatter for the logging.
45      */

46     public synchronized void setDateFormat(DateFormat df) { dateFormat = df; }
47
48     /**
49      * Sets the log stream and enables logging.
50      */

51     public synchronized void setLog(OutputStream out)
52     {
53         if (log != null)
54             close();
55         if (out != null)
56             log = new PrintStream(out);
57         logging = true;
58     }
59
60     /**
61      * Sets the log writer and enables logging.
62      */

63     public synchronized void setLog(PrintStream ps)
64     {
65         if (log != null)
66             close();
67         log = ps;
68         logging = true;
69     }
70
71     /**
72      * Returns the current PrintStream used to write to the log.
73      */

74     public PrintStream getLogStream()
75     {
76         return log;
77     }
78
79     /**
80      * Writes a message to the log, with an optional prefix.
81      */

82     public synchronized void log(String JavaDoc prefix, String JavaDoc logEntry)
83     {
84         if (!logging)
85             return;
86         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
87         if (dateFormat != null)
88             sb.append(dateFormat.format(new Date()));
89         else
90         {
91             if (ddf == null)
92                 ddf = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);
93             sb.append(ddf.format(new Date()));
94         }
95
96         sb.append(": ");
97         if (prefix != null)
98             sb.append(prefix);
99         sb.append(logEntry);
100         if (log != null)
101         {
102             synchronized(log)
103             {
104                 log.println(sb.toString());
105 // log.flush();
106
}
107         }
108     }
109
110     /**
111      * Writes a message to the log.
112      */

113     public void log(String JavaDoc logEntry)
114     {
115         log("", logEntry);
116     }
117
118     /**
119      * Writes a message with an Exception to the log file.
120      */

121     public void log(Throwable JavaDoc e, String JavaDoc prefix, String JavaDoc logEntry)
122     {
123         if (!logging)
124             return;
125         log(prefix, logEntry);
126         e.printStackTrace(log);
127         log.flush();
128     }
129
130     /**
131      * Writes a message with an Exception to the log file.
132      */

133     public void log(Throwable JavaDoc e, String JavaDoc logEntry)
134     {
135         log(e, "", logEntry);
136     }
137
138     /**
139      * Writes an Exception to the log file.
140      */

141     public void log(Throwable JavaDoc e)
142     {
143         log(e, e.getMessage());
144     }
145
146     /**
147      * Closes the log.
148      */

149     public synchronized void close()
150     {
151         logging = false;
152         if (log != null)
153         {
154             log.flush();
155             if (!isSystemLog())
156                 log.close();
157         }
158         log = null;
159     }
160
161     // Returns whether the log is writing to a system log stream.
162
private boolean isSystemLog()
163     {
164         return (!log.equals(System.out) && !log.equals(System.err));
165     }
166
167     /**
168      * Determines whether to write to the log.
169      */

170     public void setLogging(boolean b) { logging = b; }
171
172     /**
173      * Returns whether logging is enabled.
174      */

175     public boolean isLogging() { return logging; }
176
177     /**
178      * Determines whether to perform debug logging.
179      */

180     public void setDebug(boolean b) { debug = b; }
181
182     /**
183      * Returns whether debug logging is enabled.
184      */

185     public boolean isDebug() { return debug; }
186 }
Popular Tags