KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > winstone > Logger


1 /*
2  * Copyright 2003-2006 Rick Knowles <winstone-devel at lists sourceforge net>
3  * Distributed under the terms of either:
4  * - the common development and distribution license (CDDL), v1.0; or
5  * - the GNU Lesser General Public License, v2.1 or later
6  */

7 package winstone;
8
9 import java.io.OutputStream JavaDoc;
10 import java.io.PrintWriter JavaDoc;
11 import java.io.StringWriter JavaDoc;
12 import java.text.DateFormat JavaDoc;
13 import java.text.SimpleDateFormat JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.Collection JavaDoc;
16 import java.util.Date JavaDoc;
17 import java.util.Hashtable JavaDoc;
18 import java.util.Map JavaDoc;
19
20 /**
21  * A utility class for logging event and status messages. It maintains a
22  * collection of streams for different types of messages, but any messages with
23  * unknown or unspecified stream go to the default stream.
24  *
25  * @author <a HREF="mailto:rick_knowles@hotmail.com">Rick Knowles</a>
26  * @version $Id: Logger.java,v 1.6 2006/02/28 09:10:41 rickknowles Exp $
27  */

28 public class Logger {
29     public final static String JavaDoc DEFAULT_STREAM = "Winstone";
30     public static int MIN = 1;
31     public static int ERROR = 2;
32     public static int WARNING = 3;
33     public static int INFO = 5;
34     public static int SPEED = 6;
35     public static int DEBUG = 7;
36     public static int FULL_DEBUG = 8;
37     public static int MAX = 9;
38
39     protected static Boolean JavaDoc semaphore = new Boolean JavaDoc(true);
40     protected static boolean initialised = false;
41     protected static Map JavaDoc streams;
42     protected static Collection JavaDoc nullStreams;
43     protected static int currentDebugLevel;
44     protected final static DateFormat JavaDoc sdfLog = new SimpleDateFormat JavaDoc("yyyy/MM/dd HH:mm:ss");
45 // protected static boolean showThrowingLineNo;
46
protected static boolean showThrowingThread;
47
48     /**
49      * Initialises default streams
50      */

51     public static void init(int level) {
52         init(level, System.out, false);
53 // init(level, System.out, false, false);
54
}
55
56     /**
57      * Initialises default streams
58      */

59     public static void init(int level, OutputStream JavaDoc defaultStream,
60 // boolean showThrowingLineNoArg,
61
boolean showThrowingThreadArg) {
62         currentDebugLevel = level;
63         streams = new Hashtable JavaDoc();
64         nullStreams = new ArrayList JavaDoc();
65         initialised = true;
66         setStream(DEFAULT_STREAM, defaultStream);
67 // showThrowingLineNo = showThrowingLineNoArg;
68
showThrowingThread = showThrowingThreadArg;
69     }
70
71     /**
72      * Allocates a stream for redirection to a file etc
73      */

74     public static void setStream(String JavaDoc name, OutputStream JavaDoc stream) {
75         if (stream == null)
76             nullStreams.add(name);
77         else
78             setStream(name, new PrintWriter JavaDoc(stream));
79     }
80
81     /**
82      * Allocates a stream for redirection to a file etc
83      */

84     public static void setStream(String JavaDoc name, PrintWriter JavaDoc stream) {
85         // synchronized (semaphore)
86
{
87             if (!initialised)
88                 init(INFO);
89             if (stream == null)
90                 nullStreams.add(name);
91             else
92                 streams.put(name, stream);
93         }
94     }
95
96     /**
97      * Forces a flush of the contents to file, display, etc
98      */

99     public static void flush(String JavaDoc name) {
100         // synchronized (semaphore)
101
{
102             if (!initialised)
103                 init(INFO);
104             PrintWriter JavaDoc p = (PrintWriter JavaDoc) streams.get(name);
105             if (p != null)
106                 p.flush();
107         }
108     }
109
110     public static void setCurrentDebugLevel(int level) {
111         if (!initialised)
112             init(level);
113         else
114             currentDebugLevel = level;
115     }
116
117     /**
118      * Writes a log message to the default stream / public static void log(int
119      * level, String message) { log(level, DEFAULT_STREAM, message); }
120      *
121      * /** Writes a log message to the default stream / public static void
122      * log(int level, String message, Throwable error) { log(level,
123      * DEFAULT_STREAM, message, error); }
124      *
125      * /** Writes a log message to the default stream / public static void
126      * log(int level, String streamName, String message) { log(level,
127      * streamName, message, null); }
128      *
129      * /** Writes a log message to the requested stream, and immediately flushes
130      * the contents of the stream.
131      */

132     private static void logInternal(String JavaDoc streamName, String JavaDoc message, Throwable JavaDoc error) {
133         
134         if (!initialised) {
135             init(INFO);
136         }
137         
138         String JavaDoc lineNoText = "";
139 // if (showThrowingLineNo) {
140
// Throwable dummyError = new RuntimeException();
141
// StackTraceElement[] elements = dummyError.getStackTrace();
142
// int elemNumber = Math.min(2, elements.length);
143
// String errorClass = elements[elemNumber].getClassName();
144
// if (errorClass.lastIndexOf('.') != -1) {
145
// errorClass = errorClass.substring(errorClass.lastIndexOf('.') + 1);
146
// }
147
// lineNoText = "[" + errorClass + ":" + elements[elemNumber].getLineNumber() + "] - ";
148
// }
149
if (showThrowingThread) {
150             lineNoText += "[" + Thread.currentThread().getName() + "] - ";
151         }
152
153         PrintWriter JavaDoc stream = (PrintWriter JavaDoc) streams.get(streamName);
154         boolean nullStream = nullStreams.contains(streamName);
155         if ((stream == null) && !nullStream)
156             stream = (PrintWriter JavaDoc) streams.get(DEFAULT_STREAM);
157
158         if (stream != null) {
159             StringBuffer JavaDoc fullMessage = new StringBuffer JavaDoc();
160             String JavaDoc date = null;
161             synchronized (sdfLog) {
162                 date = sdfLog.format(new Date JavaDoc());
163             }
164             fullMessage.append("[").append(streamName).append(" ").append(
165                     date).append("] - ").append(lineNoText).append(message);
166             if (error != null) {
167                 StringWriter JavaDoc sw = new StringWriter JavaDoc();
168                 PrintWriter JavaDoc pw = new PrintWriter JavaDoc(sw);
169                 error.printStackTrace(pw);
170                 pw.flush();
171                 fullMessage.append('\n').append(sw.toString());
172             }
173             stream.println(fullMessage.toString());
174             stream.flush();
175         }
176     }
177
178     public static void log(int level, WinstoneResourceBundle resources,
179             String JavaDoc messageKey) {
180         if (currentDebugLevel < level)
181             return;
182         else
183             logInternal(DEFAULT_STREAM, resources.getString(messageKey), null);
184     }
185
186     public static void log(int level, WinstoneResourceBundle resources,
187             String JavaDoc messageKey, Throwable JavaDoc error) {
188         if (currentDebugLevel < level)
189             return;
190         else
191             logInternal(DEFAULT_STREAM, resources.getString(messageKey), error);
192     }
193
194     public static void log(int level, WinstoneResourceBundle resources,
195             String JavaDoc messageKey, String JavaDoc param) {
196         if (currentDebugLevel < level)
197             return;
198         else
199             logInternal(DEFAULT_STREAM, resources.getString(messageKey, param), null);
200     }
201
202     public static void log(int level, WinstoneResourceBundle resources,
203             String JavaDoc messageKey, String JavaDoc params[]) {
204         if (currentDebugLevel < level)
205             return;
206         else
207             logInternal(DEFAULT_STREAM, resources.getString(messageKey, params), null);
208     }
209
210     public static void log(int level, WinstoneResourceBundle resources,
211             String JavaDoc messageKey, String JavaDoc param, Throwable JavaDoc error) {
212         if (currentDebugLevel < level)
213             return;
214         else
215             logInternal(DEFAULT_STREAM, resources.getString(messageKey, param), error);
216     }
217
218     public static void log(int level, WinstoneResourceBundle resources,
219             String JavaDoc messageKey, String JavaDoc params[], Throwable JavaDoc error) {
220         if (currentDebugLevel < level)
221             return;
222         else
223             logInternal(DEFAULT_STREAM, resources.getString(messageKey, params), error);
224     }
225
226     public static void log(int level, WinstoneResourceBundle resources,
227             String JavaDoc streamName, String JavaDoc messageKey, String JavaDoc params[], Throwable JavaDoc error) {
228         if (currentDebugLevel < level)
229             return;
230         else
231             logInternal(streamName, resources.getString(messageKey, params), error);
232     }
233
234     public static void logDirectMessage(int level, String JavaDoc streamName, String JavaDoc message,
235             Throwable JavaDoc error) {
236         if (currentDebugLevel < level)
237             return;
238         else
239             logInternal(streamName, message, error);
240     }
241 }
242
Popular Tags