KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > logicalcobwebs > logging > impl > Jdk14Logger


1 /*
2  * $Header: /cvsroot/proxool/proxool/src/java/org/logicalcobwebs/logging/impl/Jdk14Logger.java,v 1.7 2003/09/11 23:00:23 billhorsman Exp $
3  * $Revision: 1.7 $
4  * $Date: 2003/09/11 23:00:23 $
5  *
6  * ====================================================================
7  *
8  * The Apache Software License, Version 1.1
9  *
10  * Copyright (c) 1999-2001 The Apache Software Foundation. All rights
11  * reserved.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  *
17  * 1. Redistributions of source code must retain the above copyright
18  * notice, this list of conditions and the following disclaimer.
19  *
20  * 2. Redistributions in binary form must reproduce the above copyright
21  * notice, this list of conditions and the following disclaimer in
22  * the documentation and/or other materials provided with the
23  * distribution.
24  *
25  * 3. The end-user documentation included with the redistribution, if
26  * any, must include the following acknowlegement:
27  * "This product includes software developed by the
28  * Apache Software Foundation (http://www.apache.org/)."
29  * Alternately, this acknowlegement may appear in the software itself,
30  * if and wherever such third-party acknowlegements normally appear.
31  *
32  * 4. The names "The Jakarta Project", "Commons", and "Apache Software
33  * Foundation" must not be used to endorse or promote products derived
34  * from this software without prior written permission. For written
35  * permission, please contact apache@apache.org.
36  *
37  * 5. Products derived from this software may not be called "Apache"
38  * nor may "Apache" appear in their names without prior written
39  * permission of the Apache Group.
40  *
41  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
42  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
43  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
45  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
47  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
48  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
49  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
51  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52  * SUCH DAMAGE.
53  * ====================================================================
54  *
55  * This software consists of voluntary contributions made by many
56  * individuals on behalf of the Apache Software Foundation. For more
57  * information on the Apache Software Foundation, please see
58  * <http://www.apache.org/>.
59  *
60  */

61
62 package org.logicalcobwebs.logging.impl;
63
64 import java.lang.reflect.Method JavaDoc;
65 import java.lang.reflect.InvocationTargetException JavaDoc;
66
67 import org.logicalcobwebs.logging.Log;
68
69 /**
70  * <p>Implementation of the <code>org.logicalcobwebs.logging.Log</code>
71  * interfaces that wraps the standard JDK logging mechanisms that were
72  * introduced in the Merlin release (JDK 1.4).</p>
73  *
74  * @author <a HREF="mailto:sanders@apache.org">Scott Sanders</a>
75  * @author <a HREF="mailto:bloritsch@apache.org">Berin Loritsch</a>
76  * @author <a HREF="mailto:donaldp@apache.org">Peter Donald</a>
77  * @version $Revision: 1.7 $ $Date: 2003/09/11 23:00:23 $
78  */

