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