KickJava   Java API By Example, From Geeks To Geeks.

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


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: CommonsLoggerImpl.java 572 2006-06-04 21:23:27Z benoitf $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.log;
27
28 import java.text.MessageFormat JavaDoc;
29
30 import org.apache.commons.logging.Log;
31
32 import org.objectweb.easybeans.i18n.I18n;
33
34 /**
35  * Wrapping around a commons logger allowing to use internationalization and MessageFormat API.
36  * @author Florent Benoit
37  */

38 public class CommonsLoggerImpl implements JLog {
39
40     /**
41      * Id for serializable class.
42      */

43     private static final long serialVersionUID = 1707614695786252792L;
44
45     /**
46      * Wrapped commons logging logger.
47      */

48     private Log wrapped;
49
50     /**
51      * Instance of I18n.
52      */

53     private I18n i18n = null;
54
55     /**
56      * Build a new logger with the wrapped logger.
57      * @param wrapped the delegate logger.
58      * @param i18n the internationalization object to use, if any.
59      */

60     public CommonsLoggerImpl(final Log wrapped, final I18n i18n) {
61         this.wrapped = wrapped;
62         this.i18n = i18n;
63     }
64
65     /**
66      * <p>
67      * Is debug logging currently enabled?
68      * </p>
69      * <p>
70      * Call this method to prevent having to perform expensive operations (for
71      * example, <code>String</code> concatenation) when the log level is more
72      * than debug.
73      * </p>
74      * @return true if it is enabled, else false
75      */

76     public boolean isDebugEnabled() {
77         return wrapped.isDebugEnabled();
78     }
79
80     /**
81      * <p>
82      * Is error logging currently enabled?
83      * </p>
84      * <p>
85      * Call this method to prevent having to perform expensive operations (for
86      * example, <code>String</code> concatenation) when the log level is more
87      * than error.
88      * </p>
89      * @return true if it is enabled, else false
90      */

91     public boolean isErrorEnabled() {
92         return wrapped.isErrorEnabled();
93     }
94
95     /**
96      * <p>
97      * Is fatal logging currently enabled?
98      * </p>
99      * <p>
100      * Call this method to prevent having to perform expensive operations (for
101      * example, <code>String</code> concatenation) when the log level is more
102      * than fatal.
103      * </p>
104      * @return true if it is enabled, else false
105      */

106     public boolean isFatalEnabled() {
107         return wrapped.isFatalEnabled();
108     }
109
110     /**
111      * <p>
112      * Is info logging currently enabled?
113      * </p>
114      * <p>
115      * Call this method to prevent having to perform expensive operations (for
116      * example, <code>String</code> concatenation) when the log level is more
117      * than info.
118      * </p>
119      * @return true if it is enabled, else false
120      */

121     public boolean isInfoEnabled() {
122         return wrapped.isInfoEnabled();
123     }
124
125     /**
126      * <p>
127      * Is trace logging currently enabled?
128      * </p>
129      * <p>
130      * Call this method to prevent having to perform expensive operations (for
131      * example, <code>String</code> concatenation) when the log level is more
132      * than trace.
133      * </p>
134      * @return true if it is enabled, else false
135      */

136     public boolean isTraceEnabled() {
137         return wrapped.isDebugEnabled();
138     }
139
140     /**
141      * <p>
142      * Is warn logging currently enabled?
143      * </p>
144      * <p>
145      * Call this method to prevent having to perform expensive operations (for
146      * example, <code>String</code> concatenation) when the log level is more
147      * than warn.
148      * </p>
149      * @return true if it is enabled, else false
150      */

151     public boolean isWarnEnabled() {
152         return wrapped.isWarnEnabled();
153     }
154
155     /**
156      * <p>
157      * Log a message with trace log level.
158      * </p>
159      * @param message log this message
160      */

161     public void trace(final Object JavaDoc message) {
162         wrapped.trace(message);
163     }
164
165     /**
166      * <p>
167      * Log a message with trace log level.
168      * </p>
169      * @param message - This could be
170      * <ul>
171      * <li>an entry of an I18n repository</li>
172      * <li>a preformated message given to MessageFormat class</li>
173      * <li>or a simple object on which toString() method will be called</li>
174      * </ul>
175      * @param args could be empty or contains the object for the formatter (I18n
176      * case). To log an exception, the exception must be the last
177      * argument.
178      */

179     public void trace(final Object JavaDoc message, final Object JavaDoc... args) {
180         // Do nothing is level is not enabled
181
if (!wrapped.isTraceEnabled()) {
182             return;
183         }
184         String JavaDoc msg = getMessage(message, args);
185         Throwable JavaDoc t = getThrowable(args);
186         if (t != null) {
187             wrapped.trace(msg, t);
188         } else {
189             wrapped.trace(msg);
190         }
191     }
192
193     /**
194      * <p>
195      * Log a message with debug log level.
196      * </p>
197      * @param message - This could be
198      * <ul>
199      * <li>an entry of an I18n repository</li>
200      * <li>a preformated message given to MessageFormat class</li>
201      * <li>or a simple object on which toString() method will be called</li>
202      * </ul>
203      * @param args could be empty or contains the object for the formatter (I18n
204      * case). To log an exception, the exception must be the last
205      * argument.
206      */

207     public void debug(final Object JavaDoc message, final Object JavaDoc... args) {
208         // Do nothing is level is not enabled
209
if (!wrapped.isDebugEnabled()) {
210             return;
211         }
212         String JavaDoc msg = getMessage(message, args);
213         Throwable JavaDoc t = getThrowable(args);
214         if (t != null) {
215             wrapped.debug(msg, t);
216         } else {
217             wrapped.debug(msg);
218         }
219     }
220
221     /**
222      * <p>
223      * Log a message with info log level.
224      * </p>
225      * @param message - This could be
226      * <ul>
227      * <li>an entry of an I18n repository</li>
228      * <li>a preformated message given to MessageFormat class</li>
229      * <li>or a simple object on which toString() method will be called</li>
230      * </ul>
231      * @param args could be empty or contains the object for the formatter (I18n
232      * case). To log an exception, the exception must be the last
233      * argument.
234      */

235     public void info(final Object JavaDoc message, final Object JavaDoc... args) {
236         // Do nothing is level is not enabled
237
if (!wrapped.isInfoEnabled()) {
238             return;
239         }
240
241         String JavaDoc msg = getMessage(message, args);
242         Throwable JavaDoc t = getThrowable(args);
243         if (t != null) {
244             wrapped.info(msg, t);
245         } else {
246             wrapped.info(msg);
247         }
248
249     }
250
251     /**
252      * <p>
253      * Log a message with warn log level.
254      * </p>
255      * @param message - This could be
256      * <ul>
257      * <li>an entry of an I18n repository</li>
258      * <li>a preformated message given to MessageFormat class</li>
259      * <li>or a simple object on which toString() method will be called</li>
260      * </ul>
261      * @param args could be empty or contains the object for the formatter (I18n
262      * case). To log an exception, the exception must be the last
263      * argument.
264      */

265     public void warn(final Object JavaDoc message, final Object JavaDoc... args) {
266         // Do nothing is level is not enabled
267
if (!wrapped.isWarnEnabled()) {
268             return;
269         }
270
271         String JavaDoc msg = getMessage(message, args);
272         Throwable JavaDoc t = getThrowable(args);
273         if (t != null) {
274             wrapped.warn(msg, t);
275         } else {
276             wrapped.warn(msg);
277         }
278
279     }
280
281     /**
282      * <p>
283      * Log a message with error log level.
284      * </p>
285      * @param message - This could be
286      * <ul>
287      * <li>an entry of an I18n repository</li>
288      * <li>a preformated message given to MessageFormat class</li>
289      * <li>or a simple object on which toString() method will be called</li>
290      * </ul>
291      * @param args could be empty or contains the object for the formatter (I18n
292      * case). To log an exception, the exception must be the last
293      * argument.
294      */

295     public void error(final Object JavaDoc message, final Object JavaDoc... args) {
296         // Do nothing is level is not enabled
297
if (!wrapped.isErrorEnabled()) {
298             return;
299         }
300
301         String JavaDoc msg = getMessage(message, args);
302         Throwable JavaDoc t = getThrowable(args);
303         if (t != null) {
304             wrapped.error(msg, t);
305         } else {
306             wrapped.error(msg);
307         }
308
309     }
310
311     /**
312      * <p>
313      * Log a message with fatal log level.
314      * </p>
315      * @param message - This could be
316      * <ul>
317      * <li>an entry of an I18n repository</li>
318      * <li>a preformated message given to MessageFormat class</li>
319      * <li>or a simple object on which toString() method will be called</li>
320      * </ul>
321      * @param args could be empty or contains the object for the formatter (I18n
322      * case). To log an exception, the exception must be the last
323      * argument.
324      */

325     public void fatal(final Object JavaDoc message, final Object JavaDoc... args) {
326         // Do nothing is level is not enabled
327
if (!wrapped.isFatalEnabled()) {
328             return;
329         }
330
331         String JavaDoc msg = getMessage(message, args);
332         Throwable JavaDoc t = getThrowable(args);
333         if (t != null) {
334             wrapped.fatal(msg, t);
335         } else {
336             wrapped.fatal(msg);
337         }
338
339     }
340
341     /**
342      * Gets the i18n object associated to this logger.
343      * @return i18n object.
344      */

345     public I18n getI18n() {
346         return i18n;
347     }
348
349
350     /**
351      * Gets a string message build with the given args.<br> *
352      * @param message If the message is an i18n entry, this entry will be used
353      * to format the message.<br>
354      * Else, the arguments will be used as string formatter
355      * @param args given args use by the formatter
356      * @return a string representation of the given message.
357      */

358     private String JavaDoc getMessage(final Object JavaDoc message, final Object JavaDoc... args) {
359
360         // I18n present ?
361
if (i18n != null) {
362             // try to format message
363
return i18n.getMessage(String.valueOf(message), args);
364         }
365
366         // else, try to format message
367
return MessageFormat.format(String.valueOf(message), args);
368     }
369
370     /**
371      * Try to find a Throwable object in the array of object args. The throwable
372      * should be the last parameter.
373      * @param args array of objects on which we need to find the Throwable
374      * @return a throwable object if last index if a Throwable or null
375      */

376     private Throwable JavaDoc getThrowable(final Object JavaDoc... args) {
377         int size = args.length;
378         // Detect the size
379
if (size > 0) {
380             Object JavaDoc lastArg = args[size - 1];
381             if (lastArg instanceof Throwable JavaDoc) {
382                 return (Throwable JavaDoc) lastArg;
383             }
384         }
385
386         // not found
387
return null;
388     }
389
390 }
391
Popular Tags