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    *      https://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      *
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 }