    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.log.Hierarchy;
    024import org.apache.log.Logger;
    025
    026/**
    027 * Implements {@code org.apache.commons.logging.Log}
    028 * to wrap the <a href="https://avalon.apache.org/logkit/">Apache Avalon Logkit</a>
    029 * logging system. Configuration of {@code LogKit} is left to the user.
    030 * <p>
    031 * {@code LogKit} accepts only {@code String} messages.
    032 * Therefore, this implementation converts object messages into strings
    033 * by called their {@code toString()} method before logging them.
    034 * </p>
    035 *
    036 * @deprecated Scheduled for removal because the Apache Avalon Project has been discontinued.
    037 */
    038@Deprecated
    039public class LogKitLogger implements Log, Serializable {
    040
    041    /** Serializable version identifier. */
    042    private static final long serialVersionUID = 3768538055836059519L;
    043
    044    /** Logging goes to this {@code LogKit} logger */
    045    protected transient volatile Logger logger;
    046
    047    /** Name of this logger */
    048    protected String name;
    049
    050    /**
    051     * Constructs {@code LogKitLogger} which wraps the {@code LogKit}
    052     * logger with given name.
    053     *
    054     * @param name log name
    055     */
    056    public LogKitLogger(final String name) {
    057        this.name = name;
    058        this.logger = getLogger();
    059    }
    060
    061    /**
    062     * Logs a message with {@code org.apache.log.Priority.DEBUG}.
    063     *
    064     * @param message to log
    065     * @see org.apache.commons.logging.Log#debug(Object)
    066     */
    067    @Override
    068    public void debug(final Object message) {
    069        if (message != null) {
    070            getLogger().debug(String.valueOf(message));
    071        }
    072    }
    073
    074    /**
    075     * Logs a message with {@code org.apache.log.Priority.DEBUG}.
    076     *
    077     * @param message to log
    078     * @param t log this cause
    079     * @see org.apache.commons.logging.Log#debug(Object, Throwable)
    080     */
    081    @Override
    082    public void debug(final Object message, final Throwable t) {
    083        if (message != null) {
    084            getLogger().debug(String.valueOf(message), t);
    085        }
    086    }
    087
    088    /**
    089     * Logs a message with {@code org.apache.log.Priority.ERROR}.
    090     *
    091     * @param message to log
    092     * @see org.apache.commons.logging.Log#error(Object)
    093     */
    094    @Override
    095    public void error(final Object message) {
    096        if (message != null) {
    097            getLogger().error(String.valueOf(message));
    098        }
    099    }
    100
    101    /**
    102     * Logs a message with {@code org.apache.log.Priority.ERROR}.
    103     *
    104     * @param message to log
    105     * @param t log this cause
    106     * @see org.apache.commons.logging.Log#error(Object, Throwable)
    107     */
    108    @Override
    109    public void error(final Object message, final Throwable t) {
    110        if (message != null) {
    111            getLogger().error(String.valueOf(message), t);
    112        }
    113    }
    114
    115    /**
    116     * Logs a message with {@code org.apache.log.Priority.FATAL_ERROR}.
    117     *
    118     * @param message to log
    119     * @see org.apache.commons.logging.Log#fatal(Object)
    120     */
    121    @Override
    122    public void fatal(final Object message) {
    123        if (message != null) {
    124            getLogger().fatalError(String.valueOf(message));
    125        }
    126    }
    127
    128    /**
    129     * Logs a message with {@code org.apache.log.Priority.FATAL_ERROR}.
    130     *
    131     * @param message to log
    132     * @param t log this cause
    133     * @see org.apache.commons.logging.Log#fatal(Object, Throwable)
    134     */
    135    @Override
    136    public void fatal(final Object message, final Throwable t) {
    137        if (message != null) {
    138            getLogger().fatalError(String.valueOf(message), t);
    139        }
    140    }
    141
    142    /**
    143     * Gets the underlying Logger we are using.
    144     *
    145     * @return the underlying Logger we are using.
    146     */
    147    public Logger getLogger() {
    148        Logger result = logger;
    149        if (result == null) {
    150            synchronized(this) {
    151                result = logger;
    152                if (result == null) {
    153                    logger = result = Hierarchy.getDefaultHierarchy().getLoggerFor(name);
    154                }
    155            }
    156        }
    157        return result;
    158    }
    159
    160    /**
    161     * Logs a message with {@code org.apache.log.Priority.INFO}.
    162     *
    163     * @param message to log
    164     * @see org.apache.commons.logging.Log#info(Object)
    165     */
    166    @Override
    167    public void info(final Object message) {
    168        if (message != null) {
    169            getLogger().info(String.valueOf(message));
    170        }
    171    }
    172
    173    /**
    174     * Logs a message with {@code org.apache.log.Priority.INFO}.
    175     *
    176     * @param message to log
    177     * @param t log this cause
    178     * @see org.apache.commons.logging.Log#info(Object, Throwable)
    179     */
    180    @Override
    181    public void info(final Object message, final Throwable t) {
    182        if (message != null) {
    183            getLogger().info(String.valueOf(message), t);
    184        }
    185    }
    186
    187    /**
    188     * Checks whether the {@code LogKit} logger will log messages of priority {@code DEBUG}.
    189     */
    190    @Override
    191    public boolean isDebugEnabled() {
    192        return getLogger().isDebugEnabled();
    193    }
    194
    195    /**
    196     * Checks whether the {@code LogKit} logger will log messages of priority {@code ERROR}.
    197     */
    198    @Override
    199    public boolean isErrorEnabled() {
    200        return getLogger().isErrorEnabled();
    201    }
    202
    203    /**
    204     * Checks whether the {@code LogKit} logger will log messages of priority {@code FATAL_ERROR}.
    205     */
    206    @Override
    207    public boolean isFatalEnabled() {
    208        return getLogger().isFatalErrorEnabled();
    209    }
    210
    211    /**
    212     * Checks whether the {@code LogKit} logger will log messages of priority {@code INFO}.
    213     */
    214    @Override
    215    public boolean isInfoEnabled() {
    216        return getLogger().isInfoEnabled();
    217    }
    218
    219    /**
    220     * Checks whether the {@code LogKit} logger will log messages of priority {@code DEBUG}.
    221     */
    222    @Override
    223    public boolean isTraceEnabled() {
    224        return getLogger().isDebugEnabled();
    225    }
    226
    227    /**
    228     * Checks whether the {@code LogKit} logger will log messages of priority {@code WARN}.
    229     */
    230    @Override
    231    public boolean isWarnEnabled() {
    232        return getLogger().isWarnEnabled();
    233    }
    234
    235    /**
    236     * Logs a message with {@code org.apache.log.Priority.DEBUG}.
    237     *
    238     * @param message to log
    239     * @see org.apache.commons.logging.Log#trace(Object)
    240    */
    241    @Override
    242    public void trace(final Object message) {
    243        debug(message);
    244    }
    245
    246    /**
    247     * Logs a message with {@code org.apache.log.Priority.DEBUG}.
    248     *
    249     * @param message to log
    250     * @param t log this cause
    251     * @see org.apache.commons.logging.Log#trace(Object, Throwable)
    252     */
    253    @Override
    254    public void trace(final Object message, final Throwable t) {
    255        debug(message, t);
    256    }
    257
    258    /**
    259     * Logs a message with {@code org.apache.log.Priority.WARN}.
    260     *
    261     * @param message to log
    262     * @see org.apache.commons.logging.Log#warn(Object)
    263     */
    264    @Override
    265    public void warn(final Object message) {
    266        if (message != null) {
    267            getLogger().warn(String.valueOf(message));
    268        }
    269    }
    270
    271    /**
    272     * Logs a message with {@code org.apache.log.Priority.WARN}.
    273     *
    274     * @param message to log
    275     * @param t log this cause
    276     * @see org.apache.commons.logging.Log#warn(Object, Throwable)
    277     */
    278    @Override
    279    public void warn(final Object message, final Throwable t) {
    280        if (message != null) {
    281            getLogger().warn(String.valueOf(message), t);
    282        }
    283    }
    284}