LogKitLogger.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.log.Hierarchy;
  21. import org.apache.log.Logger;

  22. /**
  23.  * Implements {@code org.apache.commons.logging.Log}
  24.  * to wrap the <a href="https://avalon.apache.org/logkit/">Apache Avalon Logkit</a>
  25.  * logging system. Configuration of {@code LogKit} is left to the user.
  26.  * <p>
  27.  * {@code LogKit} accepts only {@code String} messages.
  28.  * Therefore, this implementation converts object messages into strings
  29.  * by called their {@code toString()} method before logging them.
  30.  * </p>
  31.  *
  32.  * @deprecated Scheduled for removal because the Apache Avalon Project has been discontinued.
  33.  */
  34. @Deprecated
  35. public class LogKitLogger implements Log, Serializable {

  36.     /** Serializable version identifier. */
  37.     private static final long serialVersionUID = 3768538055836059519L;

  38.     /** Logging goes to this {@code LogKit} logger */
  39.     protected transient volatile Logger logger;

  40.     /** Name of this logger */
  41.     protected String name;

  42.     /**
  43.      * Constructs {@code LogKitLogger} which wraps the {@code LogKit}
  44.      * logger with given name.
  45.      *
  46.      * @param name log name
  47.      */
  48.     public LogKitLogger(final String name) {
  49.         this.name = name;
  50.         this.logger = getLogger();
  51.     }

  52.     /**
  53.      * Logs a message with {@code org.apache.log.Priority.DEBUG}.
  54.      *
  55.      * @param message to log
  56.      * @see org.apache.commons.logging.Log#debug(Object)
  57.      */
  58.     @Override
  59.     public void debug(final Object message) {
  60.         if (message != null) {
  61.             getLogger().debug(String.valueOf(message));
  62.         }
  63.     }

  64.     /**
  65.      * Logs a message with {@code org.apache.log.Priority.DEBUG}.
  66.      *
  67.      * @param message to log
  68.      * @param t log this cause
  69.      * @see org.apache.commons.logging.Log#debug(Object, Throwable)
  70.      */
  71.     @Override
  72.     public void debug(final Object message, final Throwable t) {
  73.         if (message != null) {
  74.             getLogger().debug(String.valueOf(message), t);
  75.         }
  76.     }

  77.     /**
  78.      * Logs a message with {@code org.apache.log.Priority.ERROR}.
  79.      *
  80.      * @param message to log
  81.      * @see org.apache.commons.logging.Log#error(Object)
  82.      */
  83.     @Override
  84.     public void error(final Object message) {
  85.         if (message != null) {
  86.             getLogger().error(String.valueOf(message));
  87.         }
  88.     }

  89.     /**
  90.      * Logs a message with {@code org.apache.log.Priority.ERROR}.
  91.      *
  92.      * @param message to log
  93.      * @param t log this cause
  94.      * @see org.apache.commons.logging.Log#error(Object, Throwable)
  95.      */
  96.     @Override
  97.     public void error(final Object message, final Throwable t) {
  98.         if (message != null) {
  99.             getLogger().error(String.valueOf(message), t);
  100.         }
  101.     }

  102.     /**
  103.      * Logs a message with {@code org.apache.log.Priority.FATAL_ERROR}.
  104.      *
  105.      * @param message to log
  106.      * @see org.apache.commons.logging.Log#fatal(Object)
  107.      */
  108.     @Override
  109.     public void fatal(final Object message) {
  110.         if (message != null) {
  111.             getLogger().fatalError(String.valueOf(message));
  112.         }
  113.     }

  114.     /**
  115.      * Logs a message with {@code org.apache.log.Priority.FATAL_ERROR}.
  116.      *
  117.      * @param message to log
  118.      * @param t log this cause
  119.      * @see org.apache.commons.logging.Log#fatal(Object, Throwable)
  120.      */
  121.     @Override
  122.     public void fatal(final Object message, final Throwable t) {
  123.         if (message != null) {
  124.             getLogger().fatalError(String.valueOf(message), t);
  125.         }
  126.     }

  127.     /**
  128.      * Gets the underlying Logger we are using.
  129.      *
  130.      * @return the underlying Logger we are using.
  131.      */
  132.     public Logger getLogger() {
  133.         Logger result = logger;
  134.         if (result == null) {
  135.             synchronized(this) {
  136.                 result = logger;
  137.                 if (result == null) {
  138.                     logger = result = Hierarchy.getDefaultHierarchy().getLoggerFor(name);
  139.                 }
  140.             }
  141.         }
  142.         return result;
  143.     }

  144.     /**
  145.      * Logs a message with {@code org.apache.log.Priority.INFO}.
  146.      *
  147.      * @param message to log
  148.      * @see org.apache.commons.logging.Log#info(Object)
  149.      */
  150.     @Override
  151.     public void info(final Object message) {
  152.         if (message != null) {
  153.             getLogger().info(String.valueOf(message));
  154.         }
  155.     }

  156.     /**
  157.      * Logs a message with {@code org.apache.log.Priority.INFO}.
  158.      *
  159.      * @param message to log
  160.      * @param t log this cause
  161.      * @see org.apache.commons.logging.Log#info(Object, Throwable)
  162.      */
  163.     @Override
  164.     public void info(final Object message, final Throwable t) {
  165.         if (message != null) {
  166.             getLogger().info(String.valueOf(message), t);
  167.         }
  168.     }

  169.     /**
  170.      * Checks whether the {@code LogKit} logger will log messages of priority {@code DEBUG}.
  171.      */
  172.     @Override
  173.     public boolean isDebugEnabled() {
  174.         return getLogger().isDebugEnabled();
  175.     }

  176.     /**
  177.      * Checks whether the {@code LogKit} logger will log messages of priority {@code ERROR}.
  178.      */
  179.     @Override
  180.     public boolean isErrorEnabled() {
  181.         return getLogger().isErrorEnabled();
  182.     }

  183.     /**
  184.      * Checks whether the {@code LogKit} logger will log messages of priority {@code FATAL_ERROR}.
  185.      */
  186.     @Override
  187.     public boolean isFatalEnabled() {
  188.         return getLogger().isFatalErrorEnabled();
  189.     }

  190.     /**
  191.      * Checks whether the {@code LogKit} logger will log messages of priority {@code INFO}.
  192.      */
  193.     @Override
  194.     public boolean isInfoEnabled() {
  195.         return getLogger().isInfoEnabled();
  196.     }

  197.     /**
  198.      * Checks whether the {@code LogKit} logger will log messages of priority {@code DEBUG}.
  199.      */
  200.     @Override
  201.     public boolean isTraceEnabled() {
  202.         return getLogger().isDebugEnabled();
  203.     }

  204.     /**
  205.      * Checks whether the {@code LogKit} logger will log messages of priority {@code WARN}.
  206.      */
  207.     @Override
  208.     public boolean isWarnEnabled() {
  209.         return getLogger().isWarnEnabled();
  210.     }

  211.     /**
  212.      * Logs a message with {@code org.apache.log.Priority.DEBUG}.
  213.      *
  214.      * @param message to log
  215.      * @see org.apache.commons.logging.Log#trace(Object)
  216.     */
  217.     @Override
  218.     public void trace(final Object message) {
  219.         debug(message);
  220.     }

  221.     /**
  222.      * Logs a message with {@code org.apache.log.Priority.DEBUG}.
  223.      *
  224.      * @param message to log
  225.      * @param t log this cause
  226.      * @see org.apache.commons.logging.Log#trace(Object, Throwable)
  227.      */
  228.     @Override
  229.     public void trace(final Object message, final Throwable t) {
  230.         debug(message, t);
  231.     }

  232.     /**
  233.      * Logs a message with {@code org.apache.log.Priority.WARN}.
  234.      *
  235.      * @param message to log
  236.      * @see org.apache.commons.logging.Log#warn(Object)
  237.      */
  238.     @Override
  239.     public void warn(final Object message) {
  240.         if (message != null) {
  241.             getLogger().warn(String.valueOf(message));
  242.         }
  243.     }

  244.     /**
  245.      * Logs a message with {@code org.apache.log.Priority.WARN}.
  246.      *
  247.      * @param message to log
  248.      * @param t log this cause
  249.      * @see org.apache.commons.logging.Log#warn(Object, Throwable)
  250.      */
  251.     @Override
  252.     public void warn(final Object message, final Throwable t) {
  253.         if (message != null) {
  254.             getLogger().warn(String.valueOf(message), t);
  255.         }
  256.     }
  257. }