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