View Javadoc

1   package uk.org.lidalia.slf4jext;
2   
3   import com.google.common.collect.ImmutableSet;
4   import org.slf4j.Logger;
5   import org.slf4j.Marker;
6   
7   import java.util.HashSet;
8   
9   import static com.google.common.collect.Sets.immutableEnumSet;
10  import static com.google.common.collect.Sets.newHashSet;
11  import static java.util.Arrays.asList;
12  
13  /**
14   * Enum modelling the logical levels implied by the way <a href="http://www.slf4j.org">SLF4J</a> has the same set of methods
15   * repeated with different names.
16   */
17  public enum Level {
18  
19      TRACE {
20          @Override
21          boolean isEnabled(final Logger logger) {
22              return logger.isTraceEnabled();
23          }
24          @Override
25          void log(final Logger logger, final String message) {
26              logger.trace(message);
27          }
28          @Override
29          void log(final Logger logger, final String message, final Object arg1) {
30              logger.trace(message, arg1);
31          }
32          @Override
33          void log(final Logger logger, final String message, final Object arg1, final Object arg2) {
34              logger.trace(message, arg1, arg2);
35          }
36          @Override
37          void log(final Logger logger, final String message, final Object[] args) {
38              logger.trace(message, args);
39          }
40          @Override
41          void log(final Logger logger, final String message, final Throwable throwable) {
42              logger.trace(message, throwable);
43          }
44          @Override
45          boolean isEnabled(final Logger logger, final Marker marker) {
46              return logger.isTraceEnabled(marker);
47          }
48          @Override
49          void log(final Logger logger, final Marker marker, final String message) {
50              logger.trace(marker, message);
51          }
52          @Override
53          void log(final Logger logger, final Marker marker, final String message, final Object arg1) {
54              logger.trace(marker, message, arg1);
55          }
56          @Override
57          void log(final Logger logger, final Marker marker, final String message, final Object arg1, final Object arg2) {
58              logger.trace(marker, message, arg1, arg2);
59          }
60          @Override
61          void log(final Logger logger, final Marker marker, final String message, final Object[] args) {
62              logger.trace(marker, message, args);
63          }
64          @Override
65          void log(final Logger logger, final Marker marker, final String message, final Throwable throwable) {
66              logger.trace(marker, message, throwable);
67          }
68      },
69  
70      DEBUG {
71          @Override
72          boolean isEnabled(final Logger logger) {
73              return logger.isDebugEnabled();
74          }
75          @Override
76          void log(final Logger logger, final String message) {
77              logger.debug(message);
78          }
79          @Override
80          void log(final Logger logger, final String message, final Object arg1) {
81              logger.debug(message, arg1);
82          }
83          @Override
84          void log(final Logger logger, final String message, final Object arg1, final Object arg2) {
85              logger.debug(message, arg1, arg2);
86          }
87          @Override
88          void log(final Logger logger, final String message, final Object[] args) {
89              logger.debug(message, args);
90          }
91          @Override
92          void log(final Logger logger, final String message, final Throwable throwable) {
93              logger.debug(message, throwable);
94          }
95          @Override
96          boolean isEnabled(final Logger logger, final Marker marker) {
97              return logger.isDebugEnabled(marker);
98          }
99          @Override
100         void log(final Logger logger, final Marker marker, final String message) {
101             logger.debug(marker, message);
102         }
103         @Override
104         void log(final Logger logger, final Marker marker, final String message, final Object arg1) {
105             logger.debug(marker, message, arg1);
106         }
107         @Override
108         void log(final Logger logger, final Marker marker, final String message, final Object arg1, final Object arg2) {
109             logger.debug(marker, message, arg1, arg2);
110         }
111         @Override
112         void log(final Logger logger, final Marker marker, final String message, final Object[] args) {
113             logger.debug(marker, message, args);
114         }
115         @Override
116         void log(final Logger logger, final Marker marker, final String message, final Throwable throwable) {
117             logger.debug(marker, message, throwable);
118         }
119     },
120 
121     INFO {
122         @Override
123         boolean isEnabled(final Logger logger) {
124             return logger.isInfoEnabled();
125         }
126         @Override
127         void log(final Logger logger, final String message) {
128             logger.info(message);
129         }
130         @Override
131         void log(final Logger logger, final String message, final Object arg1) {
132             logger.info(message, arg1);
133         }
134         @Override
135         void log(final Logger logger, final String message, final Object arg1, final Object arg2) {
136             logger.info(message, arg1, arg2);
137         }
138         @Override
139         void log(final Logger logger, final String message, final Object[] args) {
140             logger.info(message, args);
141         }
142         @Override
143         void log(final Logger logger, final String message, final Throwable throwable) {
144             logger.info(message, throwable);
145         }
146         @Override
147         boolean isEnabled(final Logger logger, final Marker marker) {
148             return logger.isInfoEnabled(marker);
149         }
150         @Override
151         void log(final Logger logger, final Marker marker, final String message) {
152             logger.info(marker, message);
153         }
154         @Override
155         void log(final Logger logger, final Marker marker, final String message, final Object arg1) {
156             logger.info(marker, message, arg1);
157         }
158         @Override
159         void log(final Logger logger, final Marker marker, final String message, final Object arg1, final Object arg2) {
160             logger.info(marker, message, arg1, arg2);
161         }
162         @Override
163         void log(final Logger logger, final Marker marker, final String message, final Object[] args) {
164             logger.info(marker, message, args);
165         }
166         @Override
167         void log(final Logger logger, final Marker marker, final String message, final Throwable throwable) {
168             logger.info(marker, message, throwable);         }
169     },
170 
171     WARN {
172         @Override
173         boolean isEnabled(final Logger logger) {
174             return logger.isWarnEnabled();
175         }
176         @Override
177         void log(final Logger logger, final String message) {
178             logger.warn(message);
179         }
180         @Override
181         void log(final Logger logger, final String message, final Object arg1) {
182             logger.warn(message, arg1);
183         }
184         @Override
185         void log(final Logger logger, final String message, final Object arg1, final Object arg2) {
186             logger.warn(message, arg1, arg2);
187         }
188         @Override
189         void log(final Logger logger, final String message, final Object[] args) {
190             logger.warn(message, args);
191         }
192         @Override
193         void log(final Logger logger, final String message, final Throwable throwable) {
194             logger.warn(message, throwable);
195         }
196         @Override
197         boolean isEnabled(final Logger logger, final Marker marker) {
198             return logger.isWarnEnabled(marker);
199         }
200         @Override
201         void log(final Logger logger, final Marker marker, final String message) {
202             logger.warn(marker, message);
203         }
204         @Override
205         void log(final Logger logger, final Marker marker, final String message, final Object arg1) {
206             logger.warn(marker, message, arg1);
207         }
208         @Override
209         void log(final Logger logger, final Marker marker, final String message, final Object arg1, final Object arg2) {
210             logger.warn(marker, message, arg1, arg2);
211         }
212         @Override
213         void log(final Logger logger, final Marker marker, final String message, final Object[] args) {
214             logger.warn(marker, message, args);
215         }
216         @Override
217         void log(final Logger logger, final Marker marker, final String message, final Throwable throwable) {
218             logger.warn(marker, message, throwable);
219        }
220     },
221 
222     ERROR {
223         @Override
224         boolean isEnabled(final Logger logger) {
225             return logger.isErrorEnabled();
226        }
227         @Override
228         void log(final Logger logger, final String message) {
229             logger.error(message);
230         }
231         @Override
232         void log(final Logger logger, final String message, final Object arg1) {
233             logger.error(message, arg1);
234         }
235         @Override
236         void log(final Logger logger, final String message, final Object arg1, final Object arg2) {
237             logger.error(message, arg1, arg2);
238         }
239         @Override
240         void log(final Logger logger, final String message, final Object[] args) {
241             logger.error(message, args);
242         }
243         @Override
244         void log(final Logger logger, final String message, final Throwable throwable) {
245             logger.error(message, throwable);
246         }
247         @Override
248         boolean isEnabled(final Logger logger, final Marker marker) {
249             return logger.isErrorEnabled(marker);
250         }
251         @Override
252         void log(final Logger logger, final Marker marker, final String message) {
253             logger.error(marker, message);
254         }
255         @Override
256         void log(final Logger logger, final Marker marker, final String message, final Object arg1) {
257             logger.error(marker, message, arg1);
258         }
259         @Override
260         void log(final Logger logger, final Marker marker, final String message, final Object arg1, final Object arg2) {
261             logger.error(marker, message, arg1, arg2);
262         }
263         @Override
264         void log(final Logger logger, final Marker marker, final String message, final Object[] args) {
265             logger.error(marker, message, args);
266         }
267         @Override
268         void log(final Logger logger, final Marker marker, final String message, final Throwable throwable) {
269             logger.error(marker, message, throwable);
270         }
271     },
272 
273     OFF {
274         @Override
275         boolean isEnabled(final Logger logger) {
276             return false;
277         }
278         @Override
279         void log(final Logger logger, final String message) {
280             // Do nothing
281         }
282         @Override
283         void log(final Logger logger, final String message, final Object arg1) {
284             // Do nothing
285         }
286         @Override
287         void log(final Logger logger, final String message, final Object arg1, final Object arg2) {
288             // Do nothing
289         }
290         @Override
291         void log(final Logger logger, final String message, final Object[] args) {
292             // Do nothing
293         }
294         @Override
295         void log(final Logger logger, final String message, final Throwable throwable) {
296             // Do nothing
297         }
298         @Override
299         boolean isEnabled(final Logger logger, final Marker marker) {
300             return false;
301         }
302         @Override
303         void log(final Logger logger, final Marker marker, final String message) {
304             // Do nothing
305         }
306         @Override
307         void log(final Logger logger, final Marker marker, final String message, final Object arg1) {
308             // Do nothing
309         }
310         @Override
311         void log(final Logger logger, final Marker marker, final String message, final Object arg1, final Object arg2) {
312             // Do nothing
313         }
314         @Override
315         void log(final Logger logger, final Marker marker, final String message, final Object[] args) {
316             // Do nothing
317         }
318         @Override
319         void log(final Logger logger, final Marker marker, final String message, final Throwable throwable) {
320             // Do nothing
321         }
322     };
323 
324     private static final ImmutableSet<Level> ALL_LEVELS = immutableEnumSet(asList(values()));
325     private static final ImmutableSet<Level> ENABLABLE_LEVELS = makeEnablabeValues();
326 
327     abstract boolean isEnabled(Logger logger);
328 
329     abstract void log(Logger logger, String message);
330 
331     abstract void log(Logger logger, String message, Object arg1);
332 
333     abstract void log(Logger logger, String message, Object arg1, Object arg2);
334 
335     abstract void log(Logger logger, String message, Object[] args);
336 
337     abstract void log(Logger logger, String message, Throwable throwable);
338 
339     abstract boolean isEnabled(Logger logger, Marker marker);
340 
341     abstract void log(final Logger logger, Marker marker, final String message);
342 
343     abstract void log(Logger logger, Marker marker, String message, Object arg1);
344 
345     abstract void log(Logger logger, Marker marker, String message, Object arg1, Object arg2);
346 
347     abstract void log(Logger logger, Marker marker, String message, Object[] args);
348 
349     abstract void log(Logger logger, Marker marker, String message, Throwable throwable);
350 
351     /**
352      * @return an ImmutableSet containing the constants of this enum type
353      */
354     public static ImmutableSet<Level> valueSet() {
355         return ALL_LEVELS;
356     }
357 
358     /**
359      * @return an ImmutableSet containing the constants of this enum type other than OFF
360      */
361     public static ImmutableSet<Level> enablableValueSet() {
362         return ENABLABLE_LEVELS;
363     }
364 
365     private static ImmutableSet<Level> makeEnablabeValues() {
366         final HashSet<Level> levels = newHashSet(values());
367         levels.remove(OFF);
368         return immutableEnumSet(levels);
369     }
370 }