AvalonLogger.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.util.Objects;

  19. import org.apache.avalon.framework.logger.Logger;
  20. import org.apache.commons.logging.Log;

  21. /**
  22.  * Implements Commons Logging's Log interface to delegate all
  23.  * logging calls to the Avalon logging abstraction: the Logger interface.
  24.  * <p>
  25.  * There are two ways in which this class can be used:
  26.  * <ul>
  27.  * <li>the instance can be constructed with an Avalon logger
  28.  * (by calling {@link #AvalonLogger(Logger)}). In this case, it acts
  29.  * as a simple thin wrapping implementation over the logger. This is
  30.  * particularly useful when using a property setter.
  31.  * </li>
  32.  * <li>the {@link #setDefaultLogger} class property can be called which
  33.  * sets the ancestral Avalon logger for this class. Any {@code AvalonLogger}
  34.  * instances created through the {@code LogFactory} mechanisms will output
  35.  * to child loggers of this {@code Logger}.
  36.  * </li>
  37.  * </ul>
  38.  * <p>
  39.  * <strong>Note:</strong> {@code AvalonLogger} does not implement Serializable
  40.  * because the constructors available for it make this impossible to achieve in all
  41.  * circumstances; there is no way to "reconnect" to an underlying Logger object on
  42.  * deserialization if one was just passed in to the constructor of the original
  43.  * object. This class <em>was</em> marked Serializable in the 1.0.4 release of
  44.  * commons-logging, but this never actually worked (a NullPointerException would
  45.  * be thrown as soon as the deserialized object was used), so removing this marker
  46.  * is not considered to be an incompatible change.
  47.  *
  48.  * @deprecated Scheduled for removal because the Apache Avalon Project has been discontinued.
  49.  */
  50. @Deprecated
  51. public class AvalonLogger implements Log {

  52.     /** Ancestral Avalon logger. */
  53.     private static volatile Logger defaultLogger;

  54.     /**
  55.      * Sets the ancestral Avalon logger from which the delegating loggers will descend.
  56.      *
  57.      * @param logger the default avalon logger,
  58.      * in case there is no logger instance supplied in constructor
  59.      */
  60.     public static void setDefaultLogger(final Logger logger) {
  61.         defaultLogger = logger;
  62.     }

  63.     /** Avalon logger used to perform log. */
  64.     private final transient Logger logger;

  65.     /**
  66.      * Constructs an {@code AvalonLogger} that outputs to the given
  67.      * {@code Logger} instance.
  68.      *
  69.      * @param logger the Avalon logger implementation to delegate to
  70.      */
  71.     public AvalonLogger(final Logger logger) {
  72.         this.logger = logger;
  73.     }

  74.     /**
  75.      * Constructs an {@code AvalonLogger} that will log to a child
  76.      * of the {@code Logger} set by calling {@link #setDefaultLogger}.
  77.      *
  78.      * @param name the name of the avalon logger implementation to delegate to
  79.      */
  80.     public AvalonLogger(final String name) {
  81.         Objects.requireNonNull(defaultLogger, "defaultLogger");
  82.         this.logger = defaultLogger.getChildLogger(name);
  83.     }

  84.     /**
  85.      * Logs a message with {@code org.apache.avalon.framework.logger.Logger.debug}.
  86.      *
  87.      * @param message to log.
  88.      * @see org.apache.commons.logging.Log#debug(Object)
  89.      */
  90.     @Override
  91.     public void debug(final Object message) {
  92.         if (getLogger().isDebugEnabled()) {
  93.             getLogger().debug(String.valueOf(message));
  94.         }
  95.     }

  96.     /**
  97.     * Logs a message with {@code org.apache.avalon.framework.logger.Logger.debug}.
  98.     *
  99.     * @param message to log
  100.     * @param t log this cause
  101.     * @see org.apache.commons.logging.Log#debug(Object, Throwable)
  102.      */
  103.     @Override
  104.     public void debug(final Object message, final Throwable t) {
  105.         if (getLogger().isDebugEnabled()) {
  106.             getLogger().debug(String.valueOf(message), t);
  107.         }
  108.     }

  109.     /**
  110.      * Logs a message with {@code org.apache.avalon.framework.logger.Logger.error}.
  111.      *
  112.      * @param message to log
  113.      * @see org.apache.commons.logging.Log#error(Object)
  114.      */
  115.     @Override
  116.     public void error(final Object message) {
  117.         if (getLogger().isErrorEnabled()) {
  118.             getLogger().error(String.valueOf(message));
  119.         }
  120.     }

  121.     /**
  122.      * Logs a message with {@code org.apache.avalon.framework.logger.Logger.error}.
  123.      *
  124.      * @param message to log
  125.      * @param t log this cause
  126.      * @see org.apache.commons.logging.Log#error(Object, Throwable)
  127.      */
  128.     @Override
  129.     public void error(final Object message, final Throwable t) {
  130.         if (getLogger().isErrorEnabled()) {
  131.             getLogger().error(String.valueOf(message), t);
  132.         }
  133.     }

  134.     /**
  135.      * Logs a message with {@code org.apache.avalon.framework.logger.Logger.fatalError}.
  136.      *
  137.      * @param message to log
  138.      * @see org.apache.commons.logging.Log#fatal(Object)
  139.      */
  140.     @Override
  141.     public void fatal(final Object message) {
  142.         if (getLogger().isFatalErrorEnabled()) {
  143.             getLogger().fatalError(String.valueOf(message));
  144.         }
  145.     }

  146.     /**
  147.      * Logs a message with {@code org.apache.avalon.framework.logger.Logger.fatalError}.
  148.      *
  149.      * @param message to log.
  150.      * @param t log this cause.
  151.      * @see org.apache.commons.logging.Log#fatal(Object, Throwable)
  152.      */
  153.     @Override
  154.     public void fatal(final Object message, final Throwable t) {
  155.         if (getLogger().isFatalErrorEnabled()) {
  156.             getLogger().fatalError(String.valueOf(message), t);
  157.         }
  158.     }

  159.     /**
  160.      * Gets the Avalon logger implementation used to perform logging.
  161.      *
  162.      * @return avalon logger implementation
  163.      */
  164.     public Logger getLogger() {
  165.         return logger;
  166.     }

  167.     /**
  168.      * Logs a message with {@code org.apache.avalon.framework.logger.Logger.info}.
  169.      *
  170.      * @param message to log
  171.      * @see org.apache.commons.logging.Log#info(Object)
  172.      */
  173.     @Override
  174.     public void info(final Object message) {
  175.         if (getLogger().isInfoEnabled()) {
  176.             getLogger().info(String.valueOf(message));
  177.         }
  178.     }

  179.     /**
  180.      * Logs a message with {@code org.apache.avalon.framework.logger.Logger.info}.
  181.      *
  182.      * @param message to log
  183.      * @param t log this cause
  184.      * @see org.apache.commons.logging.Log#info(Object, Throwable)
  185.      */
  186.     @Override
  187.     public void info(final Object message, final Throwable t) {
  188.         if (getLogger().isInfoEnabled()) {
  189.             getLogger().info(String.valueOf(message), t);
  190.         }
  191.     }

  192.     /**
  193.      * Is logging to {@code org.apache.avalon.framework.logger.Logger.debug} enabled?
  194.      * @see org.apache.commons.logging.Log#isDebugEnabled()
  195.      */
  196.     @Override
  197.     public boolean isDebugEnabled() {
  198.         return getLogger().isDebugEnabled();
  199.     }

  200.     /**
  201.      * Is logging to {@code org.apache.avalon.framework.logger.Logger.error} enabled?
  202.      * @see org.apache.commons.logging.Log#isErrorEnabled()
  203.      */
  204.     @Override
  205.     public boolean isErrorEnabled() {
  206.         return getLogger().isErrorEnabled();
  207.     }

  208.     /**
  209.      * Is logging to {@code org.apache.avalon.framework.logger.Logger.fatalError} enabled?
  210.      * @see org.apache.commons.logging.Log#isFatalEnabled()
  211.      */
  212.     @Override
  213.     public boolean isFatalEnabled() {
  214.         return getLogger().isFatalErrorEnabled();
  215.     }

  216.     /**
  217.      * Is logging to {@code org.apache.avalon.framework.logger.Logger.info} enabled?
  218.      * @see org.apache.commons.logging.Log#isInfoEnabled()
  219.      */
  220.     @Override
  221.     public boolean isInfoEnabled() {
  222.         return getLogger().isInfoEnabled();
  223.     }

  224.     /**
  225.      * Is logging to {@code org.apache.avalon.framework.logger.Logger.debug} enabled?
  226.      * @see org.apache.commons.logging.Log#isTraceEnabled()
  227.      */
  228.     @Override
  229.     public boolean isTraceEnabled() {
  230.         return getLogger().isDebugEnabled();
  231.     }

  232.     /**
  233.      * Is logging to {@code org.apache.avalon.framework.logger.Logger.warn} enabled?
  234.      * @see org.apache.commons.logging.Log#isWarnEnabled()
  235.      */
  236.     @Override
  237.     public boolean isWarnEnabled() {
  238.         return getLogger().isWarnEnabled();
  239.     }

  240.     /**
  241.      * Logs a message with {@code org.apache.avalon.framework.logger.Logger.debug}.
  242.      *
  243.      * @param message to log
  244.      * @see org.apache.commons.logging.Log#trace(Object)
  245.      */
  246.     @Override
  247.     public void trace(final Object message) {
  248.         if (getLogger().isDebugEnabled()) {
  249.             getLogger().debug(String.valueOf(message));
  250.         }
  251.     }

  252.     /**
  253.      * Logs a message with {@code org.apache.avalon.framework.logger.Logger.debug}.
  254.      *
  255.      * @param message to log.
  256.      * @param t log this cause.
  257.      * @see org.apache.commons.logging.Log#trace(Object, Throwable)
  258.      */
  259.     @Override
  260.     public void trace(final Object message, final Throwable t) {
  261.         if (getLogger().isDebugEnabled()) {
  262.             getLogger().debug(String.valueOf(message), t);
  263.         }
  264.     }

  265.     /**
  266.      * Logs a message with {@code org.apache.avalon.framework.logger.Logger.warn}.
  267.      *
  268.      * @param message to log
  269.      * @see org.apache.commons.logging.Log#warn(Object)
  270.      */
  271.     @Override
  272.     public void warn(final Object message) {
  273.         if (getLogger().isWarnEnabled()) {
  274.             getLogger().warn(String.valueOf(message));
  275.         }
  276.     }

  277.     /**
  278.      * Logs a message with {@code org.apache.avalon.framework.logger.Logger.warn}.
  279.      *
  280.      * @param message to log
  281.      * @param t log this cause
  282.      * @see org.apache.commons.logging.Log#warn(Object, Throwable)
  283.      */
  284.     @Override
  285.     public void warn(final Object message, final Throwable t) {
  286.         if (getLogger().isWarnEnabled()) {
  287.             getLogger().warn(String.valueOf(message), t);
  288.         }
  289.     }
  290. }