79
80 public final class Jdk14Logger implements Log {
81
82     // ----------------------------------------------------------- Constructors
83

84
85     /**
86      * Construct a named instance of this Logger.
87      *
88      * @param name Name of the logger to be constructed
89      */

90     public Jdk14Logger (String JavaDoc name) {
91         try {
92             final Class JavaDoc loggerClass = Class.forName("java.util.logging.Logger");
93             final Class JavaDoc levelClass = Class.forName("java.util.logging.Level");
94             final Method JavaDoc getLoggerMethod = loggerClass.getMethod("getLogger", new Class JavaDoc [] {String JavaDoc.class});
95             logger = getLoggerMethod.invoke(null, new Object JavaDoc[]{name});
96             logpMethod = logger.getClass().getMethod("logp", new Class JavaDoc[] {levelClass,
97                 String JavaDoc.class, String JavaDoc.class, String JavaDoc.class});
98             logpExMethod = logger.getClass().getMethod("logp", new Class JavaDoc[] {levelClass,
99                 String JavaDoc.class, String JavaDoc.class, String JavaDoc.class, Throwable JavaDoc.class});
100             isLoggableMethod = loggerClass.getMethod("isLoggable", new Class JavaDoc[] {levelClass});
101             getStackTraceMethod = Throwable JavaDoc.class.getMethod("getStackTrace", null);
102             final Class JavaDoc stackTraceClass = Class.forName("java.lang.StackTraceElement");
103             getClassNameMethod = stackTraceClass.getMethod("getClassName", null);
104             getMethodNameMethod = stackTraceClass.getMethod("getMethodName", null);
105             levelFINEST = levelClass.getField("FINEST").get(null);
106             levelFINE = levelClass.getField("FINE").get(null);
107             levelINFO = levelClass.getField("INFO").get(null);
108             levelWARNING = levelClass.getField("WARNING").get(null);
109             levelSEVERE = levelClass.getField("SEVERE").get(null);
110         } catch (Throwable JavaDoc e) {
111             e.printStackTrace();
112             throw new RuntimeException JavaDoc("Could not create Jdk14Logger: " + e.getMessage());
113         }
114     }
115
116
117     // ----------------------------------------------------- Instance Variables
118

119
120     /**
121      * The underlying Logger implementation we are using.
122      */

123     private Object JavaDoc logger = null;
124
125     private Method JavaDoc logpMethod = null;
126     private Method JavaDoc logpExMethod = null;
127     private Method JavaDoc isLoggableMethod = null;
128     private Method JavaDoc getStackTraceMethod = null;
129     private Method JavaDoc getClassNameMethod = null;
130     private Method JavaDoc getMethodNameMethod = null;
131
132     private Object JavaDoc levelFINEST = null;
133     private Object JavaDoc levelFINE = null;
134     private Object JavaDoc levelINFO = null;
135     private Object JavaDoc levelWARNING = null;
136     private Object JavaDoc levelSEVERE = null;
137
138
139
140
141
142
143
144     // --------------------------------------------------------- Public Methods
145

146     private void log (Object JavaDoc level, String JavaDoc msg, Throwable JavaDoc ex) {
147         // Hack (?) to get the stack trace.
148
Throwable JavaDoc dummyException = new Throwable JavaDoc ();
149         String JavaDoc cname = "unknown";
150         String JavaDoc method = "unknown";
151         // Use reflection instead of JDK1.4 code.
152
try {
153             Object JavaDoc locations[] = (Object JavaDoc[]) getStackTraceMethod.invoke(dummyException, null);
154             // Caller will be the third element
155
if (locations != null && locations.length > 2) {
156                 cname = (String JavaDoc) getClassNameMethod.invoke(locations[2], null);
157                 method = (String JavaDoc) getMethodNameMethod.invoke(locations[2], null);
158             }
159         } catch (IllegalAccessException JavaDoc e) {
160             e.printStackTrace();
161         } catch (IllegalArgumentException JavaDoc e) {
162             e.printStackTrace();
163         } catch (InvocationTargetException JavaDoc e) {
164             e.printStackTrace();
165         }
166
167         try {
168             if (ex == null) {
169                 logpMethod.invoke(logger, new Object JavaDoc[]{level, cname, method, msg});
170             } else {
171                 logpExMethod.invoke(logger, new Object JavaDoc[]{level, cname, method, msg, ex});
172             }
173         } catch (Exception JavaDoc e) {
174             throw new RuntimeException JavaDoc(
175                 "Logging of message '" + msg + "' failed" + (ex != null ? ":" + ex.getMessage() : "."));
176         }
177     }
178
179     /**
180      * Log a message with debug log level.
181      */

182     public void debug (Object JavaDoc message) {
183         log (levelFINE, String.valueOf (message), null);
184     }
185
186     /**
187      * Log a message and exception with debug log level.
188      */

189     public void debug (Object JavaDoc message, Throwable JavaDoc exception) {
190         log (levelFINE, String.valueOf (message), exception);
191     }
192
193     /**
194      * Log a message with error log level.
195      */

196     public void error (Object JavaDoc message) {
197         log (levelSEVERE, String.valueOf (message), null);
198     }
199
200     /**
201      * Log a message and exception with error log level.
202      */

203     public void error (Object JavaDoc message, Throwable JavaDoc exception) {
204         log (levelSEVERE, String.valueOf (message), exception);
205     }
206
207     /**
208      * Log a message with fatal log level.
209      */

210     public void fatal (Object JavaDoc message) {
211         log (levelSEVERE, String.valueOf (message), null);
212     }
213
214     /**
215      * Log a message and exception with fatal log level.
216      */

217     public void fatal (Object JavaDoc message, Throwable JavaDoc exception) {
218         log (levelSEVERE, String.valueOf (message), exception);
219     }
220
221     /**
222      * Log a message with info log level.
223      */

224     public void info (Object JavaDoc message) {
225         log (levelINFO, String.valueOf (message), null);
226     }
227
228     /**
229      * Log a message and exception with info log level.
230      */

231     public void info (Object JavaDoc message, Throwable JavaDoc exception) {
232         log (levelINFO, String.valueOf (message), exception);
233     }
234
235     /**
236      * Is debug logging currently enabled?
237      */

238     public boolean isDebugEnabled () {
239         return (isLoggable (levelFINE));
240     }
241
242     /**
243      * Is error logging currently enabled?
244      */

245     public boolean isErrorEnabled () {
246         return (isLoggable (levelSEVERE));
247     }
248
249     /**
250      * Is fatal logging currently enabled?
251      */

252     public boolean isFatalEnabled () {
253         return (isLoggable (levelSEVERE));
254     }
255
256     /**
257      * Is info logging currently enabled?
258      */

259     public boolean isInfoEnabled () {
260         return (isLoggable (levelINFO));
261     }
262
263     /**
264      * Is tace logging currently enabled?
265      */

266     public boolean isTraceEnabled () {
267         return (isLoggable (levelFINEST));
268     }
269
270     /**
271      * Is warning logging currently enabled?
272      */

273     public boolean isWarnEnabled () {
274         return (isLoggable (levelWARNING));
275     }
276
277     /**
278      * Log a message with trace log level.
279      */

280     public void trace (Object JavaDoc message) {
281         log (levelFINEST, String.valueOf (message), null);
282     }
283
284     /**
285      * Log a message and exception with trace log level.
286      */

287     public void trace (Object JavaDoc message, Throwable JavaDoc exception) {
288         log (levelFINEST, String.valueOf (message), exception);
289     }
290
291     /**
292      * Log a message with warn log level.
293      */

294     public void warn (Object JavaDoc message) {
295         log (levelWARNING, String.valueOf (message), null);
296     }
297
298     /**
299      * Log a message and exception with warn log level.
300      */

301     public void warn (Object JavaDoc message, Throwable JavaDoc exception) {
302         log (levelWARNING, String.valueOf (message), exception);
303     }
304
305     private boolean isLoggable(Object JavaDoc level) {
306         try {
307             return ((Boolean JavaDoc) isLoggableMethod.invoke(logger, new Object JavaDoc[] {level})).booleanValue();
308         } catch (Exception JavaDoc e) {
309             throw new RuntimeException JavaDoc("isLoggable call failed: " + e.getMessage());
310         }
311     }
312 }
313
314
Popular Tags