View Javadoc

1   /*
2    * $Id: AccessibleLog4jLoggerAdapter.java 21415 2011-03-02 17:00:09Z aperepel $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.module.logging;
12  
13  import java.io.Serializable;
14  
15  import org.apache.log4j.Level;
16  import org.slf4j.Marker;
17  import org.slf4j.helpers.FormattingTuple;
18  import org.slf4j.helpers.MarkerIgnoringBase;
19  import org.slf4j.helpers.MessageFormatter;
20  import org.slf4j.spi.LocationAwareLogger;
21  
22  /**
23   * A copy & paste of the {@link AccessibleLog4jLoggerAdapter}, just making it
24   * non-final.
25   */
26  public class AccessibleLog4jLoggerAdapter extends MarkerIgnoringBase implements LocationAwareLogger, Serializable
27  {
28  
29      private static final long serialVersionUID = 6182834493563598289L;
30  
31      final transient org.apache.log4j.Logger logger;
32  
33      /**
34       * Following the pattern discussed in pages 162 through 168 of "The complete
35       * log4j manual".
36       */
37      final static String FQCN = AccessibleLog4jLoggerAdapter.class.getName();
38  
39      // Does the log4j version in use recognize the TRACE level?
40      // The trace level was introduced in log4j 1.2.12.
41      final boolean traceCapable;
42  
43      // WARN: Log4jLoggerAdapter constructor should have only package access so
44      // that
45      // only Log4jLoggerFactory be able to create one.
46      AccessibleLog4jLoggerAdapter(org.apache.log4j.Logger logger)
47      {
48          this.logger = logger;
49          this.name = logger.getName();
50          traceCapable = isTraceCapable();
51      }
52  
53      private boolean isTraceCapable()
54      {
55          try
56          {
57              logger.isTraceEnabled();
58              return true;
59          }
60          catch (NoSuchMethodError e)
61          {
62              return false;
63          }
64      }
65  
66      /**
67       * Is this logger instance enabled for the TRACE level?
68       *
69       * @return True if this Logger is enabled for level TRACE, false otherwise.
70       */
71      public boolean isTraceEnabled()
72      {
73          if (traceCapable)
74          {
75              return logger.isTraceEnabled();
76          }
77          else
78          {
79              return logger.isDebugEnabled();
80          }
81      }
82  
83      /**
84       * Log a message object at level TRACE.
85       *
86       * @param msg - the message object to be logged
87       */
88      public void trace(String msg)
89      {
90          logger.log(FQCN, traceCapable ? Level.TRACE : Level.DEBUG, msg, null);
91      }
92  
93      /**
94       * Log a message at level TRACE according to the specified format and
95       * argument.
96       * <p/>
97       * <p>
98       * This form avoids superfluous object creation when the logger is disabled
99       * for level TRACE.
100      * </p>
101      *
102      * @param format the format string
103      * @param arg    the argument
104      */
105     public void trace(String format, Object arg)
106     {
107         if (isTraceEnabled())
108         {
109             FormattingTuple ft = MessageFormatter.format(format, arg);
110             logger.log(FQCN, traceCapable ? Level.TRACE : Level.DEBUG, ft
111                     .getMessage(), ft.getThrowable());
112         }
113     }
114 
115     /**
116      * Log a message at level TRACE according to the specified format and
117      * arguments.
118      * <p/>
119      * <p>
120      * This form avoids superfluous object creation when the logger is disabled
121      * for the TRACE level.
122      * </p>
123      *
124      * @param format the format string
125      * @param arg1   the first argument
126      * @param arg2   the second argument
127      */
128     public void trace(String format, Object arg1, Object arg2)
129     {
130         if (isTraceEnabled())
131         {
132             FormattingTuple ft = MessageFormatter.format(format, arg1, arg2);
133             logger.log(FQCN, traceCapable ? Level.TRACE : Level.DEBUG, ft
134                     .getMessage(), ft.getThrowable());
135         }
136     }
137 
138     /**
139      * Log a message at level TRACE according to the specified format and
140      * arguments.
141      * <p/>
142      * <p>
143      * This form avoids superfluous object creation when the logger is disabled
144      * for the TRACE level.
145      * </p>
146      *
147      * @param format   the format string
148      * @param argArray an array of arguments
149      */
150     public void trace(String format, Object[] argArray)
151     {
152         if (isTraceEnabled())
153         {
154             FormattingTuple ft = MessageFormatter.arrayFormat(format, argArray);
155             logger.log(FQCN, traceCapable ? Level.TRACE : Level.DEBUG, ft
156                     .getMessage(), ft.getThrowable());
157         }
158     }
159 
160     /**
161      * Log an exception (throwable) at level TRACE with an accompanying message.
162      *
163      * @param msg the message accompanying the exception
164      * @param t   the exception (throwable) to log
165      */
166     public void trace(String msg, Throwable t)
167     {
168         logger.log(FQCN, traceCapable ? Level.TRACE : Level.DEBUG, msg, t);
169     }
170 
171     /**
172      * Is this logger instance enabled for the DEBUG level?
173      *
174      * @return True if this Logger is enabled for level DEBUG, false otherwise.
175      */
176     public boolean isDebugEnabled()
177     {
178         return logger.isDebugEnabled();
179     }
180 
181     /**
182      * Log a message object at level DEBUG.
183      *
184      * @param msg - the message object to be logged
185      */
186     public void debug(String msg)
187     {
188         logger.log(FQCN, Level.DEBUG, msg, null);
189     }
190 
191     /**
192      * Log a message at level DEBUG according to the specified format and
193      * argument.
194      * <p/>
195      * <p>
196      * This form avoids superfluous object creation when the logger is disabled
197      * for level DEBUG.
198      * </p>
199      *
200      * @param format the format string
201      * @param arg    the argument
202      */
203     public void debug(String format, Object arg)
204     {
205         if (logger.isDebugEnabled())
206         {
207             FormattingTuple ft = MessageFormatter.format(format, arg);
208             logger.log(FQCN, Level.DEBUG, ft.getMessage(), ft.getThrowable());
209         }
210     }
211 
212     /**
213      * Log a message at level DEBUG according to the specified format and
214      * arguments.
215      * <p/>
216      * <p>
217      * This form avoids superfluous object creation when the logger is disabled
218      * for the DEBUG level.
219      * </p>
220      *
221      * @param format the format string
222      * @param arg1   the first argument
223      * @param arg2   the second argument
224      */
225     public void debug(String format, Object arg1, Object arg2)
226     {
227         if (logger.isDebugEnabled())
228         {
229             FormattingTuple ft = MessageFormatter.format(format, arg1, arg2);
230             logger.log(FQCN, Level.DEBUG, ft.getMessage(), ft.getThrowable());
231         }
232     }
233 
234     /**
235      * Log a message at level DEBUG according to the specified format and
236      * arguments.
237      * <p/>
238      * <p>
239      * This form avoids superfluous object creation when the logger is disabled
240      * for the DEBUG level.
241      * </p>
242      *
243      * @param format   the format string
244      * @param argArray an array of arguments
245      */
246     public void debug(String format, Object[] argArray)
247     {
248         if (logger.isDebugEnabled())
249         {
250             FormattingTuple ft = MessageFormatter.arrayFormat(format, argArray);
251             logger.log(FQCN, Level.DEBUG, ft.getMessage(), ft.getThrowable());
252         }
253     }
254 
255     /**
256      * Log an exception (throwable) at level DEBUG with an accompanying message.
257      *
258      * @param msg the message accompanying the exception
259      * @param t   the exception (throwable) to log
260      */
261     public void debug(String msg, Throwable t)
262     {
263         logger.log(FQCN, Level.DEBUG, msg, t);
264     }
265 
266     /**
267      * Is this logger instance enabled for the INFO level?
268      *
269      * @return True if this Logger is enabled for the INFO level, false otherwise.
270      */
271     public boolean isInfoEnabled()
272     {
273         return logger.isInfoEnabled();
274     }
275 
276     /**
277      * Log a message object at the INFO level.
278      *
279      * @param msg - the message object to be logged
280      */
281     public void info(String msg)
282     {
283         logger.log(FQCN, Level.INFO, msg, null);
284     }
285 
286     /**
287      * Log a message at level INFO according to the specified format and argument.
288      * <p/>
289      * <p>
290      * This form avoids superfluous object creation when the logger is disabled
291      * for the INFO level.
292      * </p>
293      *
294      * @param format the format string
295      * @param arg    the argument
296      */
297     public void info(String format, Object arg)
298     {
299         if (logger.isInfoEnabled())
300         {
301             FormattingTuple ft = MessageFormatter.format(format, arg);
302             logger.log(FQCN, Level.INFO, ft.getMessage(), ft.getThrowable());
303         }
304     }
305 
306     /**
307      * Log a message at the INFO level according to the specified format and
308      * arguments.
309      * <p/>
310      * <p>
311      * This form avoids superfluous object creation when the logger is disabled
312      * for the INFO level.
313      * </p>
314      *
315      * @param format the format string
316      * @param arg1   the first argument
317      * @param arg2   the second argument
318      */
319     public void info(String format, Object arg1, Object arg2)
320     {
321         if (logger.isInfoEnabled())
322         {
323             FormattingTuple ft = MessageFormatter.format(format, arg1, arg2);
324             logger.log(FQCN, Level.INFO, ft.getMessage(), ft.getThrowable());
325         }
326     }
327 
328     /**
329      * Log a message at level INFO according to the specified format and
330      * arguments.
331      * <p/>
332      * <p>
333      * This form avoids superfluous object creation when the logger is disabled
334      * for the INFO level.
335      * </p>
336      *
337      * @param format   the format string
338      * @param argArray an array of arguments
339      */
340     public void info(String format, Object[] argArray)
341     {
342         if (logger.isInfoEnabled())
343         {
344             FormattingTuple ft = MessageFormatter.arrayFormat(format, argArray);
345             logger.log(FQCN, Level.INFO, ft.getMessage(), ft.getThrowable());
346         }
347     }
348 
349     /**
350      * Log an exception (throwable) at the INFO level with an accompanying
351      * message.
352      *
353      * @param msg the message accompanying the exception
354      * @param t   the exception (throwable) to log
355      */
356     public void info(String msg, Throwable t)
357     {
358         logger.log(FQCN, Level.INFO, msg, t);
359     }
360 
361     /**
362      * Is this logger instance enabled for the WARN level?
363      *
364      * @return True if this Logger is enabled for the WARN level, false otherwise.
365      */
366     public boolean isWarnEnabled()
367     {
368         return logger.isEnabledFor(Level.WARN);
369     }
370 
371     /**
372      * Log a message object at the WARN level.
373      *
374      * @param msg - the message object to be logged
375      */
376     public void warn(String msg)
377     {
378         logger.log(FQCN, Level.WARN, msg, null);
379     }
380 
381     /**
382      * Log a message at the WARN level according to the specified format and
383      * argument.
384      * <p/>
385      * <p>
386      * This form avoids superfluous object creation when the logger is disabled
387      * for the WARN level.
388      * </p>
389      *
390      * @param format the format string
391      * @param arg    the argument
392      */
393     public void warn(String format, Object arg)
394     {
395         if (logger.isEnabledFor(Level.WARN))
396         {
397             FormattingTuple ft = MessageFormatter.format(format, arg);
398             logger.log(FQCN, Level.WARN, ft.getMessage(), ft.getThrowable());
399         }
400     }
401 
402     /**
403      * Log a message at the WARN level according to the specified format and
404      * arguments.
405      * <p/>
406      * <p>
407      * This form avoids superfluous object creation when the logger is disabled
408      * for the WARN level.
409      * </p>
410      *
411      * @param format the format string
412      * @param arg1   the first argument
413      * @param arg2   the second argument
414      */
415     public void warn(String format, Object arg1, Object arg2)
416     {
417         if (logger.isEnabledFor(Level.WARN))
418         {
419             FormattingTuple ft = MessageFormatter.format(format, arg1, arg2);
420             logger.log(FQCN, Level.WARN, ft.getMessage(), ft.getThrowable());
421         }
422     }
423 
424     /**
425      * Log a message at level WARN according to the specified format and
426      * arguments.
427      * <p/>
428      * <p>
429      * This form avoids superfluous object creation when the logger is disabled
430      * for the WARN level.
431      * </p>
432      *
433      * @param format   the format string
434      * @param argArray an array of arguments
435      */
436     public void warn(String format, Object[] argArray)
437     {
438         if (logger.isEnabledFor(Level.WARN))
439         {
440             FormattingTuple ft = MessageFormatter.arrayFormat(format, argArray);
441             logger.log(FQCN, Level.WARN, ft.getMessage(), ft.getThrowable());
442         }
443     }
444 
445     /**
446      * Log an exception (throwable) at the WARN level with an accompanying
447      * message.
448      *
449      * @param msg the message accompanying the exception
450      * @param t   the exception (throwable) to log
451      */
452     public void warn(String msg, Throwable t)
453     {
454         logger.log(FQCN, Level.WARN, msg, t);
455     }
456 
457     /**
458      * Is this logger instance enabled for level ERROR?
459      *
460      * @return True if this Logger is enabled for level ERROR, false otherwise.
461      */
462     public boolean isErrorEnabled()
463     {
464         return logger.isEnabledFor(Level.ERROR);
465     }
466 
467     /**
468      * Log a message object at the ERROR level.
469      *
470      * @param msg - the message object to be logged
471      */
472     public void error(String msg)
473     {
474         logger.log(FQCN, Level.ERROR, msg, null);
475     }
476 
477     /**
478      * Log a message at the ERROR level according to the specified format and
479      * argument.
480      * <p/>
481      * <p>
482      * This form avoids superfluous object creation when the logger is disabled
483      * for the ERROR level.
484      * </p>
485      *
486      * @param format the format string
487      * @param arg    the argument
488      */
489     public void error(String format, Object arg)
490     {
491         if (logger.isEnabledFor(Level.ERROR))
492         {
493             FormattingTuple ft = MessageFormatter.format(format, arg);
494             logger.log(FQCN, Level.ERROR, ft.getMessage(), ft.getThrowable());
495         }
496     }
497 
498     /**
499      * Log a message at the ERROR level according to the specified format and
500      * arguments.
501      * <p/>
502      * <p>
503      * This form avoids superfluous object creation when the logger is disabled
504      * for the ERROR level.
505      * </p>
506      *
507      * @param format the format string
508      * @param arg1   the first argument
509      * @param arg2   the second argument
510      */
511     public void error(String format, Object arg1, Object arg2)
512     {
513         if (logger.isEnabledFor(Level.ERROR))
514         {
515             FormattingTuple ft = MessageFormatter.format(format, arg1, arg2);
516             logger.log(FQCN, Level.ERROR, ft.getMessage(), ft.getThrowable());
517         }
518     }
519 
520     /**
521      * Log a message at level ERROR according to the specified format and
522      * arguments.
523      * <p/>
524      * <p>
525      * This form avoids superfluous object creation when the logger is disabled
526      * for the ERROR level.
527      * </p>
528      *
529      * @param format   the format string
530      * @param argArray an array of arguments
531      */
532     public void error(String format, Object[] argArray)
533     {
534         if (logger.isEnabledFor(Level.ERROR))
535         {
536             FormattingTuple ft = MessageFormatter.arrayFormat(format, argArray);
537             logger.log(FQCN, Level.ERROR, ft.getMessage(), ft.getThrowable());
538         }
539     }
540 
541     /**
542      * Log an exception (throwable) at the ERROR level with an accompanying
543      * message.
544      *
545      * @param msg the message accompanying the exception
546      * @param t   the exception (throwable) to log
547      */
548     public void error(String msg, Throwable t)
549     {
550         logger.log(FQCN, Level.ERROR, msg, t);
551     }
552 
553     public void log(Marker marker, String callerFQCN, int level, String msg,
554                     Object[] argArray, Throwable t)
555     {
556         Level log4jLevel;
557         switch (level)
558         {
559             case LocationAwareLogger.TRACE_INT:
560                 log4jLevel = traceCapable ? Level.TRACE : Level.DEBUG;
561                 break;
562             case LocationAwareLogger.DEBUG_INT:
563                 log4jLevel = Level.DEBUG;
564                 break;
565             case LocationAwareLogger.INFO_INT:
566                 log4jLevel = Level.INFO;
567                 break;
568             case LocationAwareLogger.WARN_INT:
569                 log4jLevel = Level.WARN;
570                 break;
571             case LocationAwareLogger.ERROR_INT:
572                 log4jLevel = Level.ERROR;
573                 break;
574             default:
575                 throw new IllegalStateException("Level number " + level
576                                                 + " is not recognized.");
577         }
578         logger.log(callerFQCN, log4jLevel, msg, t);
579     }
580 
581 }