    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 *      https://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 * Implements {@link Log} to map directly to a
    029 * <strong>Logger</strong> for Apache 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.</li>
    038 * <li>class Level extends Priority</li>
    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 TRACE_LEVEL;
    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        // Releases of Log4j 1.2 >= 1.2.12 have Priority.TRACE available, earlier
    073        // versions do not. If TRACE is not available, then we have to map
    074        // calls to Log.trace(...) onto the DEBUG level.
    075        Priority traceLevel;
    076        try {
    077            traceLevel = (Priority) Level.class.getDeclaredField("TRACE").get(null);
    078        } catch (final Exception ex) {
    079            // ok, trace not available
    080            traceLevel = Level.DEBUG;
    081        }
    082        TRACE_LEVEL = traceLevel;
    083    }
    084
    085    /** Log to this logger */
    086    private transient volatile Logger logger;
    087
    088    /** Logger name */
    089    private final String name;
    090
    091    /**
    092     * Constructs a new instance.
    093     */
    094    public Log4JLogger() {
    095        name = null;
    096    }
    097
    098    /**
    099     * For use with a Log4j factory.
    100     *
    101     * @param logger Logger.
    102     */
    103    public Log4JLogger(final Logger logger) {
    104        if (logger == null) {
    105            throw new IllegalArgumentException("Warning - null logger in constructor; possible Log4j misconfiguration.");
    106        }
    107        this.name = logger.getName();
    108        this.logger = logger;
    109    }
    110
    111    /**
    112     * Base constructor.
    113     *
    114     * @param name name.
    115     */
    116    public Log4JLogger(final String name) {
    117        this.name = name;
    118        this.logger = getLogger();
    119    }
    120
    121    /**
    122     * Logs a message with {@code org.apache.log4j.Priority.DEBUG}.
    123     *
    124     * @param message to log
    125     * @see org.apache.commons.logging.Log#debug(Object)
    126     */
    127    @Override
    128    public void debug(final Object message) {
    129        getLogger().log(FQCN, Level.DEBUG, message, null);
    130    }
    131
    132    /**
    133     * Logs a message with {@code org.apache.log4j.Priority.DEBUG}.
    134     *
    135     * @param message to log
    136     * @param t log this cause
    137     * @see org.apache.commons.logging.Log#debug(Object, Throwable)
    138     */
    139    @Override
    140    public void debug(final Object message, final Throwable t) {
    141        getLogger().log(FQCN, Level.DEBUG, message, t);
    142    }
    143
    144    /**
    145     * Logs a message with {@code org.apache.log4j.Priority.ERROR}.
    146     *
    147     * @param message to log
    148     * @see org.apache.commons.logging.Log#error(Object)
    149     */
    150    @Override
    151    public void error(final Object message) {
    152        getLogger().log(FQCN, Level.ERROR, message, null);
    153    }
    154
    155    /**
    156     * Logs a message with {@code org.apache.log4j.Priority.ERROR}.
    157     *
    158     * @param message to log
    159     * @param t log this cause
    160     * @see org.apache.commons.logging.Log#error(Object, Throwable)
    161     */
    162    @Override
    163    public void error(final Object message, final Throwable t) {
    164        getLogger().log(FQCN, Level.ERROR, message, t);
    165    }
    166
    167    /**
    168     * Logs a message with {@code org.apache.log4j.Priority.FATAL}.
    169     *
    170     * @param message to log
    171     * @see org.apache.commons.logging.Log#fatal(Object)
    172     */
    173    @Override
    174    public void fatal(final Object message) {
    175        getLogger().log(FQCN, Level.FATAL, message, null);
    176    }
    177
    178    /**
    179     * Logs a message with {@code org.apache.log4j.Priority.FATAL}.
    180     *
    181     * @param message to log
    182     * @param t log this cause
    183     * @see org.apache.commons.logging.Log#fatal(Object, Throwable)
    184     */
    185    @Override
    186    public void fatal(final Object message, final Throwable t) {
    187        getLogger().log(FQCN, Level.FATAL, message, t);
    188    }
    189
    190    /**
    191     * Gets the native Logger instance we are using.
    192     *
    193     * @return the native Logger instance we are using.
    194     */
    195    public Logger getLogger() {
    196        Logger result = logger;
    197        if (result == null) {
    198            synchronized(this) {
    199                result = logger;
    200                if (result == null) {
    201                    logger = result = Logger.getLogger(name);
    202                }
    203            }
    204        }
    205        return result;
    206    }
    207
    208    /**
    209     * Logs a message with {@code org.apache.log4j.Priority.INFO}.
    210     *
    211     * @param message to log
    212     * @see org.apache.commons.logging.Log#info(Object)
    213     */
    214    @Override
    215    public void info(final Object message) {
    216        getLogger().log(FQCN, Level.INFO, message, null);
    217    }
    218
    219    /**
    220     * Logs a message with {@code org.apache.log4j.Priority.INFO}.
    221     *
    222     * @param message to log
    223     * @param t log this cause
    224     * @see org.apache.commons.logging.Log#info(Object, Throwable)
    225     */
    226    @Override
    227    public void info(final Object message, final Throwable t) {
    228        getLogger().log(FQCN, Level.INFO, message, t);
    229    }
    230
    231    /**
    232     * Tests whether the Log4j Logger used is enabled for {@code DEBUG} priority.
    233     */
    234    @Override
    235    public boolean isDebugEnabled() {
    236        return getLogger().isDebugEnabled();
    237    }
    238
    239    /**
    240     * Tests whether the Log4j Logger used is enabled for {@code ERROR} priority.
    241     */
    242    @Override
    243    public boolean isErrorEnabled() {
    244        return getLogger().isEnabledFor(Level.ERROR);
    245    }
    246
    247    /**
    248     * Tests whether the Log4j Logger used is enabled for {@code FATAL} priority.
    249     */
    250    @Override
    251    public boolean isFatalEnabled() {
    252        return getLogger().isEnabledFor(Level.FATAL);
    253    }
    254
    255    /**
    256     * Tests whether the Log4j Logger used is enabled for {@code INFO} priority.
    257     */
    258    @Override
    259    public boolean isInfoEnabled() {
    260        return getLogger().isInfoEnabled();
    261    }
    262
    263    /**
    264     * Tests whether the Log4j Logger used is enabled for {@code TRACE} priority.
    265     * When using a Log4j version that does not support the TRACE level, this call
    266     * will report whether {@code DEBUG} is enabled or not.
    267     */
    268    @Override
    269    public boolean isTraceEnabled() {
    270        return getLogger().isEnabledFor(TRACE_LEVEL);
    271    }
    272
    273    /**
    274     * Tests whether the Log4j Logger used is enabled for {@code WARN} priority.
    275     */
    276    @Override
    277    public boolean isWarnEnabled() {
    278        return getLogger().isEnabledFor(Level.WARN);
    279    }
    280
    281    /**
    282     * Logs a message with {@code org.apache.log4j.Priority.TRACE}.
    283     * When using a Log4j version that does not support the {@code TRACE}
    284     * level, the message will be logged at the {@code DEBUG} level.
    285     *
    286     * @param message to log
    287     * @see org.apache.commons.logging.Log#trace(Object)
    288     */
    289    @Override
    290    public void trace(final Object message) {
    291        getLogger().log(FQCN, TRACE_LEVEL, message, null);
    292    }
    293
    294    /**
    295     * Logs a message with {@code org.apache.log4j.Priority.TRACE}.
    296     * When using a Log4j version that does not support the {@code TRACE}
    297     * level, the message will be logged at the {@code DEBUG} level.
    298     *
    299     * @param message to log
    300     * @param t log this cause
    301     * @see org.apache.commons.logging.Log#trace(Object, Throwable)
    302     */
    303    @Override
    304    public void trace(final Object message, final Throwable t) {
    305        getLogger().log(FQCN, TRACE_LEVEL, message, t);
    306    }
    307
    308    /**
    309     * Logs a message with {@code org.apache.log4j.Priority.WARN}.
    310     *
    311     * @param message to log
    312     * @see org.apache.commons.logging.Log#warn(Object)
    313     */
    314    @Override
    315    public void warn(final Object message) {
    316        getLogger().log(FQCN, Level.WARN, message, null);
    317    }
    318
    319    /**
    320     * Logs a message with {@code org.apache.log4j.Priority.WARN}.
    321     *
    322     * @param message to log
    323     * @param t log this cause
    324     * @see org.apache.commons.logging.Log#warn(Object, Throwable)
    325     */
    326    @Override
    327    public void warn(final Object message, final Throwable t) {
    328        getLogger().log(FQCN, Level.WARN, message, t);
    329    }
    330
    331}