Log4JLogger.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.apache.commons.logging.impl;

  18. import java.io.Serializable;

  19. import org.apache.commons.logging.Log;
  20. import org.apache.log4j.Level;
  21. import org.apache.log4j.Logger;
  22. import org.apache.log4j.Priority;

  23. /**
  24.  * Implements {@link Log} to map directly to a
  25.  * <strong>Logger</strong> for Apache Log4J version 1.2.
  26.  * <p>
  27.  * Initial configuration of the corresponding Logger instances should be done
  28.  * in the usual manner, as outlined in the Log4J documentation.
  29.  * <p>
  30.  * The reason this logger is distinct from the 1.3.0 logger is that in version 1.2
  31.  * of Log4J:
  32.  * <ul>
  33.  * <li>class Logger takes Priority parameters not Level parameters.
  34.  * <li>class Level extends Priority
  35.  * </ul>
  36.  * Log4j 1.3 is expected to change Level so it no longer extends Priority, which is
  37.  * a non-binary-compatible change. The class generated by compiling this code against
  38.  * Log4j 1.2 will therefore not run against Log4j 1.3.
  39.  *
  40.  * @deprecated Scheduled for removal since version 1.x of Log4j has reached end-of-life.
  41.  */
  42. @Deprecated
  43. public class Log4JLogger implements Log, Serializable {

  44.     /** Serializable version identifier. */
  45.     private static final long serialVersionUID = 5160705895411730424L;

  46.     /** The fully qualified name of the Log4JLogger class. */
  47.     private static final String FQCN = Log4JLogger.class.getName();

  48.     private static final Priority traceLevel;

  49.     //
  50.     // Note that this must come after the static variable declarations
  51.     // otherwise initializer expressions associated with those variables
  52.     // will override any settings done here.
  53.     //
  54.     // Verify that Log4j is available, and that it is version 1.2.
  55.     // If an ExceptionInInitializerError is generated, then LogFactoryImpl
  56.     // will treat that as meaning that the appropriate underlying logging
  57.     // library is just not present - if discovery is in progress then
  58.     // discovery will continue.
  59.     static {
  60.         if (!Priority.class.isAssignableFrom(Level.class)) {
  61.             // nope, this is Log4j 1.3, so force an ExceptionInInitializerError
  62.             throw new InstantiationError("Log4J 1.2 not available");
  63.         }

  64.         // Releases of Log4j 1.2 >= 1.2.12 have Priority.TRACE available, earlier
  65.         // versions do not. If TRACE is not available, then we have to map
  66.         // calls to Log.trace(...) onto the DEBUG level.

  67.         Priority _traceLevel;
  68.         try {
  69.             _traceLevel = (Priority) Level.class.getDeclaredField("TRACE").get(null);
  70.         } catch (final Exception ex) {
  71.             // ok, trace not available
  72.             _traceLevel = Level.DEBUG;
  73.         }
  74.         traceLevel = _traceLevel;
  75.     }

  76.     /** Log to this logger */
  77.     private transient volatile Logger logger;

  78.     /** Logger name */
  79.     private final String name;

  80.     /**
  81.      * Constructs a new instance.
  82.      */
  83.     public Log4JLogger() {
  84.         name = null;
  85.     }

  86.     /**
  87.      * For use with a Log4j factory.
  88.      *
  89.      * @param logger Logger.
  90.      */
  91.     public Log4JLogger(final Logger logger) {
  92.         if (logger == null) {
  93.             throw new IllegalArgumentException(
  94.                 "Warning - null logger in constructor; possible Log4j misconfiguration.");
  95.         }
  96.         this.name = logger.getName();
  97.         this.logger = logger;
  98.     }

  99.     /**
  100.      * Base constructor.
  101.      *
  102.      * @param name name.
  103.      */
  104.     public Log4JLogger(final String name) {
  105.         this.name = name;
  106.         this.logger = getLogger();
  107.     }

  108.     /**
  109.      * Logs a message with {@code org.apache.log4j.Priority.DEBUG}.
  110.      *
  111.      * @param message to log
  112.      * @see org.apache.commons.logging.Log#debug(Object)
  113.      */
  114.     @Override
  115.     public void debug(final Object message) {
  116.         getLogger().log(FQCN, Level.DEBUG, message, null);
  117.     }

  118.     /**
  119.      * Logs a message with {@code org.apache.log4j.Priority.DEBUG}.
  120.      *
  121.      * @param message to log
  122.      * @param t log this cause
  123.      * @see org.apache.commons.logging.Log#debug(Object, Throwable)
  124.      */
  125.     @Override
  126.     public void debug(final Object message, final Throwable t) {
  127.         getLogger().log(FQCN, Level.DEBUG, message, t);
  128.     }

  129.     /**
  130.      * Logs a message with {@code org.apache.log4j.Priority.ERROR}.
  131.      *
  132.      * @param message to log
  133.      * @see org.apache.commons.logging.Log#error(Object)
  134.      */
  135.     @Override
  136.     public void error(final Object message) {
  137.         getLogger().log(FQCN, Level.ERROR, message, null);
  138.     }

  139.     /**
  140.      * Logs a message with {@code org.apache.log4j.Priority.ERROR}.
  141.      *
  142.      * @param message to log
  143.      * @param t log this cause
  144.      * @see org.apache.commons.logging.Log#error(Object, Throwable)
  145.      */
  146.     @Override
  147.     public void error(final Object message, final Throwable t) {
  148.         getLogger().log(FQCN, Level.ERROR, message, t);
  149.     }

  150.     /**
  151.      * Logs a message with {@code org.apache.log4j.Priority.FATAL}.
  152.      *
  153.      * @param message to log
  154.      * @see org.apache.commons.logging.Log#fatal(Object)
  155.      */
  156.     @Override
  157.     public void fatal(final Object message) {
  158.         getLogger().log(FQCN, Level.FATAL, message, null);
  159.     }

  160.     /**
  161.      * Logs a message with {@code org.apache.log4j.Priority.FATAL}.
  162.      *
  163.      * @param message to log
  164.      * @param t log this cause
  165.      * @see org.apache.commons.logging.Log#fatal(Object, Throwable)
  166.      */
  167.     @Override
  168.     public void fatal(final Object message, final Throwable t) {
  169.         getLogger().log(FQCN, Level.FATAL, message, t);
  170.     }

  171.     /**
  172.      * Gets the native Logger instance we are using.
  173.      *
  174.      * @return the native Logger instance we are using.
  175.      */
  176.     public Logger getLogger() {
  177.         Logger result = logger;
  178.         if (result == null) {
  179.             synchronized(this) {
  180.                 result = logger;
  181.                 if (result == null) {
  182.                     logger = result = Logger.getLogger(name);
  183.                 }
  184.             }
  185.         }
  186.         return result;
  187.     }

  188.     /**
  189.      * Logs a message with {@code org.apache.log4j.Priority.INFO}.
  190.      *
  191.      * @param message to log
  192.      * @see org.apache.commons.logging.Log#info(Object)
  193.      */
  194.     @Override
  195.     public void info(final Object message) {
  196.         getLogger().log(FQCN, Level.INFO, message, null);
  197.     }

  198.     /**
  199.      * Logs a message with {@code org.apache.log4j.Priority.INFO}.
  200.      *
  201.      * @param message to log
  202.      * @param t log this cause
  203.      * @see org.apache.commons.logging.Log#info(Object, Throwable)
  204.      */
  205.     @Override
  206.     public void info(final Object message, final Throwable t) {
  207.         getLogger().log(FQCN, Level.INFO, message, t);
  208.     }

  209.     /**
  210.      * Tests whether the Log4j Logger used is enabled for {@code DEBUG} priority.
  211.      */
  212.     @Override
  213.     public boolean isDebugEnabled() {
  214.         return getLogger().isDebugEnabled();
  215.     }

  216.     /**
  217.      * Tests whether the Log4j Logger used is enabled for {@code ERROR} priority.
  218.      */
  219.     @Override
  220.     public boolean isErrorEnabled() {
  221.         return getLogger().isEnabledFor(Level.ERROR);
  222.     }

  223.     /**
  224.      * Tests whether the Log4j Logger used is enabled for {@code FATAL} priority.
  225.      */
  226.     @Override
  227.     public boolean isFatalEnabled() {
  228.         return getLogger().isEnabledFor(Level.FATAL);
  229.     }

  230.     /**
  231.      * Tests whether the Log4j Logger used is enabled for {@code INFO} priority.
  232.      */
  233.     @Override
  234.     public boolean isInfoEnabled() {
  235.         return getLogger().isInfoEnabled();
  236.     }

  237.     /**
  238.      * Tests whether the Log4j Logger used is enabled for {@code TRACE} priority.
  239.      * When using a Log4j version that does not support the TRACE level, this call
  240.      * will report whether {@code DEBUG} is enabled or not.
  241.      */
  242.     @Override
  243.     public boolean isTraceEnabled() {
  244.         return getLogger().isEnabledFor(traceLevel);
  245.     }

  246.     /**
  247.      * Tests whether the Log4j Logger used is enabled for {@code WARN} priority.
  248.      */
  249.     @Override
  250.     public boolean isWarnEnabled() {
  251.         return getLogger().isEnabledFor(Level.WARN);
  252.     }

  253.     /**
  254.      * Logs a message with {@code org.apache.log4j.Priority.TRACE}.
  255.      * When using a Log4j version that does not support the {@code TRACE}
  256.      * level, the message will be logged at the {@code DEBUG} level.
  257.      *
  258.      * @param message to log
  259.      * @see org.apache.commons.logging.Log#trace(Object)
  260.      */
  261.     @Override
  262.     public void trace(final Object message) {
  263.         getLogger().log(FQCN, traceLevel, message, null);
  264.     }

  265.     /**
  266.      * Logs a message with {@code org.apache.log4j.Priority.TRACE}.
  267.      * When using a Log4j version that does not support the {@code TRACE}
  268.      * level, the message will be logged at the {@code DEBUG} level.
  269.      *
  270.      * @param message to log
  271.      * @param t log this cause
  272.      * @see org.apache.commons.logging.Log#trace(Object, Throwable)
  273.      */
  274.     @Override
  275.     public void trace(final Object message, final Throwable t) {
  276.         getLogger().log(FQCN, traceLevel, message, t);
  277.     }

  278.     /**
  279.      * Logs a message with {@code org.apache.log4j.Priority.WARN}.
  280.      *
  281.      * @param message to log
  282.      * @see org.apache.commons.logging.Log#warn(Object)
  283.      */
  284.     @Override
  285.     public void warn(final Object message) {
  286.         getLogger().log(FQCN, Level.WARN, message, null);
  287.     }

  288.     /**
  289.      * Logs a message with {@code org.apache.log4j.Priority.WARN}.
  290.      *
  291.      * @param message to log
  292.      * @param t log this cause
  293.      * @see org.apache.commons.logging.Log#warn(Object, Throwable)
  294.      */
  295.     @Override
  296.     public void warn(final Object message, final Throwable t) {
  297.         getLogger().log(FQCN, Level.WARN, message, t);
  298.     }

  299. }