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 org.apache.logging.log4j.Level;
20  import org.apache.logging.log4j.Marker;
21  import org.apache.logging.log4j.message.LoggerNameAwareMessage;
22  import org.apache.logging.log4j.message.Message;
23  import org.apache.logging.log4j.message.MessageFactory;
24  import org.apache.logging.log4j.spi.AbstractLogger;
25  import org.slf4j.MarkerFactory;
26  import org.slf4j.spi.LocationAwareLogger;
27  
28  /**
29   *
30   */
31  public class SLF4JLogger extends AbstractLogger {
32  
33      private static final long serialVersionUID = 1L;
34      private final org.slf4j.Logger logger;
35      private final LocationAwareLogger locationAwareLogger;
36  
37      public SLF4JLogger(final String name, final MessageFactory messageFactory, final org.slf4j.Logger logger) {
38          super(name, messageFactory);
39          this.logger = logger;
40          this.locationAwareLogger = logger instanceof LocationAwareLogger ? (LocationAwareLogger) logger : null;
41      }
42  
43      public SLF4JLogger(final String name, final org.slf4j.Logger logger) {
44          super(name);
45          this.logger = logger;
46          this.locationAwareLogger = logger instanceof LocationAwareLogger ? (LocationAwareLogger) logger : null;
47      }
48  
49      private int convertLevel(final Level level) {
50          switch (level.getStandardLevel()) {
51              case DEBUG :
52                  return LocationAwareLogger.DEBUG_INT;
53              case TRACE :
54                  return LocationAwareLogger.TRACE_INT;
55              case INFO :
56                  return LocationAwareLogger.INFO_INT;
57              case WARN :
58                  return LocationAwareLogger.WARN_INT;
59              case ERROR :
60                  return LocationAwareLogger.ERROR_INT;
61              default :
62                  return LocationAwareLogger.ERROR_INT;
63          }
64      }
65  
66      @Override
67      public Level getLevel() {
68          if (logger.isTraceEnabled()) {
69              return Level.TRACE;
70          }
71          if (logger.isDebugEnabled()) {
72              return Level.DEBUG;
73          }
74          if (logger.isInfoEnabled()) {
75              return Level.INFO;
76          }
77          if (logger.isWarnEnabled()) {
78              return Level.WARN;
79          }
80          if (logger.isErrorEnabled()) {
81              return Level.ERROR;
82          }
83          // Option: throw new IllegalStateException("Unknown SLF4JLevel");
84          // Option: return Level.ALL;
85          return Level.OFF;
86      }
87  
88      public org.slf4j.Logger getLogger() {
89          return locationAwareLogger != null ? locationAwareLogger : logger;
90      }
91  
92      private org.slf4j.Marker getMarker(final Marker marker) {
93          if (marker == null) {
94              return null;
95          }
96          final org.slf4j.Marker slf4jMarker = MarkerFactory.getMarker(marker.getName());
97          final Marker[] parents = marker.getParents();
98          if (parents != null) {
99              for (final Marker parent : parents) {
100                 final org.slf4j.Marker slf4jParent = getMarker(parent);
101                 if (!slf4jMarker.contains(slf4jParent)) {
102                     slf4jMarker.add(slf4jParent);
103                 }
104             }
105         }
106         return slf4jMarker;
107     }
108 
109     @Override
110     public boolean isEnabled(final Level level, final Marker marker, final Message data, final Throwable t) {
111         return isEnabledFor(level, marker);
112     }
113 
114     @Override
115     public boolean isEnabled(final Level level, final Marker marker, final CharSequence data, final Throwable t) {
116         return isEnabledFor(level, marker);
117     }
118 
119     @Override
120     public boolean isEnabled(final Level level, final Marker marker, final Object data, final Throwable t) {
121         return isEnabledFor(level, marker);
122     }
123 
124     @Override
125     public boolean isEnabled(final Level level, final Marker marker, final String data) {
126         return isEnabledFor(level, marker);
127     }
128 
129     @Override
130     public boolean isEnabled(final Level level, final Marker marker, final String data, final Object... p1) {
131         return isEnabledFor(level, marker);
132     }
133 
134     @Override
135     public boolean isEnabled(final Level level, final Marker marker, final String message, final Object p0) {
136         return isEnabledFor(level, marker);
137     }
138 
139     @Override
140     public boolean isEnabled(final Level level, final Marker marker, final String message, final Object p0,
141             final Object p1) {
142         return isEnabledFor(level, marker);
143     }
144 
145     @Override
146     public boolean isEnabled(final Level level, final Marker marker, final String message, final Object p0,
147             final Object p1, final Object p2) {
148         return isEnabledFor(level, marker);
149     }
150 
151     @Override
152     public boolean isEnabled(final Level level, final Marker marker, final String message, final Object p0,
153             final Object p1, final Object p2, final Object p3) {
154         return isEnabledFor(level, marker);
155     }
156 
157     @Override
158     public boolean isEnabled(final Level level, final Marker marker, final String message, final Object p0,
159             final Object p1, final Object p2, final Object p3,
160             final Object p4) {
161         return isEnabledFor(level, marker);
162     }
163 
164     @Override
165     public boolean isEnabled(final Level level, final Marker marker, final String message, final Object p0,
166             final Object p1, final Object p2, final Object p3,
167             final Object p4, final Object p5) {
168         return isEnabledFor(level, marker);
169     }
170 
171     @Override
172     public boolean isEnabled(final Level level, final Marker marker, final String message, final Object p0,
173             final Object p1, final Object p2, final Object p3,
174             final Object p4, final Object p5, final Object p6) {
175         return isEnabledFor(level, marker);
176     }
177 
178     @Override
179     public boolean isEnabled(final Level level, final Marker marker, final String message, final Object p0,
180             final Object p1, final Object p2, final Object p3,
181             final Object p4, final Object p5, final Object p6,
182             final Object p7) {
183         return isEnabledFor(level, marker);
184     }
185 
186     @Override
187     public boolean isEnabled(final Level level, final Marker marker, final String message, final Object p0,
188             final Object p1, final Object p2, final Object p3,
189             final Object p4, final Object p5, final Object p6,
190             final Object p7, final Object p8) {
191         return isEnabledFor(level, marker);
192     }
193 
194     @Override
195     public boolean isEnabled(final Level level, final Marker marker, final String message, final Object p0,
196             final Object p1, final Object p2, final Object p3,
197             final Object p4, final Object p5, final Object p6,
198             final Object p7, final Object p8, final Object p9) {
199         return isEnabledFor(level, marker);
200     }
201 
202     @Override
203     public boolean isEnabled(final Level level, final Marker marker, final String data, final Throwable t) {
204         return isEnabledFor(level, marker);
205     }
206 
207     private boolean isEnabledFor(final Level level, final Marker marker) {
208         final org.slf4j.Marker slf4jMarker = getMarker(marker);
209         switch (level.getStandardLevel()) {
210             case DEBUG :
211                 return logger.isDebugEnabled(slf4jMarker);
212             case TRACE :
213                 return logger.isTraceEnabled(slf4jMarker);
214             case INFO :
215                 return logger.isInfoEnabled(slf4jMarker);
216             case WARN :
217                 return logger.isWarnEnabled(slf4jMarker);
218             case ERROR :
219                 return logger.isErrorEnabled(slf4jMarker);
220             default :
221                 return logger.isErrorEnabled(slf4jMarker);
222 
223         }
224     }
225 
226     @Override
227     public void logMessage(final String fqcn, final Level level, final Marker marker, final Message message, final Throwable t) {
228         if (locationAwareLogger != null) {
229             if (message instanceof LoggerNameAwareMessage) {
230                 ((LoggerNameAwareMessage) message).setLoggerName(getName());
231             }
232             locationAwareLogger.log(getMarker(marker), fqcn, convertLevel(level), message.getFormattedMessage(),
233                     message.getParameters(), t);
234         } else {
235             switch (level.getStandardLevel()) {
236                 case DEBUG :
237                     logger.debug(getMarker(marker), message.getFormattedMessage(), message.getParameters(), t);
238                     break;
239                 case TRACE :
240                     logger.trace(getMarker(marker), message.getFormattedMessage(), message.getParameters(), t);
241                     break;
242                 case INFO :
243                     logger.info(getMarker(marker), message.getFormattedMessage(), message.getParameters(), t);
244                     break;
245                 case WARN :
246                     logger.warn(getMarker(marker), message.getFormattedMessage(), message.getParameters(), t);
247                     break;
248                 case ERROR :
249                     logger.error(getMarker(marker), message.getFormattedMessage(), message.getParameters(), t);
250                     break;
251                 default :
252                     logger.error(getMarker(marker), message.getFormattedMessage(), message.getParameters(), t);
253                     break;
254             }
255         }
256     }
257 
258 }