KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > log > JDKLogger


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id:$
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.log;
27
28 import java.io.Serializable JavaDoc;
29 import java.text.MessageFormat JavaDoc;
30 import java.util.logging.ConsoleHandler JavaDoc;
31 import java.util.logging.Handler JavaDoc;
32 import java.util.logging.Level JavaDoc;
33 import java.util.logging.Logger JavaDoc;
34
35 import org.objectweb.easybeans.i18n.I18n;
36
37 /**
38  * Logger with I18n and infinite arguments.<br>
39  * @author Florent Benoit
40  */

41 public class JDKLogger implements JLog, Serializable JavaDoc {
42
43     /**
44      * Id for serializable class.
45      */

46     private static final long serialVersionUID = -3786299204538237422L;
47
48     /**
49      * Name of this logger.
50      */

51     private String JavaDoc name = null;
52
53     /**
54      * Logger object (transient).
55      */

56     private transient Logger JavaDoc logger = null;
57
58     /**
59      * I18n object.
60      */

61     private I18n i18n = null;
62
63     /**
64      * Handler already set.
65      */

66     private static boolean handlerSet = false;
67
68     /**
69      * Property to disable the set of our own handler.
70      */

71     public static final String JavaDoc SET_HANDLER = "easybeans.log.handler";
72
73     /**
74      * Creates a new logger.
75      * @param clazz the class that requires a logger.
76      * @param i18n the internationalization object to use, if any.
77      */

78     public JDKLogger(final Class JavaDoc clazz, final I18n i18n) {
79         this.name = clazz.getName();
80         this.i18n = i18n;
81         logger = getLogger();
82
83     }
84
85     /**
86      * Log the given message with the given throwable (if any).
87      * @param level the level of the message (to log)
88      * @param msg the content of the message
89      * @param t the throwable (if any)
90      */

91     private void log(final Level JavaDoc level, final String JavaDoc msg, final Throwable JavaDoc t) {
92         // Get stack trace
93
Throwable JavaDoc throwable = new Throwable JavaDoc();
94         StackTraceElement JavaDoc[] stackTraceElements = throwable.getStackTrace();
95
96         // Caller = 3rd element in the stack
97
String JavaDoc className = "stack-not-found";
98         String JavaDoc methodName = "stack-not-found";
99         if (stackTraceElements != null && stackTraceElements.length > 2) {
100             StackTraceElement JavaDoc caller = stackTraceElements[2];
101             className = caller.getClassName();
102             methodName = caller.getMethodName();
103         }
104
105         // Get logger
106
Logger JavaDoc logger = getLogger();
107
108         // log
109
if (t == null) {
110             logger.logp(level, className, methodName, msg);
111         } else {
112             logger.logp(level, className, methodName, msg, t);
113         }
114
115     }
116
117     /**
118      * Replace the console handler by our handler.
119      * @param logger the given logger to analyze.
120      */

121     private void replaceConsoleHandlerInParentHandlers(final Logger JavaDoc logger) {
122         if (logger == null) {
123             return;
124         }
125         Logger JavaDoc parent = logger.getParent();
126         if (parent == null) {
127             return;
128         }
129         Handler JavaDoc[] handlers = parent.getHandlers();
130         if (handlers != null) {
131             for (Handler JavaDoc handler : handlers) {
132                 if (handler instanceof ConsoleHandler JavaDoc) {
133                     parent.removeHandler(handler);
134                     // add our customize handler
135
Handler JavaDoc easyBeansHandler = new JDKConsoleHandler();
136                     easyBeansHandler.setFormatter(new JDKFormatter());
137                     parent.addHandler(easyBeansHandler);
138                 }
139            }
140         }
141     }
142
143     /**
144      * @return JDK logger
145      */

146     public Logger JavaDoc getLogger() {
147         if (logger == null) {
148             logger = Logger.getLogger(name);
149         }
150
151
152         // Remove parent handler if not already done
153
if (!handlerSet) {
154             String JavaDoc prop = System.getProperty(SET_HANDLER, "true");
155             // if true, set our own handler
156
if (Boolean.parseBoolean(prop)) {
157                 replaceConsoleHandlerInParentHandlers(logger);
158
159             }
160             // even if it was false, set value to true to avoid to check each time.
161
handlerSet = true;
162         }
163         return logger;
164     }
165
166     /**
167      * <p>
168      * Is debug logging currently enabled?
169      * </p>
170      * <p>
171      * Call this method to prevent having to perform expensive operations (for
172      * example, <code>String</code> concatenation) when the log level is more
173      * than debug.
174      * </p>
175      * @return true if it is enabled, else false
176      */

177     public boolean isDebugEnabled() {
178         return (getLogger().isLoggable(Level.FINE));
179     }
180
181     /**
182      * <p>
183      * Is error logging currently enabled?
184      * </p>
185      * <p>
186      * Call this method to prevent having to perform expensive operations (for
187      * example, <code>String</code> concatenation) when the log level is more
188      * than error.
189      * </p>
190      * @return true if it is enabled, else false
191      */

192     public boolean isErrorEnabled() {
193         return (getLogger().isLoggable(Level.SEVERE));
194     }
195
196     /**
197      * <p>
198      * Is fatal logging currently enabled?
199      * </p>
200      * <p>
201      * Call this method to prevent having to perform expensive operations (for
202      * example, <code>String</code> concatenation) when the log level is more
203      * than fatal.
204      * </p>
205      * @return true if it is enabled, else false
206      */

207     public boolean isFatalEnabled() {
208         return (getLogger().isLoggable(Level.SEVERE));
209     }
210
211     /**
212      * <p>
213      * Is info logging currently enabled?
214      * </p>
215      * <p>
216      * Call this method to prevent having to perform expensive operations (for
217      * example, <code>String</code> concatenation) when the log level is more
218      * than info.
219      * </p>
220      * @return true if it is enabled, else false
221      */

222     public boolean isInfoEnabled() {
223         return (getLogger().isLoggable(Level.INFO));
224     }
225
226     /**
227      * <p>
228      * Is trace logging currently enabled?
229      * </p>
230      * <p>
231      * Call this method to prevent having to perform expensive operations (for
232      * example, <code>String</code> concatenation) when the log level is more
233      * than trace.
234      * </p>
235      * @return true if it is enabled, else false
236      */

237     public boolean isTraceEnabled() {
238         return (getLogger().isLoggable(Level.FINEST));
239     }
240
241     /**
242      * <p>
243      * Is warn logging currently enabled?
244      * </p>
245      * <p>
246      * Call this method to prevent having to perform expensive operations (for
247      * example, <code>String</code> concatenation) when the log level is more
248      * than warn.
249      * </p>
250      * @return true if it is enabled, else false
251      */

252     public boolean isWarnEnabled() {
253         return (getLogger().isLoggable(Level.WARNING));
254     }
255
256     /**
257      * Gets the i18n object associated to this logger.
258      * @return i18n object.
259      */

260     public I18n getI18n() {
261         return i18n;
262     }
263
264     /**
265      * <p>
266      * Log a message with debug log level.
267      * </p>
268      * @param message - This could be
269      * <ul>
270      * <li>an entry of an I18n repository</li>
271      * <li>a preformated message given to MessageFormat class</li>
272      * <li>or a simple object on which toString() method will be called</li>
273      * </ul>
274      * @param args could be empty or contains the object for the formatter (I18n
275      * case). To log an exception, the exception must be the last
276      * argument.
277      */

278     public void debug(final Object JavaDoc message, final Object JavaDoc... args) {
279         // Do nothing is level is not enabled
280
if (!isDebugEnabled()) {
281             return;
282         }
283
284         String JavaDoc msg = getMessage(message, args);
285         Throwable JavaDoc t = getThrowable(args);
286         log(Level.FINE, msg, t);
287
288     }
289
290     /**
291      * <p>
292      * Log a message with error log level.
293      * </p>
294      * @param message - This could be
295      * <ul>
296      * <li>an entry of an I18n repository</li>
297      * <li>a preformated message given to MessageFormat class</li>
298      * <li>or a simple object on which toString() method will be called</li>
299      * </ul>
300      * @param args could be empty or contains the object for the formatter (I18n
301      * case). To log an exception, the exception must be the last
302      * argument.
303      */

304     public void error(final Object JavaDoc message, final Object JavaDoc... args) {
305         String JavaDoc msg = getMessage(message, args);
306         Throwable JavaDoc t = getThrowable(args);
307         log(Level.SEVERE, msg, t);
308     }
309
310     /**
311      * <p>
312      * Log a message with fatal log level.
313      * </p>
314      * @param message - This could be
315      * <ul>
316      * <li>an entry of an I18n repository</li>
317      * <li>a preformated message given to MessageFormat class</li>
318      * <li>or a simple object on which toString() method will be called</li>
319      * </ul>
320      * @param args could be empty or contains the object for the formatter (I18n
321      * case). To log an exception, the exception must be the last
322      * argument.
323      */

324     public void fatal(final Object JavaDoc message, final Object JavaDoc... args) {
325         // same as error
326
error(message, args);
327     }
328
329     /**
330      * <p>
331      * Log a message with info log level.
332      * </p>
333      * @param message - This could be
334      * <ul>
335      * <li>an entry of an I18n repository</li>
336      * <li>a preformated message given to MessageFormat class</li>
337      * <li>or a simple object on which toString() method will be called</li>
338      * </ul>
339      * @param args could be empty or contains the object for the formatter (I18n
340      * case). To log an exception, the exception must be the last
341      * argument.
342      */

343     public void info(final Object JavaDoc message, final Object JavaDoc... args) {
344         // Do nothing is level is not enabled
345
if (!isInfoEnabled()) {
346             return;
347         }
348
349         String JavaDoc msg = getMessage(message, args);
350         Throwable JavaDoc t = getThrowable(args);
351         log(Level.INFO, msg, t);
352     }
353
354     /**
355      * <p>
356      * Log a message with trace log level.
357      * </p>
358      * @param message - This could be
359      * <ul>
360      * <li>an entry of an I18n repository</li>
361      * <li>a preformated message given to MessageFormat class</li>
362      * <li>or a simple object on which toString() method will be called</li>
363      * </ul>
364      * @param args could be empty or contains the object for the formatter (I18n
365      * case). To log an exception, the exception must be the last
366      * argument.
367      */

368     public void trace(final Object JavaDoc message, final Object JavaDoc... args) {
369         // Do nothing is level is not enabled
370
if (!isTraceEnabled()) {
371             return;
372         }
373
374         String JavaDoc msg = getMessage(message, args);
375         Throwable JavaDoc t = getThrowable(args);
376         log(Level.FINEST, msg, t);
377
378     }
379
380     /**
381      * <p>
382      * Log a message with warn log level.
383      * </p>
384      * @param message - This could be
385      * <ul>
386      * <li>an entry of an I18n repository</li>
387      * <li>a preformated message given to MessageFormat class</li>
388      * <li>or a simple object on which toString() method will be called</li>
389      * </ul>
390      * @param args could be empty or contains the object for the formatter (I18n
391      * case). To log an exception, the exception must be the last
392      * argument.
393      */

394     public void warn(final Object JavaDoc message, final Object JavaDoc... args) {
395         // Do nothing is level is not enabled
396
if (!isWarnEnabled()) {
397             return;
398         }
399
400         String JavaDoc msg = getMessage(message, args);
401         Throwable JavaDoc t = getThrowable(args);
402         log(Level.WARNING, msg, t);
403
404     }
405
406     /**
407      * Gets a string message build with the given args.<br> *
408      * @param message If the message is an i18n entry, this entry will be used
409      * to format the message.<br>
410      * Else, the arguments will be used as string formatter
411      * @param args given args use by the formatter
412      * @return a string representation of the given message.
413      */

414     private String JavaDoc getMessage(final Object JavaDoc message, final Object JavaDoc... args) {
415
416         // I18n present ?
417
if (i18n != null) {
418             // try to format message
419
return i18n.getMessage(String.valueOf(message), args);
420         }
421
422         // else, try to format message
423
return MessageFormat.format(String.valueOf(message), args);
424     }
425
426     /**
427      * Try to find a Throwable object in the array of object args. The throwable
428      * should be the last parameter.
429      * @param args array of objects on which we need to find the Throwable
430      * @return a throwable object if last index if a Throwable or null
431      */

432     private Throwable JavaDoc getThrowable(final Object JavaDoc... args) {
433         int size = args.length;
434         // Detect the size
435
if (size > 0) {
436             Object JavaDoc lastArg = args[size - 1];
437             if (lastArg instanceof Throwable JavaDoc) {
438                 return (Throwable JavaDoc) lastArg;
439             }
440         }
441
442         // not found
443
return null;
444     }
445
446 }
447
Popular Tags