001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017package org.apache.logging.slf4j;
018
019import org.apache.logging.log4j.Level;
020import org.apache.logging.log4j.Marker;
021import org.apache.logging.log4j.message.LoggerNameAwareMessage;
022import org.apache.logging.log4j.message.Message;
023import org.apache.logging.log4j.message.MessageFactory;
024import org.apache.logging.log4j.spi.AbstractLogger;
025import org.slf4j.MarkerFactory;
026import org.slf4j.spi.LocationAwareLogger;
027
028/**
029 *
030 */
031public class SLF4JLogger extends AbstractLogger {
032
033    private static final long serialVersionUID = 1L;
034    private final org.slf4j.Logger logger;
035    private final LocationAwareLogger locationAwareLogger;
036
037    public SLF4JLogger(final String name, final MessageFactory messageFactory, final org.slf4j.Logger logger) {
038        super(name, messageFactory);
039        this.logger = logger;
040        this.locationAwareLogger = logger instanceof LocationAwareLogger ? (LocationAwareLogger) logger : null;
041    }
042
043    public SLF4JLogger(final String name, final org.slf4j.Logger logger) {
044        super(name);
045        this.logger = logger;
046        this.locationAwareLogger = logger instanceof LocationAwareLogger ? (LocationAwareLogger) logger : null;
047    }
048
049    private int convertLevel(final Level level) {
050        switch (level.getStandardLevel()) {
051            case DEBUG :
052                return LocationAwareLogger.DEBUG_INT;
053            case TRACE :
054                return LocationAwareLogger.TRACE_INT;
055            case INFO :
056                return LocationAwareLogger.INFO_INT;
057            case WARN :
058                return LocationAwareLogger.WARN_INT;
059            case ERROR :
060                return LocationAwareLogger.ERROR_INT;
061            default :
062                return LocationAwareLogger.ERROR_INT;
063        }
064    }
065
066    @Override
067    public Level getLevel() {
068        if (logger.isTraceEnabled()) {
069            return Level.TRACE;
070        }
071        if (logger.isDebugEnabled()) {
072            return Level.DEBUG;
073        }
074        if (logger.isInfoEnabled()) {
075            return Level.INFO;
076        }
077        if (logger.isWarnEnabled()) {
078            return Level.WARN;
079        }
080        if (logger.isErrorEnabled()) {
081            return Level.ERROR;
082        }
083        // Option: throw new IllegalStateException("Unknown SLF4JLevel");
084        // Option: return Level.ALL;
085        return Level.OFF;
086    }
087
088    public org.slf4j.Logger getLogger() {
089        return locationAwareLogger != null ? locationAwareLogger : logger;
090    }
091
092    private org.slf4j.Marker getMarker(final Marker marker) {
093        if (marker == null) {
094            return null;
095        }
096        final org.slf4j.Marker slf4jMarker = MarkerFactory.getMarker(marker.getName());
097        final Marker[] parents = marker.getParents();
098        if (parents != null) {
099            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}