    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     */
    017    package org.apache.logging.slf4j;
    018
    019    import org.apache.logging.log4j.Level;
    020    import org.apache.logging.log4j.Marker;
    021    import org.apache.logging.log4j.message.LoggerNameAwareMessage;
    022    import org.apache.logging.log4j.message.Message;
    023    import org.apache.logging.log4j.message.MessageFactory;
    024    import org.apache.logging.log4j.spi.AbstractLogger;
    025    import org.slf4j.MarkerFactory;
    026    import org.slf4j.spi.LocationAwareLogger;
    027
    028    /**
    029     *
    030     */
    031    public 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 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    }