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