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 Object data, final Throwable t) {
116         return isEnabledFor(level, marker);
117     }
118 
119     @Override
120     public boolean isEnabled(final Level level, final Marker marker, final String data) {
121         return isEnabledFor(level, marker);
122     }
123 
124     @Override
125     public boolean isEnabled(final Level level, final Marker marker, final String data, final Object... p1) {
126         return isEnabledFor(level, marker);
127     }
128 
129     @Override
130     public boolean isEnabled(final Level level, final Marker marker, final String data, final Throwable t) {
131         return isEnabledFor(level, marker);
132     }
133 
134     private boolean isEnabledFor(final Level level, final Marker marker) {
135         final org.slf4j.Marker slf4jMarker = getMarker(marker);
136         switch (level.getStandardLevel()) {
137             case DEBUG :
138                 return logger.isDebugEnabled(slf4jMarker);
139             case TRACE :
140                 return logger.isTraceEnabled(slf4jMarker);
141             case INFO :
142                 return logger.isInfoEnabled(slf4jMarker);
143             case WARN :
144                 return logger.isWarnEnabled(slf4jMarker);
145             case ERROR :
146                 return logger.isErrorEnabled(slf4jMarker);
147             default :
148                 return logger.isErrorEnabled(slf4jMarker);
149 
150         }
151     }
152 
153     @Override
154     public void logMessage(final String fqcn, final Level level, final Marker marker, final Message message, final Throwable t) {
155         if (locationAwareLogger != null) {
156             if (message instanceof LoggerNameAwareMessage) {
157                 ((LoggerNameAwareMessage) message).setLoggerName(getName());
158             }
159             locationAwareLogger.log(getMarker(marker), fqcn, convertLevel(level), message.getFormattedMessage(),
160                     message.getParameters(), t);
161         } else {
162             switch (level.getStandardLevel()) {
163                 case DEBUG :
164                     logger.debug(getMarker(marker), message.getFormattedMessage(), message.getParameters(), t);
165                     break;
166                 case TRACE :
167                     logger.trace(getMarker(marker), message.getFormattedMessage(), message.getParameters(), t);
168                     break;
169                 case INFO :
170                     logger.info(getMarker(marker), message.getFormattedMessage(), message.getParameters(), t);
171                     break;
172                 case WARN :
173                     logger.warn(getMarker(marker), message.getFormattedMessage(), message.getParameters(), t);
174                     break;
175                 case ERROR :
176                     logger.error(getMarker(marker), message.getFormattedMessage(), message.getParameters(), t);
177                     break;
178                 default :
179                     logger.error(getMarker(marker), message.getFormattedMessage(), message.getParameters(), t);
180                     break;
181             }
182         }
183     }
184 
185 }