1   /* 
2    * Copyright (c) 2004-2008 QOS.ch
3    * All rights reserved.
4    * 
5    * Permission is hereby granted, free  of charge, to any person obtaining
6    * a  copy  of this  software  and  associated  documentation files  (the
7    * "Software"), to  deal in  the Software without  restriction, including
8    * without limitation  the rights to  use, copy, modify,  merge, publish,
9    * distribute,  sublicense, and/or sell  copies of  the Software,  and to
10   * permit persons to whom the Software  is furnished to do so, subject to
11   * the following conditions:
12   * 
13   * The  above  copyright  notice  and  this permission  notice  shall  be
14   * included in all copies or substantial portions of the Software.
15   * 
16   * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
17   * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
18   * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
19   * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20   * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21   * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
22   * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23   */
24  
25  
26  package org.slf4j.impl;
27  
28  import org.apache.commons.logging.Log;
29  import org.slf4j.Logger;
30  import org.slf4j.helpers.MarkerIgnoringBase;
31  import org.slf4j.helpers.MessageFormatter;
32  
33  /**
34   * A wrapper over {@link org.apache.commons.logging.Log
35   * org.apache.commons.logging.Log} in conformance with the {@link Logger}
36   * interface.
37   * 
38   * @author Ceki Gülcü
39   */
40  public final class JCLLoggerAdapter extends MarkerIgnoringBase {
41    final Log log;
42    final String name;
43    
44    // WARN: JCLLoggerAdapter constructor should have only package access so
45    // that only JCLLoggerFactory be able to create one.
46    JCLLoggerAdapter(Log log, String name) {
47      this.log = log;
48      this.name = name;
49    }
50  
51    public String getName() {
52      return name;
53    }
54  
55    /**
56     * Delegates to the {@link Log#isTraceEnabled} method of the underlying
57     * {@link Log} instance. 
58     */
59    public boolean isTraceEnabled() {
60      return log.isTraceEnabled();
61    }
62  
63    //
64  
65    /**
66     * Delegates to the {@link Log#trace(java.lang.Object)} method of the underlying
67     * {@link Log} instance.
68     * 
69     * @param msg - the message object to be logged
70     */
71    public void trace(String msg) {
72      log.trace(msg);
73    }
74  
75    /**
76     * Delegates to the {@link Log#trace(java.lang.Object)} method of the underlying
77     * {@link Log} instance.
78     * 
79     * <p>
80     * However, this form avoids superfluous object creation when the logger is disabled
81     * for level TRACE.
82     * </p>
83     * 
84     * @param format
85     *          the format string
86     * @param arg
87     *          the argument
88     */
89    public void trace(String format, Object arg) {
90      if (log.isDebugEnabled()) {
91        String msgStr = MessageFormatter.format(format, arg);
92        log.trace(msgStr);
93      }
94    }
95  
96    /**
97     * Delegates to the {@link Log#trace(java.lang.Object)} method of the underlying
98     * {@link Log} instance.
99     * 
100    * <p>
101    * However, this form avoids superfluous object creation when the logger is disabled
102    * for level TRACE.
103    * </p>
104    * 
105    * @param format
106    *          the format string
107    * @param arg1
108    *          the first argument
109    * @param arg2
110    *          the second argument
111    */
112   public void trace(String format, Object arg1, Object arg2) {
113     if (log.isDebugEnabled()) {
114       String msgStr = MessageFormatter.format(format, arg1, arg2);
115       log.trace(msgStr);
116     }
117   }
118   
119 
120   /**
121    * Delegates to the {@link Log#trace(java.lang.Object)} method of the underlying
122    * {@link Log} instance.
123    * 
124    * <p>
125    * However, this form avoids superfluous object creation when the logger is disabled
126    * for level TRACE.
127    * </p>
128    * 
129    * @param format the format string
130    * @param argArray an array of arguments
131    */
132   public void trace(String format, Object[] argArray) {
133     if (log.isDebugEnabled()) {
134       String msgStr = MessageFormatter.arrayFormat(format, argArray);
135       log.trace(msgStr);
136     }
137   }
138   
139   /**
140    * Delegates to the {@link Log#trace(java.lang.Object, java.lang.Throwable)} method of 
141    * the underlying {@link Log} instance.
142    * 
143    * @param msg
144    *          the message accompanying the exception
145    * @param t
146    *          the exception (throwable) to log
147    */
148   public void trace(String msg, Throwable t) {
149       log.trace(msg, t);
150   }
151 
152   
153   /**
154    * Delegates to the {@link Log#isDebugEnabled} method of the underlying
155    * {@link Log} instance. 
156    */
157   public boolean isDebugEnabled() {
158     return log.isDebugEnabled();
159   }
160 
161   //
162 
163   /**
164    * Delegates to the {@link Log#debug(java.lang.Object)} method of the underlying
165    * {@link Log} instance.
166    * 
167    * @param msg - the message object to be logged
168    */
169   public void debug(String msg) {
170     log.debug(msg);
171   }
172 
173   /**
174    * Delegates to the {@link Log#debug(java.lang.Object)} method of the underlying
175    * {@link Log} instance.
176    * 
177    * <p>
178    * However, this form avoids superfluous object creation when the logger is disabled
179    * for level DEBUG.
180    * </p>
181    * 
182    * @param format
183    *          the format string
184    * @param arg
185    *          the argument
186    */
187   public void debug(String format, Object arg) {
188     if (log.isDebugEnabled()) {
189       String msgStr = MessageFormatter.format(format, arg);
190       log.debug(msgStr);
191     }
192   }
193 
194   /**
195    * Delegates to the {@link Log#debug(java.lang.Object)} method of the underlying
196    * {@link Log} instance.
197    * 
198    * <p>
199    * However, this form avoids superfluous object creation when the logger is disabled
200    * for level DEBUG.
201    * </p>
202    * 
203    * @param format
204    *          the format string
205    * @param arg1
206    *          the first argument
207    * @param arg2
208    *          the second argument
209    */
210   public void debug(String format, Object arg1, Object arg2) {
211     if (log.isDebugEnabled()) {
212       String msgStr = MessageFormatter.format(format, arg1, arg2);
213       log.debug(msgStr);
214     }
215   }
216   
217 
218   /**
219    * Delegates to the {@link Log#debug(java.lang.Object)} method of the underlying
220    * {@link Log} instance.
221    * 
222    * <p>
223    * However, this form avoids superfluous object creation when the logger is disabled
224    * for level DEBUG.
225    * </p>
226    * 
227    * @param format the format string
228    * @param argArray an array of arguments
229    */
230   public void debug(String format, Object[] argArray) {
231     if (log.isDebugEnabled()) {
232       String msgStr = MessageFormatter.arrayFormat(format, argArray);
233       log.debug(msgStr);
234     }
235   }
236   
237   /**
238    * Delegates to the {@link Log#debug(java.lang.Object, java.lang.Throwable)} method of 
239    * the underlying {@link Log} instance.
240    * 
241    * @param msg
242    *          the message accompanying the exception
243    * @param t
244    *          the exception (throwable) to log
245    */
246   public void debug(String msg, Throwable t) {
247       log.debug(msg, t);
248   }
249 
250   /**
251    * Delegates to the {@link Log#isInfoEnabled} method of the underlying
252    * {@link Log} instance. 
253    */
254   public boolean isInfoEnabled() {
255     return log.isInfoEnabled();
256   }
257 
258   /**
259    * Delegates to the {@link Log#debug(java.lang.Object)} method of the underlying
260    * {@link Log} instance.
261    * 
262    * @param msg - the message object to be logged
263    */
264   public void info(String msg) {
265     log.info(msg);
266   }
267 
268   /**
269    * Delegates to the {@link Log#info(java.lang.Object)} method of the underlying
270    * {@link Log} instance.
271    * 
272    * <p>
273    * However, this form avoids superfluous object creation when the logger is disabled
274    * for level INFO.
275    * </p>
276    * 
277    * @param format
278    *          the format string
279    * @param arg
280    *          the argument
281    */
282 
283   public void info(String format, Object arg) {
284     if (log.isInfoEnabled()) {
285       String msgStr = MessageFormatter.format(format, arg);
286       log.info(msgStr);
287     }
288   }
289   /**
290    * Delegates to the {@link Log#info(java.lang.Object)} method of the underlying
291    * {@link Log} instance.
292    * 
293    * <p>
294    * However, this form avoids superfluous object creation when the logger is disabled
295    * for level INFO.
296    * </p>
297    * 
298    * @param format
299    *          the format string
300    * @param arg1
301    *          the first argument
302    * @param arg2
303    *          the second argument
304    */
305   public void info(String format, Object arg1, Object arg2) {
306     if (log.isInfoEnabled()) {
307       String msgStr = MessageFormatter.format(format, arg1, arg2);
308       log.info(msgStr);
309     }
310   }
311 
312   /**
313    * Delegates to the {@link Log#info(java.lang.Object)} method of the underlying
314    * {@link Log} instance.
315    * 
316    * <p>
317    * However, this form avoids superfluous object creation when the logger is disabled
318    * for level INFO.
319    * </p>
320    * 
321    * @param format the format string
322    * @param argArray an array of arguments
323    */
324   public void info(String format, Object[] argArray) {
325     if (log.isInfoEnabled()) {
326       String msgStr = MessageFormatter.arrayFormat(format, argArray);
327       log.info(msgStr);
328     }
329   }
330   
331   
332   /**
333    * Delegates to the {@link Log#info(java.lang.Object, java.lang.Throwable)} method of 
334    * the underlying {@link Log} instance.
335    * 
336    * @param msg
337    *          the message accompanying the exception
338    * @param t
339    *          the exception (throwable) to log
340    */
341   public void info(String msg, Throwable t) {
342     log.info(msg, t);
343   }
344 
345   /**
346    * Delegates to the {@link Log#isWarnEnabled} method of the underlying
347    * {@link Log} instance. 
348    */
349   public boolean isWarnEnabled() {
350     return log.isWarnEnabled();
351   }
352 
353   /**
354    * Delegates to the {@link Log#warn(java.lang.Object)} method of the underlying
355    * {@link Log} instance.
356    * 
357    * @param msg - the message object to be logged
358    */
359   public void warn(String msg) {
360     log.warn(msg);
361   }
362 
363   /**
364    * Delegates to the {@link Log#warn(java.lang.Object)} method of the underlying
365    * {@link Log} instance.
366    * 
367    * <p>
368    * However, this form avoids superfluous object creation when the logger is disabled
369    * for level WARN.
370    * </p>
371    * 
372    * @param format
373    *          the format string
374    * @param arg
375    *          the argument
376    */
377   public void warn(String format, Object arg) {
378     if (log.isWarnEnabled()) {
379       String msgStr = MessageFormatter.format(format, arg);
380       log.warn(msgStr);
381     }
382   }
383   
384   /**
385    * Delegates to the {@link Log#warn(java.lang.Object)} method of the underlying
386    * {@link Log} instance.
387    * 
388    * <p>
389    * However, this form avoids superfluous object creation when the logger is disabled
390    * for level WARN.
391    * </p>
392    * 
393    * @param format
394    *          the format string
395    * @param arg1
396    *          the first argument
397    * @param arg2
398    *          the second argument
399    */
400   public void warn(String format, Object arg1, Object arg2) {
401     if (log.isWarnEnabled()) {
402       String msgStr = MessageFormatter.format(format, arg1, arg2);
403       log.warn(msgStr);
404     }
405   }
406   
407   /**
408    * Delegates to the {@link Log#warn(java.lang.Object)} method of the underlying
409    * {@link Log} instance.
410    * 
411    * <p>
412    * However, this form avoids superfluous object creation when the logger is disabled
413    * for level WARN.
414    * </p>
415    * 
416    * @param format the format string
417    * @param argArray an array of arguments
418    */
419   public void warn(String format, Object[] argArray) {
420     if (log.isWarnEnabled()) {
421       String msgStr = MessageFormatter.arrayFormat(format, argArray);
422       log.warn(msgStr);
423     }
424   }
425   
426 
427   /**
428    * Delegates to the {@link Log#warn(java.lang.Object, java.lang.Throwable)} method of 
429    * the underlying {@link Log} instance.
430    * 
431    * @param msg
432    *          the message accompanying the exception
433    * @param t
434    *          the exception (throwable) to log
435    */
436   
437   public void warn(String msg, Throwable t) {
438     log.warn(msg, t);
439   }
440 
441 
442   /**
443    * Delegates to the {@link Log#isErrorEnabled} method of the underlying
444    * {@link Log} instance. 
445    */
446   public boolean isErrorEnabled() {
447     return log.isErrorEnabled();
448   }
449 
450   /**
451    * Delegates to the {@link Log#error(java.lang.Object)} method of the underlying
452    * {@link Log} instance.
453    * 
454    * @param msg - the message object to be logged
455    */
456   public void error(String msg) {
457     log.error(msg);
458   }
459 
460   /**
461    * Delegates to the {@link Log#error(java.lang.Object)} method of the underlying
462    * {@link Log} instance.
463    * 
464    * <p>
465    * However, this form avoids superfluous object creation when the logger is disabled
466    * for level ERROR.
467    * </p>
468    * 
469    * @param format
470    *          the format string
471    * @param arg
472    *          the argument
473    */
474   public void error(String format, Object arg) {
475     if (log.isErrorEnabled()) {
476       String msgStr = MessageFormatter.format(format, arg);
477       log.error(msgStr);
478     }
479   }
480   
481   /**
482    * Delegates to the {@link Log#error(java.lang.Object)} method of the underlying
483    * {@link Log} instance.
484    * 
485    * <p>
486    * However, this form avoids superfluous object creation when the logger is disabled
487    * for level ERROR.
488    * </p>
489    * 
490    * @param format
491    *          the format string
492    * @param arg1
493    *          the first argument
494    * @param arg2
495    *          the second argument
496    */
497   public void error(String format, Object arg1, Object arg2) {
498     if (log.isErrorEnabled()) {
499       String msgStr = MessageFormatter.format(format, arg1, arg2);
500       log.error(msgStr);
501     }
502   }
503 
504   /**
505    * Delegates to the {@link Log#error(java.lang.Object)} method of the underlying
506    * {@link Log} instance.
507    * 
508    * <p>
509    * However, this form avoids superfluous object creation when the logger is disabled
510    * for level ERROR.
511    * </p>
512    * 
513    * @param format the format string
514    * @param argArray an array of arguments
515    */
516   public void error(String format, Object[] argArray) {
517     if (log.isErrorEnabled()) {
518       String msgStr = MessageFormatter.arrayFormat(format, argArray);
519       log.error(msgStr);
520     }
521   }
522   
523   
524   /**
525    * Delegates to the {@link Log#error(java.lang.Object, java.lang.Throwable)} method of 
526    * the underlying {@link Log} instance.
527    * 
528    * @param msg
529    *          the message accompanying the exception
530    * @param t
531    *          the exception (throwable) to log
532    */
533   
534   public void error(String msg, Throwable t) {
535     log.error(msg, t);
536   }
537 
538 }