View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.slf4j.impl;
18  
19  import org.apache.logging.log4j.Level;
20  import org.apache.logging.log4j.message.Message;
21  import org.apache.logging.log4j.message.ParameterizedMessage;
22  import org.apache.logging.log4j.message.SimpleMessage;
23  import org.apache.logging.log4j.spi.AbstractLogger;
24  import org.apache.logging.log4j.spi.AbstractLoggerWrapper;
25  import org.slf4j.Marker;
26  import org.slf4j.MarkerFactory;
27  import org.slf4j.helpers.EventDataConverter;
28  import org.slf4j.spi.LocationAwareLogger;
29  
30  /**
31   *
32   */
33  public class SLF4JLogger implements LocationAwareLogger {
34  
35      private static final String FQCN = SLF4JLogger.class.getName();
36      private static final Marker EVENT_MARKER = MarkerFactory.getMarker("EVENT");
37      private final boolean eventLogger;
38      private final AbstractLoggerWrapper logger;
39      private final String name;
40      private final EventDataConverter converter;
41  
42      public SLF4JLogger(final AbstractLogger logger, final String name) {
43          this.logger = new AbstractLoggerWrapper(logger, name, null);
44          this.eventLogger = "EventLogger".equals(name);
45          this.name = name;
46          this.converter = createConverter();
47      }
48  
49      @Override
50      public void trace(final String format) {
51          if (logger.isTraceEnabled()) {
52              logger.log(null, FQCN, Level.TRACE, new SimpleMessage(format), null);
53          }
54      }
55  
56      @Override
57      public void trace(final String format, final Object o) {
58          if (logger.isTraceEnabled()) {
59              logger.log(null, FQCN, Level.TRACE, new ParameterizedMessage(format, o), null);
60          }
61      }
62  
63      @Override
64      public void trace(final String format, final Object arg1, final Object arg2) {
65          if (logger.isTraceEnabled()) {
66              final ParameterizedMessage msg = new ParameterizedMessage(format, arg1, arg2);
67              logger.log(null, FQCN, Level.TRACE, msg, msg.getThrowable());
68          }
69      }
70  
71      @Override
72      public void trace(final String format, final Object... args) {
73          if (logger.isTraceEnabled()) {
74              final ParameterizedMessage msg = new ParameterizedMessage(format, args);
75              logger.log(null, FQCN, Level.TRACE, msg, msg.getThrowable());
76          }
77      }
78  
79      @Override
80      public void trace(final String format, final Throwable t) {
81          if (logger.isTraceEnabled()) {
82              logger.log(null, FQCN, Level.TRACE, new SimpleMessage(format), t);
83          }
84      }
85  
86      @Override
87      public boolean isTraceEnabled() {
88          return logger.isTraceEnabled();
89      }
90  
91      @Override
92      public boolean isTraceEnabled(final Marker marker) {
93          return logger.isTraceEnabled((org.apache.logging.log4j.Marker) marker);
94      }
95  
96      @Override
97      public void trace(final Marker marker, final String s) {
98          if (isTraceEnabled(marker)) {
99              logger.log((org.apache.logging.log4j.Marker) marker, FQCN, Level.TRACE, new SimpleMessage(s), null);
100         }
101     }
102 
103     @Override
104     public void trace(final Marker marker, final String s, final Object o) {
105         if (isTraceEnabled(marker)) {
106             logger.log((org.apache.logging.log4j.Marker) marker, FQCN, Level.TRACE,
107                 new ParameterizedMessage(s, o), null);
108         }
109     }
110 
111     @Override
112     public void trace(final Marker marker, final String s, final Object o, final Object o1) {
113         if (isTraceEnabled(marker)) {
114             final ParameterizedMessage msg = new ParameterizedMessage(s, o, o1);
115             logger.log((org.apache.logging.log4j.Marker) marker, FQCN, Level.TRACE, msg, msg.getThrowable());
116         }
117     }
118 
119     @Override
120     public void trace(final Marker marker, final String s, final Object... objects) {
121         if (isTraceEnabled(marker)) {
122             final ParameterizedMessage msg = new ParameterizedMessage(s, objects);
123             logger.log((org.apache.logging.log4j.Marker) marker, FQCN, Level.TRACE, msg, msg.getThrowable());
124         }
125     }
126 
127     @Override
128     public void trace(final Marker marker, final String s, final Throwable throwable) {
129         if (isTraceEnabled(marker)) {
130             logger.log((org.apache.logging.log4j.Marker) marker, FQCN, Level.TRACE,
131                 new SimpleMessage(s), throwable);
132         }
133     }
134 
135     @Override
136     public void debug(final String format) {
137         if (logger.isDebugEnabled()) {
138             logger.log(null, FQCN, Level.DEBUG, new SimpleMessage(format), null);
139         }
140     }
141 
142     @Override
143     public void debug(final String format, final Object o) {
144         if (logger.isDebugEnabled()) {
145             logger.log(null, FQCN, Level.DEBUG, new ParameterizedMessage(format, o), null);
146         }
147     }
148 
149     @Override
150     public void debug(final String format, final Object arg1, final Object arg2) {
151         if (logger.isDebugEnabled()) {
152             final ParameterizedMessage msg = new ParameterizedMessage(format, arg1, arg2);
153             logger.log(null, FQCN, Level.DEBUG, msg, msg.getThrowable());
154         }
155     }
156 
157     @Override
158     public void debug(final String format, final Object... args) {
159         if (logger.isDebugEnabled()) {
160             final ParameterizedMessage msg = new ParameterizedMessage(format, args);
161             logger.log(null, FQCN, Level.DEBUG, msg, msg.getThrowable());
162         }
163     }
164 
165     @Override
166     public void debug(final String format, final Throwable t) {
167         if (logger.isDebugEnabled()) {
168             logger.log(null, FQCN, Level.DEBUG, new SimpleMessage(format), t);
169         }
170     }
171 
172     @Override
173     public boolean isDebugEnabled() {
174         return logger.isDebugEnabled();
175     }
176 
177     @Override
178     public boolean isDebugEnabled(final Marker marker) {
179         return logger.isDebugEnabled((org.apache.logging.log4j.Marker) marker);
180     }
181 
182     @Override
183     public void debug(final Marker marker, final String s) {
184         if (isDebugEnabled(marker)) {
185             logger.log((org.apache.logging.log4j.Marker) marker, FQCN, Level.DEBUG, new SimpleMessage(s), null);
186         }
187     }
188 
189     @Override
190     public void debug(final Marker marker, final String s, final Object o) {
191         if (isDebugEnabled(marker)) {
192             logger.log((org.apache.logging.log4j.Marker) marker, FQCN, Level.DEBUG,
193                 new ParameterizedMessage(s, o), null);
194         }
195     }
196 
197     @Override
198     public void debug(final Marker marker, final String s, final Object o, final Object o1) {
199         if (isDebugEnabled(marker)) {
200             final ParameterizedMessage msg = new ParameterizedMessage(s, o, o1);
201             logger.log((org.apache.logging.log4j.Marker) marker, FQCN, Level.DEBUG, msg, msg.getThrowable());
202         }
203     }
204 
205     @Override
206     public void debug(final Marker marker, final String s, final Object... objects) {
207         if (isDebugEnabled(marker)) {
208             final ParameterizedMessage msg = new ParameterizedMessage(s, objects);
209             logger.log((org.apache.logging.log4j.Marker) marker, FQCN, Level.DEBUG, msg, msg.getThrowable());
210         }
211     }
212 
213     @Override
214     public void debug(final Marker marker, final String s, final Throwable throwable) {
215         if (isDebugEnabled(marker)) {
216             logger.log((org.apache.logging.log4j.Marker) marker, FQCN, Level.DEBUG,
217                 new SimpleMessage(s), throwable);
218         }
219     }
220 
221     @Override
222     public void info(final String format) {
223         if (logger.isInfoEnabled()) {
224             logger.log(null, FQCN, Level.INFO, new SimpleMessage(format), null);
225         }
226     }
227 
228     @Override
229     public void info(final String format, final Object o) {
230         if (logger.isInfoEnabled()) {
231             logger.log(null, FQCN, Level.INFO, new ParameterizedMessage(format, o), null);
232         }
233     }
234 
235     @Override
236     public void info(final String format, final Object arg1, final Object arg2) {
237         if (logger.isInfoEnabled()) {
238             final ParameterizedMessage msg = new ParameterizedMessage(format, arg1, arg2);
239             logger.log(null, FQCN, Level.INFO, msg, msg.getThrowable());
240         }
241     }
242 
243     @Override
244     public void info(final String format, final Object... args) {
245         if (logger.isInfoEnabled()) {
246             final ParameterizedMessage msg = new ParameterizedMessage(format, args);
247             logger.log(null, FQCN, Level.INFO, msg, msg.getThrowable());
248         }
249     }
250 
251     @Override
252     public void info(final String format, final Throwable t) {
253         if (logger.isInfoEnabled()) {
254             logger.log(null, FQCN, Level.INFO, new SimpleMessage(format), t);
255         }
256     }
257 
258     @Override
259     public boolean isInfoEnabled() {
260         return logger.isInfoEnabled();
261     }
262 
263     @Override
264     public boolean isInfoEnabled(final Marker marker) {
265         return logger.isInfoEnabled((org.apache.logging.log4j.Marker) marker);
266     }
267 
268     @Override
269     public void info(final Marker marker, final String s) {
270         if (isInfoEnabled(marker)) {
271             logger.log((org.apache.logging.log4j.Marker) marker, FQCN, Level.INFO, new SimpleMessage(s), null);
272         }
273     }
274 
275     @Override
276     public void info(final Marker marker, final String s, final Object o) {
277         if (isInfoEnabled(marker)) {
278             logger.log((org.apache.logging.log4j.Marker) marker, FQCN, Level.INFO,
279                 new ParameterizedMessage(s, o), null);
280         }
281     }
282 
283     @Override
284     public void info(final Marker marker, final String s, final Object o, final Object o1) {
285         if (isInfoEnabled(marker)) {
286             final ParameterizedMessage msg = new ParameterizedMessage(s, o, o1);
287             logger.log((org.apache.logging.log4j.Marker) marker, FQCN, Level.INFO, msg, msg.getThrowable());
288         }
289     }
290 
291     @Override
292     public void info(final Marker marker, final String s, final Object... objects) {
293         if (isInfoEnabled(marker)) {
294             final ParameterizedMessage msg = new ParameterizedMessage(s, objects);
295             logger.log((org.apache.logging.log4j.Marker) marker, FQCN, Level.INFO, msg, msg.getThrowable());
296         }
297     }
298 
299     @Override
300     public void info(final Marker marker, final String s, final Throwable throwable) {
301         if (isInfoEnabled(marker)) {
302             logger.log((org.apache.logging.log4j.Marker) marker, FQCN, Level.INFO,
303                 new SimpleMessage(s), throwable);
304         }
305     }
306 
307     @Override
308     public void warn(final String format) {
309         if (logger.isWarnEnabled()) {
310             logger.log(null, FQCN, Level.WARN, new SimpleMessage(format), null);
311         }
312     }
313 
314     @Override
315     public void warn(final String format, final Object o) {
316         if (logger.isWarnEnabled()) {
317             logger.log(null, FQCN, Level.WARN, new ParameterizedMessage(format, o), null);
318         }
319     }
320 
321     @Override
322     public void warn(final String format, final Object arg1, final Object arg2) {
323         if (logger.isWarnEnabled()) {
324             final ParameterizedMessage msg = new ParameterizedMessage(format, arg1, arg2);
325             logger.log(null, FQCN, Level.WARN, msg, msg.getThrowable());
326         }
327     }
328 
329     @Override
330     public void warn(final String format, final Object... args) {
331         if (logger.isWarnEnabled()) {
332             final ParameterizedMessage msg = new ParameterizedMessage(format, args);
333             logger.log(null, FQCN, Level.WARN, msg, msg.getThrowable());
334         }
335     }
336 
337     @Override
338     public void warn(final String format, final Throwable t) {
339         if (logger.isWarnEnabled()) {
340             logger.log(null, FQCN, Level.WARN, new SimpleMessage(format), t);
341         }
342     }
343 
344     @Override
345     public boolean isWarnEnabled() {
346         return logger.isWarnEnabled();
347     }
348 
349     @Override
350     public boolean isWarnEnabled(final Marker marker) {
351         return logger.isWarnEnabled((org.apache.logging.log4j.Marker) marker);
352     }
353 
354     @Override
355     public void warn(final Marker marker, final String s) {
356         if (isWarnEnabled(marker)) {
357             logger.log((org.apache.logging.log4j.Marker) marker, FQCN, Level.WARN, new SimpleMessage(s), null);
358         }
359     }
360 
361     @Override
362     public void warn(final Marker marker, final String s, final Object o) {
363         if (isWarnEnabled(marker)) {
364             logger.log((org.apache.logging.log4j.Marker) marker, FQCN, Level.WARN,
365                 new ParameterizedMessage(s, o), null);
366         }
367     }
368 
369     @Override
370     public void warn(final Marker marker, final String s, final Object o, final Object o1) {
371         if (isWarnEnabled(marker)) {
372             final ParameterizedMessage msg = new ParameterizedMessage(s, o, o1);
373             logger.log((org.apache.logging.log4j.Marker) marker, FQCN, Level.WARN, msg, msg.getThrowable());
374         }
375     }
376 
377     @Override
378     public void warn(final Marker marker, final String s, final Object... objects) {
379         if (isWarnEnabled(marker)) {
380             final ParameterizedMessage msg = new ParameterizedMessage(s, objects);
381             logger.log((org.apache.logging.log4j.Marker) marker, FQCN, Level.WARN, msg, msg.getThrowable());
382         }
383     }
384 
385     @Override
386     public void warn(final Marker marker, final String s, final Throwable throwable) {
387         if (isWarnEnabled(marker)) {
388             logger.log((org.apache.logging.log4j.Marker) marker, FQCN, Level.WARN,
389                 new SimpleMessage(s), throwable);
390         }
391     }
392 
393     @Override
394     public void error(final String format) {
395         if (logger.isErrorEnabled()) {
396             logger.log(null, FQCN, Level.ERROR, new SimpleMessage(format), null);
397         }
398     }
399 
400     @Override
401     public void error(final String format, final Object o) {
402         if (logger.isErrorEnabled()) {
403             logger.log(null, FQCN, Level.ERROR, new ParameterizedMessage(format, o), null);
404         }
405     }
406 
407     @Override
408     public void error(final String format, final Object arg1, final Object arg2) {
409         if (logger.isErrorEnabled()) {
410             final ParameterizedMessage msg = new ParameterizedMessage(format, arg1, arg2);
411             logger.log(null, FQCN, Level.ERROR, msg, msg.getThrowable());
412         }
413     }
414 
415     @Override
416     public void error(final String format, final Object... args) {
417         if (logger.isErrorEnabled()) {
418             final ParameterizedMessage msg = new ParameterizedMessage(format, args);
419             logger.log(null, FQCN, Level.ERROR, msg, msg.getThrowable());
420         }
421     }
422 
423     @Override
424     public void error(final String format, final Throwable t) {
425         if (logger.isErrorEnabled()) {
426             logger.log(null, FQCN, Level.ERROR, new SimpleMessage(format), t);
427         }
428     }
429 
430     @Override
431     public boolean isErrorEnabled() {
432         return logger.isErrorEnabled();
433     }
434 
435     @Override
436     public boolean isErrorEnabled(final Marker marker) {
437         return logger.isErrorEnabled((org.apache.logging.log4j.Marker) marker);
438     }
439 
440     @Override
441     public void error(final Marker marker, final String s) {
442         if (isErrorEnabled(marker)) {
443             logger.log((org.apache.logging.log4j.Marker) marker, FQCN, Level.ERROR, new SimpleMessage(s), null);
444         }
445     }
446 
447     @Override
448     public void error(final Marker marker, final String s, final Object o) {
449         if (isErrorEnabled(marker)) {
450             logger.log((org.apache.logging.log4j.Marker) marker, FQCN, Level.ERROR,
451                 new ParameterizedMessage(s, o), null);
452         }
453     }
454 
455     @Override
456     public void error(final Marker marker, final String s, final Object o, final Object o1) {
457         if (isErrorEnabled(marker)) {
458             final ParameterizedMessage msg = new ParameterizedMessage(s, o, o1);
459             logger.log((org.apache.logging.log4j.Marker) marker, FQCN, Level.ERROR, msg, msg.getThrowable());
460         }
461     }
462 
463     @Override
464     public void error(final Marker marker, final String s, final Object... objects) {
465         if (isErrorEnabled(marker)) {
466             final ParameterizedMessage msg = new ParameterizedMessage(s, objects);
467             logger.log((org.apache.logging.log4j.Marker) marker, FQCN, Level.ERROR, msg, msg.getThrowable());
468         }
469     }
470 
471     @Override
472     public void error(final Marker marker, final String s, final Throwable throwable) {
473         if (isErrorEnabled(marker)) {
474             logger.log((org.apache.logging.log4j.Marker) marker, FQCN, Level.ERROR,
475                 new SimpleMessage(s), throwable);
476         }
477     }
478 
479 
480     @Override
481     public void log(final Marker marker, final String fqcn, final int i, final String s1, final Object[] objects,
482                     Throwable throwable) {
483         if (!logger.isEnabled(getLevel(i), (org.apache.logging.log4j.Marker) marker, s1)) {
484             return;
485         }
486         Message msg;
487         if (eventLogger && marker != null && marker.contains(EVENT_MARKER) && converter != null) {
488             msg = converter.convertEvent(s1, objects, throwable);
489        } else if (objects == null) {
490             msg = new SimpleMessage(s1);
491         } else {
492             msg = new ParameterizedMessage(s1, objects, throwable);
493             if (throwable != null) {
494                 throwable = msg.getThrowable();
495             }
496         }
497         logger.log((org.apache.logging.log4j.Marker) marker, fqcn, getLevel(i), msg, throwable);
498     }
499 
500     @Override
501     public String getName() {
502         return name;
503     }
504 
505     private EventDataConverter createConverter() {
506         try {
507             Class.forName("org.slf4j.ext.EventData");
508             return new EventDataConverter();
509         } catch (ClassNotFoundException cnfe) {
510             return null;
511         }
512     }
513 
514     private Level getLevel(final int i) {
515         switch (i) {
516             case TRACE_INT :
517                 return Level.TRACE;
518             case DEBUG_INT :
519                 return Level.DEBUG;
520             case INFO_INT :
521                 return Level.INFO;
522             case WARN_INT :
523                 return Level.WARN;
524             case ERROR_INT :
525                 return Level.ERROR;
526         }
527         return Level.ERROR;
528     }
529 }