View Javadoc

1   package uk.org.lidalia.slf4jext;
2   
3   import org.slf4j.Marker;
4   
5   /**
6    * A decorator around an SLF4J {@link org.slf4j.Logger} that adds a set of methods called log that take an instance of Level as
7    * the first argument and delegate to the appropriate method on the SLF4J Logger (info for INFO, etc).
8    *
9    * All methods defined by the SLF4J {@link org.slf4j.Logger} interface delegate directly to the decorated SLF4J
10   * {@link org.slf4j.Logger}.
11   */
12  @SuppressWarnings({ "PMD.ExcessivePublicCount", "PMD.TooManyMethods" })
13  public class Logger implements org.slf4j.Logger {
14  
15      private final org.slf4j.Logger decorated;
16  
17      /**
18       * Returns a new Logger that decorates the provided SLF4J {@link org.slf4j.Logger}.
19       *
20       * It is generally more convenient to call {@link LoggerFactory#getLogger(Class)} or {@link LoggerFactory#getLogger(String)}.
21       *
22       * @param decorated the SLF4J {@link org.slf4j.Logger} to which all logging calls will be delegated
23       */
24      public Logger(final org.slf4j.Logger decorated) {
25          this.decorated = decorated;
26      }
27  
28      /**
29       * Is the logger instance enabled for a given level?
30       *
31       * Delegates to the appropriate method for the level - for instance, if the provided level is {@link Level#DEBUG}, this
32       * method will delegate to {@link org.slf4j.Logger#isDebugEnabled()} on the decorated logger.
33       *
34       * @param level the level that may or may not be enabled
35       * @return True if this Logger is enabled for the given level,
36       *         false otherwise.
37       */
38      public boolean isEnabled(final Level level) {
39          return level.isEnabled(decorated);
40      }
41  
42      /**
43       * Delegates to the appropriate logging method for the level.
44       *
45       * For instance, if the provided level is {@link Level#DEBUG}, this method will delegate to
46       * {@link org.slf4j.Logger#debug(String)} on the decorated logger.
47       *
48       * @param level the level at which to log
49       * @param msg the message string to be logged
50       */
51      public void log(final Level level, final String msg) {
52          level.log(decorated, msg);
53      }
54  
55      /**
56       * Delegates to the appropriate logging method for the level.
57       *
58       * For instance, if the provided level is {@link Level#DEBUG}, this method will delegate to
59       * {@link org.slf4j.Logger#debug(String, Object)} on the decorated logger.
60       *
61       * @param level the level at which to log
62       * @param format the format string
63       * @param arg the argument
64       */
65      public void log(final Level level, final String format, final Object arg) {
66          level.log(decorated, format, arg);
67      }
68  
69      /**
70       * Delegates to the appropriate logging method for the level.
71       *
72       * For instance, if the provided level is {@link Level#DEBUG}, this method will delegate to
73       * {@link org.slf4j.Logger#debug(String, Object, Object)} on the decorated logger.
74       *
75       * @param level the level at which to log
76       * @param format the format string
77       * @param arg1 the first argument
78       * @param arg2 the second argument
79       */
80      public void log(final Level level, final String format, final Object arg1, final Object arg2) {
81          level.log(decorated, format, arg1, arg2);
82      }
83  
84      /**
85       * Delegates to the appropriate logging method for the level.
86       *
87       * For instance, if the provided level is {@link Level#DEBUG}, this method will delegate to
88       * {@link org.slf4j.Logger#debug(String, Object...)} on the decorated logger.
89       *
90       * @param level the level at which to log
91       * @param format the format string
92       * @param arguments a list of 3 or more arguments
93       */
94      public void log(final Level level, final String format, final Object... arguments) {
95          level.log(decorated, format, arguments);
96      }
97  
98      /**
99       * Delegates to the appropriate logging method for the level.
100      *
101      * For instance, if the provided level is {@link Level#DEBUG}, this method will delegate to
102      * {@link org.slf4j.Logger#debug(String, Throwable)} on the decorated logger.
103      *
104      * @param level the level at which to log
105      * @param msg the message string to be logged
106      * @param throwable the exception (throwable) to log
107      */
108     public void log(final Level level, final String msg, final Throwable throwable) {
109         level.log(decorated, msg, throwable);
110     }
111 
112     /**
113      * Similar to {@link #isTraceEnabled()} method except that the
114      * marker data is also taken into account.
115      *
116      * Delegates to the appropriate method for the level - for instance, if the provided level is {@link Level#DEBUG}, this
117      * method will delegate to {@link org.slf4j.Logger#isDebugEnabled(Marker)} on the decorated logger.
118      *
119      * @param level the level that may or may not be enabled
120      * @param marker The marker data to take into consideration
121      * @return True if this Logger is enabled for the given level,
122      *         false otherwise.
123      */
124     public boolean isEnabled(final Level level, final Marker marker) {
125         return level.isEnabled(decorated, marker);
126     }
127 
128     /**
129      * Delegates to the appropriate logging method for the level.
130      *
131      * For instance, if the provided level is {@link Level#DEBUG}, this method will delegate to
132      * {@link org.slf4j.Logger#debug(Marker, String)} on the decorated logger.
133      *
134      * @param level the level at which to log
135      * @param marker The marker data to take into consideration
136      * @param msg the message string to be logged
137      */
138     public void log(final Level level, final Marker marker, final String msg) {
139         level.log(decorated, marker, msg);
140     }
141 
142     /**
143      * Delegates to the appropriate logging method for the level.
144      *
145      * For instance, if the provided level is {@link Level#DEBUG}, this method will delegate to
146      * {@link org.slf4j.Logger#debug(String, Object)} on the decorated logger.
147      *
148      * @param level the level at which to log
149      * @param marker The marker data to take into consideration
150      * @param format the format string
151      * @param arg the argument
152      */
153     public void log(final Level level, final Marker marker, final String format, final Object arg) {
154         level.log(decorated, marker, format, arg);
155     }
156 
157     /**
158      * Delegates to the appropriate logging method for the level.
159      *
160      * For instance, if the provided level is {@link Level#DEBUG}, this method will delegate to
161      * {@link org.slf4j.Logger#debug(String, Object, Object)} on the decorated logger.
162      *
163      * @param level the level at which to log
164      * @param marker The marker data to take into consideration
165      * @param format the format string
166      * @param arg1 the first argument
167      * @param arg2 the second argument
168      */
169     public void log(final Level level, final Marker marker, final String format, final Object arg1, final Object arg2) {
170         level.log(decorated, marker, format, arg1, arg2);
171     }
172 
173     /**
174      * Delegates to the appropriate logging method for the level.
175      *
176      * For instance, if the provided level is {@link Level#DEBUG}, this method will delegate to
177      * {@link org.slf4j.Logger#debug(String, Object...)} on the decorated logger.
178      *
179      * @param level the level at which to log
180      * @param marker The marker data to take into consideration
181      * @param format the format string
182      * @param arguments a list of 3 or more arguments
183      */
184     public void log(final Level level, final Marker marker, final String format, final Object... arguments) {
185         level.log(decorated, marker, format, arguments);
186     }
187 
188     /**
189      * Delegates to the appropriate logging method for the level.
190      *
191      * For instance, if the provided level is {@link Level#DEBUG}, this method will delegate to
192      * {@link org.slf4j.Logger#debug(String, Throwable)} on the decorated logger.
193      *
194      * @param level the level at which to log
195      * @param marker The marker data to take into consideration
196      * @param msg the message string to be logged
197      * @param throwable the exception (throwable) to log
198      */
199     public void log(final Level level, final Marker marker, final String msg, final Throwable throwable) {
200         level.log(decorated, marker, msg, throwable);
201     }
202 
203     /* ALL OTHER METHODS SIMPLY DELEGATE TO THE SAME METHOD ON THE DECORATED LOGGER */
204 
205     @Override
206     public String getName() {
207         return decorated.getName();
208     }
209 
210     @Override
211     public boolean isTraceEnabled() {
212         return decorated.isTraceEnabled();
213     }
214 
215     @Override
216     public void trace(final String msg) {
217         decorated.trace(msg);
218     }
219 
220     @Override
221     public void trace(final String format, final Object arg) {
222         decorated.trace(format, arg);
223     }
224 
225     @Override
226     public void trace(final String format, final Object arg1, final Object arg2) {
227         decorated.trace(format, arg1, arg2);
228     }
229 
230     @Override
231     public void trace(final String format, final Object... arguments) {
232         decorated.trace(format, arguments);
233     }
234 
235     @Override
236     public void trace(final String msg, final Throwable throwable) {
237         decorated.trace(msg, throwable);
238     }
239 
240     @Override
241     public boolean isTraceEnabled(final Marker marker) {
242         return decorated.isTraceEnabled(marker);
243     }
244 
245     @Override
246     public void trace(final Marker marker, final String msg) {
247         decorated.trace(marker, msg);
248     }
249 
250     @Override
251     public void trace(final Marker marker, final String format, final Object arg) {
252         decorated.trace(marker, format, arg);
253     }
254 
255     @Override
256     public void trace(final Marker marker, final String format, final Object arg1, final Object arg2) {
257         decorated.trace(marker, format, arg1, arg2);
258     }
259 
260     @Override
261     public void trace(final Marker marker, final String format, final Object... arguments) {
262         decorated.trace(marker, format, arguments);
263     }
264 
265     @Override
266     public void trace(final Marker marker, final String msg, final Throwable throwable) {
267         decorated.trace(marker, msg, throwable);
268     }
269 
270     @Override
271     public boolean isDebugEnabled() {
272         return decorated.isDebugEnabled();
273     }
274 
275     @Override
276     public void debug(final String msg) {
277         decorated.debug(msg);
278     }
279 
280     @Override
281     public void debug(final String format, final Object arg) {
282         decorated.debug(format, arg);
283     }
284 
285     @Override
286     public void debug(final String format, final Object arg1, final Object arg2) {
287         decorated.debug(format, arg1, arg2);
288     }
289 
290     @Override
291     public void debug(final String format, final Object... arguments) {
292         decorated.debug(format, arguments);
293     }
294 
295     @Override
296     public void debug(final String msg, final Throwable throwable) {
297         decorated.debug(msg, throwable);
298     }
299 
300     @Override
301     public boolean isDebugEnabled(final Marker marker) {
302         return decorated.isDebugEnabled(marker);
303     }
304 
305     @Override
306     public void debug(final Marker marker, final String msg) {
307         decorated.debug(marker, msg);
308     }
309 
310     @Override
311     public void debug(final Marker marker, final String format, final Object arg) {
312         decorated.debug(marker, format, arg);
313     }
314 
315     @Override
316     public void debug(final Marker marker, final String format, final Object arg1, final Object arg2) {
317         decorated.debug(marker, format, arg1, arg2);
318     }
319 
320     @Override
321     public void debug(final Marker marker, final String format, final Object... arguments) {
322         decorated.debug(marker, format, arguments);
323     }
324 
325     @Override
326     public void debug(final Marker marker, final String msg, final Throwable throwable) {
327         decorated.debug(marker, msg, throwable);
328     }
329 
330     @Override
331     public boolean isInfoEnabled() {
332         return decorated.isInfoEnabled();
333     }
334 
335     @Override
336     public void info(final String msg) {
337         decorated.info(msg);
338     }
339 
340     @Override
341     public void info(final String format, final Object arg) {
342         decorated.info(format, arg);
343     }
344 
345     @Override
346     public void info(final String format, final Object arg1, final Object arg2) {
347         decorated.info(format, arg1, arg2);
348     }
349 
350     @Override
351     public void info(final String format, final Object... arguments) {
352         decorated.info(format, arguments);
353     }
354 
355     @Override
356     public void info(final String msg, final Throwable throwable) {
357         decorated.info(msg, throwable);
358     }
359 
360     @Override
361     public boolean isInfoEnabled(final Marker marker) {
362         return decorated.isInfoEnabled(marker);
363     }
364 
365     @Override
366     public void info(final Marker marker, final String msg) {
367         decorated.info(marker, msg);
368     }
369 
370     @Override
371     public void info(final Marker marker, final String format, final Object arg) {
372         decorated.info(marker, format, arg);
373     }
374 
375     @Override
376     public void info(final Marker marker, final String format, final Object arg1, final Object arg2) {
377         decorated.info(marker, format, arg1, arg2);
378     }
379 
380     @Override
381     public void info(final Marker marker, final String format, final Object... arguments) {
382         decorated.info(marker, format, arguments);
383     }
384 
385     @Override
386     public void info(final Marker marker, final String msg, final Throwable throwable) {
387         decorated.info(marker, msg, throwable);
388     }
389 
390     @Override
391     public boolean isWarnEnabled() {
392         return decorated.isWarnEnabled();
393     }
394 
395     @Override
396     public void warn(final String msg) {
397         decorated.warn(msg);
398     }
399 
400     @Override
401     public void warn(final String format, final Object arg) {
402         decorated.warn(format, arg);
403     }
404 
405     @Override
406     public void warn(final String format, final Object... arguments) {
407         decorated.warn(format, arguments);
408     }
409 
410     @Override
411     public void warn(final String format, final Object arg1, final Object arg2) {
412         decorated.warn(format, arg1, arg2);
413     }
414 
415     @Override
416     public void warn(final String msg, final Throwable throwable) {
417         decorated.warn(msg, throwable);
418     }
419 
420     @Override
421     public boolean isWarnEnabled(final Marker marker) {
422         return decorated.isWarnEnabled(marker);
423     }
424 
425     @Override
426     public void warn(final Marker marker, final String msg) {
427         decorated.warn(marker, msg);
428     }
429 
430     @Override
431     public void warn(final Marker marker, final String format, final Object arg) {
432         decorated.warn(marker, format, arg);
433     }
434 
435     @Override
436     public void warn(final Marker marker, final String format, final Object arg1, final Object arg2) {
437         decorated.warn(marker, format, arg1, arg2);
438     }
439 
440     @Override
441     public void warn(final Marker marker, final String format, final Object... arguments) {
442         decorated.warn(marker, format, arguments);
443     }
444 
445     @Override
446     public void warn(final Marker marker, final String msg, final Throwable throwable) {
447         decorated.warn(marker, msg, throwable);
448     }
449 
450     @Override
451     public boolean isErrorEnabled() {
452         return decorated.isErrorEnabled();
453     }
454 
455     @Override
456     public void error(final String msg) {
457         decorated.error(msg);
458     }
459 
460     @Override
461     public void error(final String format, final Object arg) {
462         decorated.error(format, arg);
463     }
464 
465     @Override
466     public void error(final String format, final Object arg1, final Object arg2) {
467         decorated.error(format, arg1, arg2);
468     }
469 
470     @Override
471     public void error(final String format, final Object... arguments) {
472         decorated.error(format, arguments);
473     }
474 
475     @Override
476     public void error(final String msg, final Throwable throwable) {
477         decorated.error(msg, throwable);
478     }
479 
480     @Override
481     public boolean isErrorEnabled(final Marker marker) {
482         return decorated.isErrorEnabled(marker);
483     }
484 
485     @Override
486     public void error(final Marker marker, final String msg) {
487         decorated.error(marker, msg);
488     }
489 
490     @Override
491     public void error(final Marker marker, final String format, final Object arg) {
492         decorated.error(marker, format, arg);
493     }
494 
495     @Override
496     public void error(final Marker marker, final String format, final Object arg1, final Object arg2) {
497         decorated.error(marker, format, arg1, arg2);
498     }
499 
500     @Override
501     public void error(final Marker marker, final String format, final Object... arguments) {
502         decorated.error(marker, format, arguments);
503     }
504 
505     @Override
506     public void error(final Marker marker, final String msg, final Throwable throwable) {
507         decorated.error(marker, msg, throwable);
508     }
509 }