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.util.Objects;
021
022import org.apache.avalon.framework.logger.Logger;
023import org.apache.commons.logging.Log;
024
025/**
026 * Implements Commons Logging's Log interface to delegate all
027 * logging calls to the Avalon logging abstraction: the Logger interface.
028 * <p>
029 * There are two ways in which this class can be used:
030 * <ul>
031 * <li>the instance can be constructed with an Avalon logger
032 * (by calling {@link #AvalonLogger(Logger)}). In this case, it acts
033 * as a simple thin wrapping implementation over the logger. This is
034 * particularly useful when using a property setter.
035 * </li>
036 * <li>the {@link #setDefaultLogger} class property can be called which
037 * sets the ancestral Avalon logger for this class. Any {@code AvalonLogger}
038 * instances created through the {@code LogFactory} mechanisms will output
039 * to child loggers of this {@code Logger}.
040 * </li>
041 * </ul>
042 * <p>
043 * <strong>Note:</strong> {@code AvalonLogger} does not implement Serializable
044 * because the constructors available for it make this impossible to achieve in all
045 * circumstances; there is no way to "reconnect" to an underlying Logger object on
046 * deserialization if one was just passed in to the constructor of the original
047 * object. This class <em>was</em> marked Serializable in the 1.0.4 release of
048 * commons-logging, but this never actually worked (a NullPointerException would
049 * be thrown as soon as the deserialized object was used), so removing this marker
050 * is not considered to be an incompatible change.
051 *
052 * @deprecated Scheduled for removal because the Apache Avalon Project has been discontinued.
053 */
054@Deprecated
055public class AvalonLogger implements Log {
056
057    /** Ancestral Avalon logger. */
058    private static volatile Logger defaultLogger;
059
060    /**
061     * Sets the ancestral Avalon logger from which the delegating loggers will descend.
062     *
063     * @param logger the default avalon logger,
064     * in case there is no logger instance supplied in constructor
065     */
066    public static void setDefaultLogger(final Logger logger) {
067        defaultLogger = logger;
068    }
069
070    /** Avalon logger used to perform log. */
071    private final transient Logger logger;
072
073    /**
074     * Constructs an {@code AvalonLogger} that outputs to the given
075     * {@code Logger} instance.
076     *
077     * @param logger the Avalon logger implementation to delegate to
078     */
079    public AvalonLogger(final Logger logger) {
080        this.logger = logger;
081    }
082
083    /**
084     * Constructs an {@code AvalonLogger} that will log to a child
085     * of the {@code Logger} set by calling {@link #setDefaultLogger}.
086     *
087     * @param name the name of the avalon logger implementation to delegate to
088     */
089    public AvalonLogger(final String name) {
090        Objects.requireNonNull(defaultLogger, "defaultLogger");
091        this.logger = defaultLogger.getChildLogger(name);
092    }
093
094    /**
095     * Logs a message with {@code org.apache.avalon.framework.logger.Logger.debug}.
096     *
097     * @param message to log.
098     * @see org.apache.commons.logging.Log#debug(Object)
099     */
100    @Override
101    public void debug(final Object message) {
102        if (getLogger().isDebugEnabled()) {
103            getLogger().debug(String.valueOf(message));
104        }
105    }
106
107    /**
108    * Logs a message with {@code org.apache.avalon.framework.logger.Logger.debug}.
109    *
110    * @param message to log
111    * @param t log this cause
112    * @see org.apache.commons.logging.Log#debug(Object, Throwable)
113     */
114    @Override
115    public void debug(final Object message, final Throwable t) {
116        if (getLogger().isDebugEnabled()) {
117            getLogger().debug(String.valueOf(message), t);
118        }
119    }
120
121    /**
122     * Logs a message with {@code org.apache.avalon.framework.logger.Logger.error}.
123     *
124     * @param message to log
125     * @see org.apache.commons.logging.Log#error(Object)
126     */
127    @Override
128    public void error(final Object message) {
129        if (getLogger().isErrorEnabled()) {
130            getLogger().error(String.valueOf(message));
131        }
132    }
133
134    /**
135     * Logs a message with {@code org.apache.avalon.framework.logger.Logger.error}.
136     *
137     * @param message to log
138     * @param t log this cause
139     * @see org.apache.commons.logging.Log#error(Object, Throwable)
140     */
141    @Override
142    public void error(final Object message, final Throwable t) {
143        if (getLogger().isErrorEnabled()) {
144            getLogger().error(String.valueOf(message), t);
145        }
146    }
147
148    /**
149     * Logs a message with {@code org.apache.avalon.framework.logger.Logger.fatalError}.
150     *
151     * @param message to log
152     * @see org.apache.commons.logging.Log#fatal(Object)
153     */
154    @Override
155    public void fatal(final Object message) {
156        if (getLogger().isFatalErrorEnabled()) {
157            getLogger().fatalError(String.valueOf(message));
158        }
159    }
160
161    /**
162     * Logs a message with {@code org.apache.avalon.framework.logger.Logger.fatalError}.
163     *
164     * @param message to log.
165     * @param t log this cause.
166     * @see org.apache.commons.logging.Log#fatal(Object, Throwable)
167     */
168    @Override
169    public void fatal(final Object message, final Throwable t) {
170        if (getLogger().isFatalErrorEnabled()) {
171            getLogger().fatalError(String.valueOf(message), t);
172        }
173    }
174
175    /**
176     * Gets the Avalon logger implementation used to perform logging.
177     *
178     * @return avalon logger implementation
179     */
180    public Logger getLogger() {
181        return logger;
182    }
183
184    /**
185     * Logs a message with {@code org.apache.avalon.framework.logger.Logger.info}.
186     *
187     * @param message to log
188     * @see org.apache.commons.logging.Log#info(Object)
189     */
190    @Override
191    public void info(final Object message) {
192        if (getLogger().isInfoEnabled()) {
193            getLogger().info(String.valueOf(message));
194        }
195    }
196
197    /**
198     * Logs a message with {@code org.apache.avalon.framework.logger.Logger.info}.
199     *
200     * @param message to log
201     * @param t log this cause
202     * @see org.apache.commons.logging.Log#info(Object, Throwable)
203     */
204    @Override
205    public void info(final Object message, final Throwable t) {
206        if (getLogger().isInfoEnabled()) {
207            getLogger().info(String.valueOf(message), t);
208        }
209    }
210
211    /**
212     * Is logging to {@code org.apache.avalon.framework.logger.Logger.debug} enabled?
213     *
214     * @see org.apache.commons.logging.Log#isDebugEnabled()
215     */
216    @Override
217    public boolean isDebugEnabled() {
218        return getLogger().isDebugEnabled();
219    }
220
221    /**
222     * Is logging to {@code org.apache.avalon.framework.logger.Logger.error} enabled?
223     *
224     * @see org.apache.commons.logging.Log#isErrorEnabled()
225     */
226    @Override
227    public boolean isErrorEnabled() {
228        return getLogger().isErrorEnabled();
229    }
230
231    /**
232     * Is logging to {@code org.apache.avalon.framework.logger.Logger.fatalError} enabled?
233     *
234     * @see org.apache.commons.logging.Log#isFatalEnabled()
235     */
236    @Override
237    public boolean isFatalEnabled() {
238        return getLogger().isFatalErrorEnabled();
239    }
240
241    /**
242     * Is logging to {@code org.apache.avalon.framework.logger.Logger.info} enabled?
243     *
244     * @see org.apache.commons.logging.Log#isInfoEnabled()
245     */
246    @Override
247    public boolean isInfoEnabled() {
248        return getLogger().isInfoEnabled();
249    }
250
251    /**
252     * Is logging to {@code org.apache.avalon.framework.logger.Logger.debug} enabled?
253     *
254     * @see org.apache.commons.logging.Log#isTraceEnabled()
255     */
256    @Override
257    public boolean isTraceEnabled() {
258        return getLogger().isDebugEnabled();
259    }
260
261    /**
262     * Is logging to {@code org.apache.avalon.framework.logger.Logger.warn} enabled?
263     *
264     * @see org.apache.commons.logging.Log#isWarnEnabled()
265     */
266    @Override
267    public boolean isWarnEnabled() {
268        return getLogger().isWarnEnabled();
269    }
270
271    /**
272     * Logs a message with {@code org.apache.avalon.framework.logger.Logger.debug}.
273     *
274     * @param message to log
275     * @see org.apache.commons.logging.Log#trace(Object)
276     */
277    @Override
278    public void trace(final Object message) {
279        if (getLogger().isDebugEnabled()) {
280            getLogger().debug(String.valueOf(message));
281        }
282    }
283
284    /**
285     * Logs a message with {@code org.apache.avalon.framework.logger.Logger.debug}.
286     *
287     * @param message to log.
288     * @param t log this cause.
289     * @see org.apache.commons.logging.Log#trace(Object, Throwable)
290     */
291    @Override
292    public void trace(final Object message, final Throwable t) {
293        if (getLogger().isDebugEnabled()) {
294            getLogger().debug(String.valueOf(message), t);
295        }
296    }
297
298    /**
299     * Logs a message with {@code org.apache.avalon.framework.logger.Logger.warn}.
300     *
301     * @param message to log
302     * @see org.apache.commons.logging.Log#warn(Object)
303     */
304    @Override
305    public void warn(final Object message) {
306        if (getLogger().isWarnEnabled()) {
307            getLogger().warn(String.valueOf(message));
308        }
309    }
310
311    /**
312     * Logs a message with {@code org.apache.avalon.framework.logger.Logger.warn}.
313     *
314     * @param message to log
315     * @param t log this cause
316     * @see org.apache.commons.logging.Log#warn(Object, Throwable)
317     */
318    @Override
319    public void warn(final Object message, final Throwable t) {
320        if (getLogger().isWarnEnabled()) {
321            getLogger().warn(String.valueOf(message), t);
322        }
323    }
324}