View Javadoc
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  
18  package org.apache.commons.logging.impl;
19  
20  import java.util.Objects;
21  
22  import org.apache.avalon.framework.logger.Logger;
23  import org.apache.commons.logging.Log;
24  
25  /**
26   * Implements Commons Logging's Log interface to delegate all
27   * logging calls to the Avalon logging abstraction: the Logger interface.
28   * <p>
29   * There are two ways in which this class can be used:
30   * <ul>
31   * <li>the instance can be constructed with an Avalon logger
32   * (by calling {@link #AvalonLogger(Logger)}). In this case, it acts
33   * as a simple thin wrapping implementation over the logger. This is
34   * particularly useful when using a property setter.
35   * </li>
36   * <li>the {@link #setDefaultLogger} class property can be called which
37   * sets the ancestral Avalon logger for this class. Any {@code AvalonLogger}
38   * instances created through the {@code LogFactory} mechanisms will output
39   * to child loggers of this {@code Logger}.
40   * </li>
41   * </ul>
42   * <p>
43   * <strong>Note:</strong> {@code AvalonLogger} does not implement Serializable
44   * because the constructors available for it make this impossible to achieve in all
45   * circumstances; there is no way to "reconnect" to an underlying Logger object on
46   * deserialization if one was just passed in to the constructor of the original
47   * object. This class <em>was</em> marked Serializable in the 1.0.4 release of
48   * commons-logging, but this never actually worked (a NullPointerException would
49   * be thrown as soon as the deserialized object was used), so removing this marker
50   * is not considered to be an incompatible change.
51   *
52   * @deprecated Scheduled for removal because the Apache Avalon Project has been discontinued.
53   */
54  @Deprecated
55  public class AvalonLogger implements Log {
56  
57      /** Ancestral Avalon logger. */
58      private static volatile Logger defaultLogger;
59  
60      /**
61       * Sets the ancestral Avalon logger from which the delegating loggers will descend.
62       *
63       * @param logger the default avalon logger,
64       * in case there is no logger instance supplied in constructor
65       */
66      public static void setDefaultLogger(final Logger logger) {
67          defaultLogger = logger;
68      }
69  
70      /** Avalon logger used to perform log. */
71      private final transient Logger logger;
72  
73      /**
74       * Constructs an {@code AvalonLogger} that outputs to the given
75       * {@code Logger} instance.
76       *
77       * @param logger the Avalon logger implementation to delegate to
78       */
79      public AvalonLogger(final Logger logger) {
80          this.logger = logger;
81      }
82  
83      /**
84       * Constructs an {@code AvalonLogger} that will log to a child
85       * of the {@code Logger} set by calling {@link #setDefaultLogger}.
86       *
87       * @param name the name of the avalon logger implementation to delegate to
88       */
89      public AvalonLogger(final String name) {
90          Objects.requireNonNull(defaultLogger, "defaultLogger");
91          this.logger = defaultLogger.getChildLogger(name);
92      }
93  
94      /**
95       * Logs a message with {@code org.apache.avalon.framework.logger.Logger.debug}.
96       *
97       * @param message to log.
98       * @see org.apache.commons.logging.Log#debug(Object)
99       */
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      * @see org.apache.commons.logging.Log#isDebugEnabled()
214      */
215     @Override
216     public boolean isDebugEnabled() {
217         return getLogger().isDebugEnabled();
218     }
219 
220     /**
221      * Is logging to {@code org.apache.avalon.framework.logger.Logger.error} enabled?
222      * @see org.apache.commons.logging.Log#isErrorEnabled()
223      */
224     @Override
225     public boolean isErrorEnabled() {
226         return getLogger().isErrorEnabled();
227     }
228 
229     /**
230      * Is logging to {@code org.apache.avalon.framework.logger.Logger.fatalError} enabled?
231      * @see org.apache.commons.logging.Log#isFatalEnabled()
232      */
233     @Override
234     public boolean isFatalEnabled() {
235         return getLogger().isFatalErrorEnabled();
236     }
237 
238     /**
239      * Is logging to {@code org.apache.avalon.framework.logger.Logger.info} enabled?
240      * @see org.apache.commons.logging.Log#isInfoEnabled()
241      */
242     @Override
243     public boolean isInfoEnabled() {
244         return getLogger().isInfoEnabled();
245     }
246 
247     /**
248      * Is logging to {@code org.apache.avalon.framework.logger.Logger.debug} enabled?
249      * @see org.apache.commons.logging.Log#isTraceEnabled()
250      */
251     @Override
252     public boolean isTraceEnabled() {
253         return getLogger().isDebugEnabled();
254     }
255 
256     /**
257      * Is logging to {@code org.apache.avalon.framework.logger.Logger.warn} enabled?
258      * @see org.apache.commons.logging.Log#isWarnEnabled()
259      */
260     @Override
261     public boolean isWarnEnabled() {
262         return getLogger().isWarnEnabled();
263     }
264 
265     /**
266      * Logs a message with {@code org.apache.avalon.framework.logger.Logger.debug}.
267      *
268      * @param message to log
269      * @see org.apache.commons.logging.Log#trace(Object)
270      */
271     @Override
272     public void trace(final Object message) {
273         if (getLogger().isDebugEnabled()) {
274             getLogger().debug(String.valueOf(message));
275         }
276     }
277 
278     /**
279      * Logs a message with {@code org.apache.avalon.framework.logger.Logger.debug}.
280      *
281      * @param message to log.
282      * @param t log this cause.
283      * @see org.apache.commons.logging.Log#trace(Object, Throwable)
284      */
285     @Override
286     public void trace(final Object message, final Throwable t) {
287         if (getLogger().isDebugEnabled()) {
288             getLogger().debug(String.valueOf(message), t);
289         }
290     }
291 
292     /**
293      * Logs a message with {@code org.apache.avalon.framework.logger.Logger.warn}.
294      *
295      * @param message to log
296      * @see org.apache.commons.logging.Log#warn(Object)
297      */
298     @Override
299     public void warn(final Object message) {
300         if (getLogger().isWarnEnabled()) {
301             getLogger().warn(String.valueOf(message));
302         }
303     }
304 
305     /**
306      * Logs a message with {@code org.apache.avalon.framework.logger.Logger.warn}.
307      *
308      * @param message to log
309      * @param t log this cause
310      * @see org.apache.commons.logging.Log#warn(Object, Throwable)
311      */
312     @Override
313     public void warn(final Object message, final Throwable t) {
314         if (getLogger().isWarnEnabled()) {
315             getLogger().warn(String.valueOf(message), t);
316         }
317     }
318 }