    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
    018package org.apache.commons.logging.impl;
    019
    020import java.io.Serializable;
    021
    022import org.apache.commons.logging.Log;
    023import org.apache.log4j.Level;
    024import org.apache.log4j.Logger;
    025import org.apache.log4j.Priority;
    026
    027/**
    028 * Implementation of {@link Log} that maps directly to a
    029 * <strong>Logger</strong> for log4J version 1.2.
    030 * <p>
    031 * Initial configuration of the corresponding Logger instances should be done
    032 * in the usual manner, as outlined in the Log4J documentation.
    033 * <p>
    034 * The reason this logger is distinct from the 1.3.0 logger is that in version 1.2
    035 * of Log4J:
    036 * <ul>
    037 * <li>class Logger takes Priority parameters not Level parameters.
    038 * <li>class Level extends Priority
    039 * </ul>
    040 * Log4j 1.3 is expected to change Level so it no longer extends Priority, which is
    041 * a non-binary-compatible change. The class generated by compiling this code against
    042 * Log4j 1.2 will therefore not run against Log4j 1.3.
    043 *
    044 * @deprecated Scheduled for removal since version 1.x of Log4j has reached end-of-life.
    045 */
    046@Deprecated
    047public class Log4JLogger implements Log, Serializable {
    048
    049    /** Serializable version identifier. */
    050    private static final long serialVersionUID = 5160705895411730424L;
    051
    052    /** The fully qualified name of the Log4JLogger class. */
    053    private static final String FQCN = Log4JLogger.class.getName();
    054
    055    private static final Priority traceLevel;
    056
    057    //
    058    // Note that this must come after the static variable declarations
    059    // otherwise initializer expressions associated with those variables
    060    // will override any settings done here.
    061    //
    062    // Verify that Log4j is available, and that it is version 1.2.
    063    // If an ExceptionInInitializerError is generated, then LogFactoryImpl
    064    // will treat that as meaning that the appropriate underlying logging
    065    // library is just not present - if discovery is in progress then
    066    // discovery will continue.
    067    static {
    068        if (!Priority.class.isAssignableFrom(Level.class)) {
    069            // nope, this is Log4j 1.3, so force an ExceptionInInitializerError
    070            throw new InstantiationError("Log4J 1.2 not available");
    071        }
    072
    073        // Releases of Log4j 1.2 >= 1.2.12 have Priority.TRACE available, earlier
    074        // versions do not. If TRACE is not available, then we have to map
    075        // calls to Log.trace(...) onto the DEBUG level.
    076
    077        Priority _traceLevel;
    078        try {
    079            _traceLevel = (Priority) Level.class.getDeclaredField("TRACE").get(null);
    080        } catch (final Exception ex) {
    081            // ok, trace not available
    082            _traceLevel = Level.DEBUG;
    083        }
    084        traceLevel = _traceLevel;
    085    }
    086
    087    /** Log to this logger */
    088    private transient volatile Logger logger;
    089
    090    /** Logger name */
    091    private final String name;
    092
    093    /**
    094     * Constructs a new instance.
    095     */
    096    public Log4JLogger() {
    097        name = null;
    098    }
    099
    100    /**
    101     * For use with a Log4j factory.
    102     *
    103     * @param logger Logger.
    104     */
    105    public Log4JLogger(final Logger logger) {
    106        if (logger == null) {
    107            throw new IllegalArgumentException(
    108                "Warning - null logger in constructor; possible Log4j misconfiguration.");
    109        }
    110        this.name = logger.getName();
    111        this.logger = logger;
    112    }
    113
    114    /**
    115     * Base constructor.
    116     *
    117     * @param name name.
    118     */
    119    public Log4JLogger(final String name) {
    120        this.name = name;
    121        this.logger = getLogger();
    122    }
    123
    124    /**
    125     * Logs a message with {@code org.apache.log4j.Priority.DEBUG}.
    126     *
    127     * @param message to log
    128     * @see org.apache.commons.logging.Log#debug(Object)
    129     */
    130    @Override
    131    public void debug(final Object message) {
    132        getLogger().log(FQCN, Level.DEBUG, message, null);
    133    }
    134
    135    /**
    136     * Logs a message with {@code org.apache.log4j.Priority.DEBUG}.
    137     *
    138     * @param message to log
    139     * @param t log this cause
    140     * @see org.apache.commons.logging.Log#debug(Object, Throwable)
    141     */
    142    @Override
    143    public void debug(final Object message, final Throwable t) {
    144        getLogger().log(FQCN, Level.DEBUG, message, t);
    145    }
    146
    147    /**
    148     * Logs a message with {@code org.apache.log4j.Priority.ERROR}.
    149     *
    150     * @param message to log
    151     * @see org.apache.commons.logging.Log#error(Object)
    152     */
    153    @Override
    154    public void error(final Object message) {
    155        getLogger().log(FQCN, Level.ERROR, message, null);
    156    }
    157
    158    /**
    159     * Logs a message with {@code org.apache.log4j.Priority.ERROR}.
    160     *
    161     * @param message to log
    162     * @param t log this cause
    163     * @see org.apache.commons.logging.Log#error(Object, Throwable)
    164     */
    165    @Override
    166    public void error(final Object message, final Throwable t) {
    167        getLogger().log(FQCN, Level.ERROR, message, t);
    168    }
    169
    170    /**
    171     * Logs a message with {@code org.apache.log4j.Priority.FATAL}.
    172     *
    173     * @param message to log
    174     * @see org.apache.commons.logging.Log#fatal(Object)
    175     */
    176    @Override
    177    public void fatal(final Object message) {
    178        getLogger().log(FQCN, Level.FATAL, message, null);
    179    }
    180
    181    /**
    182     * Logs a message with {@code org.apache.log4j.Priority.FATAL}.
    183     *
    184     * @param message to log
    185     * @param t log this cause
    186     * @see org.apache.commons.logging.Log#fatal(Object, Throwable)
    187     */
    188    @Override
    189    public void fatal(final Object message, final Throwable t) {
    190        getLogger().log(FQCN, Level.FATAL, message, t);
    191    }
    192
    193    /**
    194     * Gets the native Logger instance we are using.
    195     *
    196     * @return the native Logger instance we are using.
    197     */
    198    public Logger getLogger() {
    199        Logger result = logger;
    200        if (result == null) {
    201            synchronized(this) {
    202                result = logger;
    203                if (result == null) {
    204                    logger = result = Logger.getLogger(name);
    205                }
    206            }
    207        }
    208        return result;
    209    }
    210
    211    /**
    212     * Logs a message with {@code org.apache.log4j.Priority.INFO}.
    213     *
    214     * @param message to log
    215     * @see org.apache.commons.logging.Log#info(Object)
    216     */
    217    @Override
    218    public void info(final Object message) {
    219        getLogger().log(FQCN, Level.INFO, message, null);
    220    }
    221
    222    /**
    223     * Logs a message with {@code org.apache.log4j.Priority.INFO}.
    224     *
    225     * @param message to log
    226     * @param t log this cause
    227     * @see org.apache.commons.logging.Log#info(Object, Throwable)
    228     */
    229    @Override
    230    public void info(final Object message, final Throwable t) {
    231        getLogger().log(FQCN, Level.INFO, message, t);
    232    }
    233
    234    /**
    235     * Tests whether the Log4j Logger used is enabled for {@code DEBUG} priority.
    236     */
    237    @Override
    238    public boolean isDebugEnabled() {
    239        return getLogger().isDebugEnabled();
    240    }
    241
    242    /**
    243     * Tests whether the Log4j Logger used is enabled for {@code ERROR} priority.
    244     */
    245    @Override
    246    public boolean isErrorEnabled() {
    247        return getLogger().isEnabledFor(Level.ERROR);
    248    }
    249
    250    /**
    251     * Tests whether the Log4j Logger used is enabled for {@code FATAL} priority.
    252     */
    253    @Override
    254    public boolean isFatalEnabled() {
    255        return getLogger().isEnabledFor(Level.FATAL);
    256    }
    257
    258    /**
    259     * Tests whether the Log4j Logger used is enabled for {@code INFO} priority.
    260     */
    261    @Override
    262    public boolean isInfoEnabled() {
    263        return getLogger().isInfoEnabled();
    264    }
    265
    266    /**
    267     * Tests whether the Log4j Logger used is enabled for {@code TRACE} priority.
    268     * When using a Log4j version that does not support the TRACE level, this call
    269     * will report whether {@code DEBUG} is enabled or not.
    270     */
    271    @Override
    272    public boolean isTraceEnabled() {
    273        return getLogger().isEnabledFor(traceLevel);
    274    }
    275
    276    /**
    277     * Tests whether the Log4j Logger used is enabled for {@code WARN} priority.
    278     */
    279    @Override
    280    public boolean isWarnEnabled() {
    281        return getLogger().isEnabledFor(Level.WARN);
    282    }
    283
    284    /**
    285     * Logs a message with {@code org.apache.log4j.Priority.TRACE}.
    286     * When using a Log4j version that does not support the {@code TRACE}
    287     * level, the message will be logged at the {@code DEBUG} level.
    288     *
    289     * @param message to log
    290     * @see org.apache.commons.logging.Log#trace(Object)
    291     */
    292    @Override
    293    public void trace(final Object message) {
    294        getLogger().log(FQCN, traceLevel, message, null);
    295    }
    296
    297    /**
    298     * Logs a message with {@code org.apache.log4j.Priority.TRACE}.
    299     * When using a Log4j version that does not support the {@code TRACE}
    300     * level, the message will be logged at the {@code DEBUG} level.
    301     *
    302     * @param message to log
    303     * @param t log this cause
    304     * @see org.apache.commons.logging.Log#trace(Object, Throwable)
    305     */
    306    @Override
    307    public void trace(final Object message, final Throwable t) {
    308        getLogger().log(FQCN, traceLevel, message, t);
    309    }
    310
    311    /**
    312     * Logs a message with {@code org.apache.log4j.Priority.WARN}.
    313     *
    314     * @param message to log
    315     * @see org.apache.commons.logging.Log#warn(Object)
    316     */
    317    @Override
    318    public void warn(final Object message) {
    319        getLogger().log(FQCN, Level.WARN, message, null);
    320    }
    321
    322    /**
    323     * Logs a message with {@code org.apache.log4j.Priority.WARN}.
    324     *
    325     * @param message to log
    326     * @param t log this cause
    327     * @see org.apache.commons.logging.Log#warn(Object, Throwable)
    328     */
    329    @Override
    330    public void warn(final Object message, final Throwable t) {
    331        getLogger().log(FQCN, Level.WARN, message, t);
    332    }
    333
    334}