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 final org.slf4j.Logger logger;
34      private final LocationAwareLogger locationAwareLogger;
35  
36      public SLF4JLogger(final String name, final org.slf4j.Logger logger) {
37          super(name);
38          this.logger = logger;
39          this.locationAwareLogger = logger instanceof LocationAwareLogger ? (LocationAwareLogger) logger : null;
40      }
41  
42      public SLF4JLogger(final String name, final MessageFactory messageFactory, final org.slf4j.Logger logger) {
43          super(name, messageFactory);
44          this.logger = logger;
45          this.locationAwareLogger = logger instanceof LocationAwareLogger ? (LocationAwareLogger) logger : null;
46      }
47  
48      @Override
49      protected void log(final Marker marker, final String fqcn, final Level level, final Message data,
50                         final Throwable t) {
51          if (locationAwareLogger != null) {
52              if (data instanceof LoggerNameAwareMessage) {
53                  ((LoggerNameAwareMessage) data).setLoggerName(getName());
54              }
55              locationAwareLogger.log(getMarker(marker), fqcn, convertLevel(level), data.getFormattedMessage(),
56                  data.getParameters(), t);
57          } else {
58              switch (level) {
59                  case DEBUG :
60                      logger.debug(getMarker(marker), data.getFormattedMessage(), data.getParameters(), t);
61                      break;
62                  case TRACE :
63                      logger.trace(getMarker(marker), data.getFormattedMessage(), data.getParameters(), t);
64                      break;
65                  case INFO :
66                      logger.info(getMarker(marker), data.getFormattedMessage(), data.getParameters(), t);
67                      break;
68                  case WARN :
69                      logger.warn(getMarker(marker), data.getFormattedMessage(), data.getParameters(), t);
70                      break;
71                  case ERROR :
72                      logger.error(getMarker(marker), data.getFormattedMessage(), data.getParameters(), t);
73                      break;
74                  default :
75                      logger.error(getMarker(marker), data.getFormattedMessage(), data.getParameters(), t);
76                      break;
77              }
78          }
79      }
80  
81      private org.slf4j.Marker getMarker(final Marker marker) {
82          if (marker == null) {
83              return null;
84          }
85          final Marker parent = marker.getParent();
86          final org.slf4j.Marker parentMarker = parent == null ? null : getMarker(parent);
87          final org.slf4j.Marker slf4jMarker = MarkerFactory.getMarker(marker.getName());
88          if (parentMarker != null && !slf4jMarker.contains(parentMarker)) {
89              slf4jMarker.add(parentMarker);
90          }
91          return slf4jMarker;
92      }
93  
94      private int convertLevel(final Level level) {
95          switch (level) {
96              case DEBUG :
97                  return LocationAwareLogger.DEBUG_INT;
98              case TRACE :
99                  return LocationAwareLogger.TRACE_INT;
100             case INFO :
101                 return LocationAwareLogger.INFO_INT;
102             case WARN :
103                 return LocationAwareLogger.WARN_INT;
104             case ERROR :
105                 return LocationAwareLogger.ERROR_INT;
106             default :
107                 return LocationAwareLogger.ERROR_INT;
108         }
109     }
110 
111     @Override
112     protected boolean isEnabled(final Level level, final Marker marker, final String data) {
113         return isEnabledFor(level, marker);
114     }
115 
116     @Override
117     protected boolean isEnabled(final Level level, final Marker marker, final String data, final Throwable t) {
118         return isEnabledFor(level, marker);
119     }
120 
121     @Override
122     protected boolean isEnabled(final Level level, final Marker marker, final String data, final Object... p1) {
123         return isEnabledFor(level, marker);
124     }
125 
126     @Override
127     protected boolean isEnabled(final Level level, final Marker marker, final Object data, final Throwable t) {
128         return isEnabledFor(level, marker);
129     }
130 
131     @Override
132     protected boolean isEnabled(final Level level, final Marker marker, final Message data, final Throwable t) {
133         return isEnabledFor(level, marker);
134     }
135 
136     private boolean isEnabledFor(final Level level, final Marker marker) {
137         final org.slf4j.Marker slf4jMarker = getMarker(marker);
138         switch (level) {
139             case DEBUG :
140                 return logger.isDebugEnabled(slf4jMarker);
141             case TRACE :
142                 return logger.isTraceEnabled(slf4jMarker);
143             case INFO :
144                 return logger.isInfoEnabled(slf4jMarker);
145             case WARN :
146                 return logger.isWarnEnabled(slf4jMarker);
147             case ERROR :
148                 return logger.isErrorEnabled(slf4jMarker);
149             default :
150                 return logger.isErrorEnabled(slf4jMarker);
151 
152         }
153     }
154 
155     public org.slf4j.Logger getLogger() {
156         return locationAwareLogger != null ? locationAwareLogger : logger;
157     }
158 
159 }