    001/*
    002 * Copyright 2001-2004 The Apache Software Foundation.
    003 *
    004 * Licensed under the Apache License, Version 2.0 (the "License");
    005 * you may not use this file except in compliance with the License.
    006 * You may obtain a copy of the License at
    007 *
    008 *      http://www.apache.org/licenses/LICENSE-2.0
    009 *
    010 * Unless required by applicable law or agreed to in writing, software
    011 * distributed under the License is distributed on an "AS IS" BASIS,
    012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    013 * See the License for the specific language governing permissions and
    014 * limitations under the License.
    015 */
    016
    017package org.apache.commons.logging.impl;
    018
    019import java.io.ObjectStreamException;
    020import java.io.Serializable;
    021
    022import org.apache.commons.logging.Log;
    023import org.slf4j.Logger;
    024import org.slf4j.LoggerFactory;
    025import org.slf4j.spi.LocationAwareLogger;
    026
    027/**
    028 * Implementation of {@link Log org.apache.commons.logging.Log} interface which
    029 * delegates all processing to a wrapped {@link Logger org.slf4j.Logger}
    030 * instance.
    031 *
    032 * <p>
    033 * JCL's FATAL level is mapped to ERROR. All other levels map one to one.
    034 *
    035 * @author Ceki G&uuml;lc&uuml;
    036 */
    037public class SLF4JLocationAwareLog implements Log, Serializable {
    038
    039    private static final long serialVersionUID = -2379157579039314822L;
    040
    041    // used to store this logger's name to recreate it after serialization
    042    protected String name;
    043
    044    // in both Log4jLogger and Jdk14Logger classes in the original JCL, the
    045    // logger instance is transient
    046    private transient LocationAwareLogger logger;
    047
    048    private static final String FQCN = SLF4JLocationAwareLog.class.getName();
    049
    050    SLF4JLocationAwareLog(LocationAwareLogger logger) {
    051        this.logger = logger;
    052        this.name = logger.getName();
    053    }
    054
    055    /**
    056     * Delegates to the <code>isTraceEnabled<code> method of the wrapped
    057     * <code>org.slf4j.Logger</code> instance.
    058     */
    059    public boolean isTraceEnabled() {
    060        return logger.isTraceEnabled();
    061    }
    062
    063    /**
    064     * Directly delegates to the wrapped <code>org.slf4j.Logger</code> instance.
    065     */
    066    public boolean isDebugEnabled() {
    067        return logger.isDebugEnabled();
    068    }
    069
    070    /**
    071     * Directly delegates to the wrapped <code>org.slf4j.Logger</code> instance.
    072     */
    073    public boolean isInfoEnabled() {
    074        return logger.isInfoEnabled();
    075    }
    076
    077    /**
    078     * Directly delegates to the wrapped <code>org.slf4j.Logger</code> instance.
    079     */
    080    public boolean isWarnEnabled() {
    081        return logger.isWarnEnabled();
    082    }
    083
    084    /**
    085     * Directly delegates to the wrapped <code>org.slf4j.Logger</code> instance.
    086     */
    087    public boolean isErrorEnabled() {
    088        return logger.isErrorEnabled();
    089    }
    090
    091    /**
    092     * Delegates to the <code>isErrorEnabled<code> method of the wrapped
    093     * <code>org.slf4j.Logger</code> instance.
    094     */
    095    public boolean isFatalEnabled() {
    096        return logger.isErrorEnabled();
    097    }
    098
    099    /**
    100     * Converts the input parameter to String and then delegates to the debug
    101     * method of the wrapped <code>org.slf4j.Logger</code> instance.
    102     *
    103     * @param message
    104     *          the message to log. Converted to {@link String}
    105     */
    106    public void trace(Object message) {
    107        logger.log(null, FQCN, LocationAwareLogger.TRACE_INT, String.valueOf(message), null, null);
    108    }
    109
    110    /**
    111     * Converts the first input parameter to String and then delegates to the
    112     * debug method of the wrapped <code>org.slf4j.Logger</code> instance.
    113     *
    114     * @param message
    115     *          the message to log. Converted to {@link String}
    116     * @param t
    117     *          the exception to log
    118     */
    119    public void trace(Object message, Throwable t) {
    120        logger.log(null, FQCN, LocationAwareLogger.TRACE_INT, String.valueOf(message), null, t);
    121    }
    122
    123    /**
    124     * Converts the input parameter to String and then delegates to the wrapped
    125     * <code>org.slf4j.Logger</code> instance.
    126     *
    127     * @param message
    128     *          the message to log. Converted to {@link String}
    129     */
    130    public void debug(Object message) {
    131        logger.log(null, FQCN, LocationAwareLogger.DEBUG_INT, String.valueOf(message), null, null);
    132    }
    133
    134    /**
    135     * Converts the first input parameter to String and then delegates to the
    136     * wrapped <code>org.slf4j.Logger</code> instance.
    137     *
    138     * @param message
    139     *          the message to log. Converted to {@link String}
    140     * @param t
    141     *          the exception to log
    142     */
    143    public void debug(Object message, Throwable t) {
    144        logger.log(null, FQCN, LocationAwareLogger.DEBUG_INT, String.valueOf(message), null, t);
    145    }
    146
    147    /**
    148     * Converts the input parameter to String and then delegates to the wrapped
    149     * <code>org.slf4j.Logger</code> instance.
    150     *
    151     * @param message
    152     *          the message to log. Converted to {@link String}
    153     */
    154    public void info(Object message) {
    155        logger.log(null, FQCN, LocationAwareLogger.INFO_INT, String.valueOf(message), null, null);
    156    }
    157
    158    /**
    159     * Converts the first input parameter to String and then delegates to the
    160     * wrapped <code>org.slf4j.Logger</code> instance.
    161     *
    162     * @param message
    163     *          the message to log. Converted to {@link String}
    164     * @param t
    165     *          the exception to log
    166     */
    167    public void info(Object message, Throwable t) {
    168        logger.log(null, FQCN, LocationAwareLogger.INFO_INT, String.valueOf(message), null, t);
    169    }
    170
    171    /**
    172     * Converts the input parameter to String and then delegates to the wrapped
    173     * <code>org.slf4j.Logger</code> instance.
    174     *
    175     * @param message
    176     *          the message to log. Converted to {@link String}
    177     */
    178    public void warn(Object message) {
    179        logger.log(null, FQCN, LocationAwareLogger.WARN_INT, String.valueOf(message), null, null);
    180    }
    181
    182    /**
    183     * Converts the first input parameter to String and then delegates to the
    184     * wrapped <code>org.slf4j.Logger</code> instance.
    185     *
    186     * @param message
    187     *          the message to log. Converted to {@link String}
    188     * @param t
    189     *          the exception to log
    190     */
    191    public void warn(Object message, Throwable t) {
    192        logger.log(null, FQCN, LocationAwareLogger.WARN_INT, String.valueOf(message), null, t);
    193    }
    194
    195    /**
    196     * Converts the input parameter to String and then delegates to the wrapped
    197     * <code>org.slf4j.Logger</code> instance.
    198     *
    199     * @param message
    200     *          the message to log. Converted to {@link String}
    201     */
    202    public void error(Object message) {
    203        logger.log(null, FQCN, LocationAwareLogger.ERROR_INT, String.valueOf(message), null, null);
    204    }
    205
    206    /**
    207     * Converts the first input parameter to String and then delegates to the
    208     * wrapped <code>org.slf4j.Logger</code> instance.
    209     *
    210     * @param message
    211     *          the message to log. Converted to {@link String}
    212     * @param t
    213     *          the exception to log
    214     */
    215    public void error(Object message, Throwable t) {
    216        logger.log(null, FQCN, LocationAwareLogger.ERROR_INT, String.valueOf(message), null, t);
    217    }
    218
    219    /**
    220     * Converts the input parameter to String and then delegates to the error
    221     * method of the wrapped <code>org.slf4j.Logger</code> instance.
    222     *
    223     * @param message
    224     *          the message to log. Converted to {@link String}
    225     */
    226    public void fatal(Object message) {
    227        logger.log(null, FQCN, LocationAwareLogger.ERROR_INT, String.valueOf(message), null, null);
    228    }
    229
    230    /**
    231     * Converts the first input parameter to String and then delegates to the
    232     * error method of the wrapped <code>org.slf4j.Logger</code> instance.
    233     *
    234     * @param message
    235     *          the message to log. Converted to {@link String}
    236     * @param t
    237     *          the exception to log
    238     */
    239    public void fatal(Object message, Throwable t) {
    240        logger.log(null, FQCN, LocationAwareLogger.ERROR_INT, String.valueOf(message), null, t);
    241    }
    242
    243    /**
    244     * Replace this instance with a homonymous (same name) logger returned by
    245     * LoggerFactory. Note that this method is only called during deserialization.
    246     *
    247     * @return logger with same name as returned by LoggerFactory
    248     * @throws ObjectStreamException
    249     */
    250    protected Object readResolve() throws ObjectStreamException {
    251        Logger logger = LoggerFactory.getLogger(this.name);
    252        return new SLF4JLocationAwareLog((LocationAwareLogger) logger);
    253    }
    254}