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.apache.logging.slf4j;
18  
19  import java.io.IOException;
20  import java.io.ObjectInputStream;
21  import java.io.ObjectOutputStream;
22  import java.io.Serializable;
23  
24  import org.apache.logging.log4j.Level;
25  import org.apache.logging.log4j.LogManager;
26  import org.apache.logging.log4j.message.Message;
27  import org.apache.logging.log4j.message.ParameterizedMessage;
28  import org.apache.logging.log4j.message.SimpleMessage;
29  import org.apache.logging.log4j.spi.ExtendedLogger;
30  import org.slf4j.Marker;
31  import org.slf4j.spi.LocationAwareLogger;
32  
33  /**
34   * SLF4J logger implementation that uses Log4j.
35   */
36  public class Log4jLogger implements LocationAwareLogger, Serializable {
37  
38      public static final String FQCN = Log4jLogger.class.getName();
39  
40      private static final long serialVersionUID = 7869000638091304316L;
41      private transient ExtendedLogger logger;
42      private final String name;
43      private transient Log4jMarkerFactory markerFactory;
44  
45      public Log4jLogger(final Log4jMarkerFactory markerFactory, final ExtendedLogger logger, final String name) {
46          this.markerFactory = markerFactory;
47          this.logger = logger;
48          this.name = name;
49      }
50  
51      @Override
52      public void trace(final String format) {
53          logger.logIfEnabled(FQCN, Level.TRACE, null, format);
54      }
55  
56      @Override
57      public void trace(final String format, final Object o) {
58          logger.logIfEnabled(FQCN, Level.TRACE, null, format, o);
59      }
60  
61      @Override
62      public void trace(final String format, final Object arg1, final Object arg2) {
63          logger.logIfEnabled(FQCN, Level.TRACE, null, format, arg1, arg2);
64      }
65  
66      @Override
67      public void trace(final String format, final Object... args) {
68          logger.logIfEnabled(FQCN, Level.TRACE, null, format, args);
69      }
70  
71      @Override
72      public void trace(final String format, final Throwable t) {
73          logger.logIfEnabled(FQCN, Level.TRACE, null, format, t);
74      }
75  
76      @Override
77      public boolean isTraceEnabled() {
78          return logger.isEnabled(Level.TRACE, null, null);
79      }
80  
81      @Override
82      public boolean isTraceEnabled(final Marker marker) {
83          return logger.isEnabled(Level.TRACE, getMarker(marker), null);
84      }
85  
86      @Override
87      public void trace(final Marker marker, final String s) {
88          logger.logIfEnabled(FQCN, Level.TRACE, getMarker(marker), s);
89      }
90  
91      @Override
92      public void trace(final Marker marker, final String s, final Object o) {
93          logger.logIfEnabled(FQCN, Level.TRACE, getMarker(marker), s, o);
94      }
95  
96      @Override
97      public void trace(final Marker marker, final String s, final Object o, final Object o1) {
98          logger.logIfEnabled(FQCN, Level.TRACE, getMarker(marker), s, o, o1);
99      }
100 
101     @Override
102     public void trace(final Marker marker, final String s, final Object... objects) {
103         logger.logIfEnabled(FQCN, Level.TRACE, getMarker(marker), s, objects);
104     }
105 
106     @Override
107     public void trace(final Marker marker, final String s, final Throwable throwable) {
108         logger.logIfEnabled(FQCN, Level.TRACE, getMarker(marker), s, throwable);
109     }
110 
111     @Override
112     public void debug(final String format) {
113         logger.logIfEnabled(FQCN, Level.DEBUG, null, format);
114     }
115 
116     @Override
117     public void debug(final String format, final Object o) {
118         logger.logIfEnabled(FQCN, Level.DEBUG, null, format, o);
119     }
120 
121     @Override
122     public void debug(final String format, final Object arg1, final Object arg2) {
123         logger.logIfEnabled(FQCN, Level.DEBUG, null, format, arg1, arg2);
124     }
125 
126     @Override
127     public void debug(final String format, final Object... args) {
128         logger.logIfEnabled(FQCN, Level.DEBUG, null, format, args);
129     }
130 
131     @Override
132     public void debug(final String format, final Throwable t) {
133         logger.logIfEnabled(FQCN, Level.DEBUG, null, format, t);
134     }
135 
136     @Override
137     public boolean isDebugEnabled() {
138         return logger.isEnabled(Level.DEBUG, null, null);
139     }
140 
141     @Override
142     public boolean isDebugEnabled(final Marker marker) {
143         return logger.isEnabled(Level.DEBUG, getMarker(marker), null);
144     }
145 
146     @Override
147     public void debug(final Marker marker, final String s) {
148         logger.logIfEnabled(FQCN, Level.DEBUG, getMarker(marker), s);
149     }
150 
151     @Override
152     public void debug(final Marker marker, final String s, final Object o) {
153         logger.logIfEnabled(FQCN, Level.DEBUG, getMarker(marker), s, o);
154     }
155 
156     @Override
157     public void debug(final Marker marker, final String s, final Object o, final Object o1) {
158         logger.logIfEnabled(FQCN, Level.DEBUG, getMarker(marker), s, o, o1);
159     }
160 
161     @Override
162     public void debug(final Marker marker, final String s, final Object... objects) {
163         logger.logIfEnabled(FQCN, Level.DEBUG, getMarker(marker), s, objects);
164     }
165 
166     @Override
167     public void debug(final Marker marker, final String s, final Throwable throwable) {
168         logger.logIfEnabled(FQCN, Level.DEBUG, getMarker(marker), s, throwable);
169     }
170 
171     @Override
172     public void info(final String format) {
173         logger.logIfEnabled(FQCN, Level.INFO, null, format);
174     }
175 
176     @Override
177     public void info(final String format, final Object o) {
178         logger.logIfEnabled(FQCN, Level.INFO, null, format, o);
179     }
180 
181     @Override
182     public void info(final String format, final Object arg1, final Object arg2) {
183         logger.logIfEnabled(FQCN, Level.INFO, null, format, arg1, arg2);
184     }
185 
186     @Override
187     public void info(final String format, final Object... args) {
188         logger.logIfEnabled(FQCN, Level.INFO, null, format, args);
189     }
190 
191     @Override
192     public void info(final String format, final Throwable t) {
193         logger.logIfEnabled(FQCN, Level.INFO, null, format, t);
194     }
195 
196     @Override
197     public boolean isInfoEnabled() {
198         return logger.isEnabled(Level.INFO, null, null);
199     }
200 
201     @Override
202     public boolean isInfoEnabled(final Marker marker) {
203         return logger.isEnabled(Level.INFO, getMarker(marker), null);
204     }
205 
206     @Override
207     public void info(final Marker marker, final String s) {
208         logger.logIfEnabled(FQCN, Level.INFO, getMarker(marker), s);
209     }
210 
211     @Override
212     public void info(final Marker marker, final String s, final Object o) {
213         logger.logIfEnabled(FQCN, Level.INFO, getMarker(marker), s, o);
214     }
215 
216     @Override
217     public void info(final Marker marker, final String s, final Object o, final Object o1) {
218         logger.logIfEnabled(FQCN, Level.INFO, getMarker(marker), s, o, o1);
219     }
220 
221     @Override
222     public void info(final Marker marker, final String s, final Object... objects) {
223         logger.logIfEnabled(FQCN, Level.INFO, getMarker(marker), s, objects);
224     }
225 
226     @Override
227     public void info(final Marker marker, final String s, final Throwable throwable) {
228         logger.logIfEnabled(FQCN, Level.INFO, getMarker(marker), s, throwable);
229     }
230 
231     @Override
232     public void warn(final String format) {
233         logger.logIfEnabled(FQCN, Level.WARN, null, format);
234     }
235 
236     @Override
237     public void warn(final String format, final Object o) {
238         logger.logIfEnabled(FQCN, Level.WARN, null, format, o);
239     }
240 
241     @Override
242     public void warn(final String format, final Object arg1, final Object arg2) {
243         logger.logIfEnabled(FQCN, Level.WARN, null, format, arg1, arg2);
244     }
245 
246     @Override
247     public void warn(final String format, final Object... args) {
248         logger.logIfEnabled(FQCN, Level.WARN, null, format, args);
249     }
250 
251     @Override
252     public void warn(final String format, final Throwable t) {
253         logger.logIfEnabled(FQCN, Level.WARN, null, format, t);
254     }
255 
256     @Override
257     public boolean isWarnEnabled() {
258         return logger.isEnabled(Level.WARN, null, null);
259     }
260 
261     @Override
262     public boolean isWarnEnabled(final Marker marker) {
263         return logger.isEnabled(Level.WARN, getMarker(marker), null);
264     }
265 
266     @Override
267     public void warn(final Marker marker, final String s) {
268         logger.logIfEnabled(FQCN, Level.WARN, getMarker(marker), s);
269     }
270 
271     @Override
272     public void warn(final Marker marker, final String s, final Object o) {
273         logger.logIfEnabled(FQCN, Level.WARN, getMarker(marker), s, o);
274     }
275 
276     @Override
277     public void warn(final Marker marker, final String s, final Object o, final Object o1) {
278         logger.logIfEnabled(FQCN, Level.WARN, getMarker(marker), s, o, o1);
279     }
280 
281     @Override
282     public void warn(final Marker marker, final String s, final Object... objects) {
283         logger.logIfEnabled(FQCN, Level.WARN, getMarker(marker), s, objects);
284     }
285 
286     @Override
287     public void warn(final Marker marker, final String s, final Throwable throwable) {
288         logger.logIfEnabled(FQCN, Level.WARN, getMarker(marker), s, throwable);
289     }
290 
291     @Override
292     public void error(final String format) {
293         logger.logIfEnabled(FQCN, Level.ERROR, null, format);
294     }
295 
296     @Override
297     public void error(final String format, final Object o) {
298         logger.logIfEnabled(FQCN, Level.ERROR, null, format, o);
299     }
300 
301     @Override
302     public void error(final String format, final Object arg1, final Object arg2) {
303         logger.logIfEnabled(FQCN, Level.ERROR, null, format, arg1, arg2);
304     }
305 
306     @Override
307     public void error(final String format, final Object... args) {
308         logger.logIfEnabled(FQCN, Level.ERROR, null, format, args);
309     }
310 
311     @Override
312     public void error(final String format, final Throwable t) {
313         logger.logIfEnabled(FQCN, Level.ERROR, null, format, t);
314     }
315 
316     @Override
317     public boolean isErrorEnabled() {
318         return logger.isEnabled(Level.ERROR, null, null);
319     }
320 
321     @Override
322     public boolean isErrorEnabled(final Marker marker) {
323         return logger.isEnabled(Level.ERROR, getMarker(marker), null);
324     }
325 
326     @Override
327     public void error(final Marker marker, final String s) {
328         logger.logIfEnabled(FQCN, Level.ERROR, getMarker(marker), s);
329     }
330 
331     @Override
332     public void error(final Marker marker, final String s, final Object o) {
333         logger.logIfEnabled(FQCN, Level.ERROR, getMarker(marker), s, o);
334     }
335 
336     @Override
337     public void error(final Marker marker, final String s, final Object o, final Object o1) {
338         logger.logIfEnabled(FQCN, Level.ERROR, getMarker(marker), s, o, o1);
339     }
340 
341     @Override
342     public void error(final Marker marker, final String s, final Object... objects) {
343         logger.logIfEnabled(FQCN, Level.ERROR, getMarker(marker), s, objects);
344     }
345 
346     @Override
347     public void error(final Marker marker, final String s, final Throwable throwable) {
348         logger.logIfEnabled(FQCN, Level.ERROR, getMarker(marker), s, throwable);
349     }
350 
351     @Override
352     public void log(final Marker marker, final String fqcn, final int level, final String message, final Object[] params, Throwable throwable) {
353         final Level log4jLevel = getLevel(level);
354         final org.apache.logging.log4j.Marker log4jMarker = getMarker(marker);
355 
356         if (!logger.isEnabled(log4jLevel, log4jMarker, message, params)) {
357             return;
358         }
359         final Message msg;
360         if (params == null) {
361             msg = new SimpleMessage(message);
362         } else {
363             msg = new ParameterizedMessage(message, params, throwable);
364             if (throwable != null) {
365                 throwable = msg.getThrowable();
366             }
367         }
368         logger.logMessage(fqcn, log4jLevel, log4jMarker, msg, throwable);
369     }
370 
371     private org.apache.logging.log4j.Marker getMarker(final Marker marker) {
372         if (marker == null) {
373             return null;
374         } else if (marker instanceof Log4jMarker) {
375             return ((Log4jMarker) marker).getLog4jMarker();
376         } else {
377             return ((Log4jMarker) markerFactory.getMarker(marker)).getLog4jMarker();
378         }
379     }
380 
381     @Override
382     public String getName() {
383         return name;
384     }
385 
386     /**
387      * Always treat de-serialization as a full-blown constructor, by validating the final state of
388      * the de-serialized object.
389      */
390     private void readObject(final ObjectInputStream aInputStream) throws ClassNotFoundException, IOException {
391         // always perform the default de-serialization first
392         aInputStream.defaultReadObject();
393         logger = LogManager.getContext().getLogger(name);
394         markerFactory = ((Log4jLoggerFactory) org.slf4j.LoggerFactory.getILoggerFactory()).getMarkerFactory();
395     }
396 
397     /**
398      * This is the default implementation of writeObject. Customise if necessary.
399      */
400     private void writeObject(final ObjectOutputStream aOutputStream) throws IOException {
401         // perform the default serialization for all non-transient, non-static fields
402         aOutputStream.defaultWriteObject();
403     }
404 
405     private static Level getLevel(final int i) {
406         switch (i) {
407         case TRACE_INT:
408             return Level.TRACE;
409         case DEBUG_INT:
410             return Level.DEBUG;
411         case INFO_INT:
412             return Level.INFO;
413         case WARN_INT:
414             return Level.WARN;
415         case ERROR_INT:
416             return Level.ERROR;
417         }
418         return Level.ERROR;
419     }
420 }