ConfigurationLogger.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.configuration2.io;

  18. import org.apache.commons.logging.Log;
  19. import org.apache.commons.logging.LogFactory;
  20. import org.apache.commons.logging.impl.NoOpLog;

  21. /**
  22.  * <p>
  23.  * A class providing basic logging capabilities.
  24.  * </p>
  25.  * <p>
  26.  * When reading configuration files in complex scenarios having log output is useful for diagnostic purposes. Therefore,
  27.  * <em>Commons Configuration</em> produces some logging output. As concrete projects have different requirements on the
  28.  * amount and detail of logging, there is a way of configuring logging: All classes derived from
  29.  * {@link org.apache.commons.configuration2.AbstractConfiguration} can be assigned a logger which is then used for all
  30.  * log statements generated.
  31.  * </p>
  32.  * <p>
  33.  * Allowing a logger object to be passed to a configuration creates a direct dependency to a concrete logging framework
  34.  * in the configuration API. This would make it impossible to switch to an alternative logging framework without
  35.  * breaking backwards compatibility. To avoid this, the {@code ConfigurationLogger} class is introduced. It is a minimum
  36.  * abstraction over a logging framework offering only very basic logging capabilities. The methods defined in this class
  37.  * are used by configuration implementations to produce their logging statements. Client applications can create
  38.  * specialized instances and pass them to configuration objects without having to deal with a concrete logging
  39.  * framework. It is even possible to create a subclass that uses a completely different logging framework.
  40.  * </p>
  41.  *
  42.  * @since 2.0
  43.  */
  44. public class ConfigurationLogger {
  45.     /**
  46.      * Creates an internal logger for the given class. Throws an exception if the class is undefined.
  47.      *
  48.      * @param cls the logger class
  49.      * @return the logger object
  50.      * @throws IllegalArgumentException if the logger class is undefined
  51.      */
  52.     private static Log createLoggerForClass(final Class<?> cls) {
  53.         if (cls == null) {
  54.             throw new IllegalArgumentException("Logger class must not be null!");
  55.         }
  56.         return LogFactory.getLog(cls);
  57.     }

  58.     /**
  59.      * Creates an internal logger for the given name. Throws an exception if the name is undefined.
  60.      *
  61.      * @param name the name of the logger
  62.      * @return the logger object
  63.      * @throws IllegalArgumentException if the logger name is undefined
  64.      */
  65.     private static Log createLoggerForName(final String name) {
  66.         if (name == null) {
  67.             throw new IllegalArgumentException("Logger name must not be null!");
  68.         }
  69.         return LogFactory.getLog(name);
  70.     }

  71.     /**
  72.      * Creates a new dummy logger which produces no output. If such a logger is passed to a configuration object, logging is
  73.      * effectively disabled.
  74.      *
  75.      * @return the new dummy logger
  76.      */
  77.     public static ConfigurationLogger newDummyLogger() {
  78.         return new ConfigurationLogger(new NoOpLog());
  79.     }

  80.     /** The internal logger. */
  81.     private final Log log;

  82.     /**
  83.      * Creates a new, uninitialized instance of {@code ConfigurationLogger}. This constructor can be used by derived classes
  84.      * that implement their own specific logging mechanism. Such classes must override all methods because the default
  85.      * implementations do not work in this uninitialized state.
  86.      */
  87.     protected ConfigurationLogger() {
  88.         this((Log) null);
  89.     }

  90.     /**
  91.      * Creates a new instance of {@code ConfigurationLogger} that uses a logger whose name is derived from the provided
  92.      * class.
  93.      *
  94.      * @param logCls the class whose name is to be used for logging (must not be <strong>null</strong>)
  95.      * @throws IllegalArgumentException if the logger class is <strong>null</strong>
  96.      */
  97.     public ConfigurationLogger(final Class<?> logCls) {
  98.         this(createLoggerForClass(logCls));
  99.     }

  100.     /**
  101.      * Creates a new instance of {@code ConfigurationLogger} which wraps the specified logger.
  102.      *
  103.      * @param wrapped the logger to be wrapped
  104.      */
  105.     ConfigurationLogger(final Log wrapped) {
  106.         log = wrapped;
  107.     }

  108.     /**
  109.      * Creates a new instance of {@code ConfigurationLogger} that uses the specified logger name.
  110.      *
  111.      * @param loggerName the logger name (must not be <strong>null</strong>)
  112.      * @throws IllegalArgumentException if the logger name is <strong>null</strong>
  113.      */
  114.     public ConfigurationLogger(final String loggerName) {
  115.         this(createLoggerForName(loggerName));
  116.     }

  117.     /**
  118.      * Logs the specified message on debug level.
  119.      *
  120.      * @param msg the message to be logged
  121.      */
  122.     public void debug(final String msg) {
  123.         getLog().debug(msg);
  124.     }

  125.     /**
  126.      * Logs the specified message on error level.
  127.      *
  128.      * @param msg the message to be logged
  129.      */
  130.     public void error(final String msg) {
  131.         getLog().error(msg);
  132.     }

  133.     /**
  134.      * Logs the specified exception on error level.
  135.      *
  136.      * @param msg the message to be logged
  137.      * @param ex the exception to be logged
  138.      */
  139.     public void error(final String msg, final Throwable ex) {
  140.         getLog().error(msg, ex);
  141.     }

  142.     /**
  143.      * Gets the internal logger.
  144.      *
  145.      * @return the internal logger
  146.      */
  147.     Log getLog() {
  148.         return log;
  149.     }

  150.     /**
  151.      * Logs the specified message on info level.
  152.      *
  153.      * @param msg the message to be logged
  154.      */
  155.     public void info(final String msg) {
  156.         getLog().info(msg);
  157.     }

  158.     /**
  159.      * Returns a flag whether logging on debug level is enabled.
  160.      *
  161.      * @return <strong>true</strong> if debug logging is enabled, <strong>false</strong> otherwise
  162.      */
  163.     public boolean isDebugEnabled() {
  164.         return getLog().isDebugEnabled();
  165.     }

  166.     /**
  167.      * Returns a flag whether logging on info level is enabled.
  168.      *
  169.      * @return <strong>true</strong> if debug logging is enabled, <strong>false</strong> otherwise
  170.      */
  171.     public boolean isInfoEnabled() {
  172.         return getLog().isInfoEnabled();
  173.     }

  174.     /**
  175.      * Logs the specified message on warn level.
  176.      *
  177.      * @param msg the message to be logged
  178.      */
  179.     public void warn(final String msg) {
  180.         getLog().warn(msg);
  181.     }

  182.     /**
  183.      * Logs the specified exception on warn level.
  184.      *
  185.      * @param msg the message to be logged
  186.      * @param ex the exception to be logged
  187.      */
  188.     public void warn(final String msg, final Throwable ex) {
  189.         getLog().warn(msg, ex);
  190.     }
  191. }