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   * </p>
31   * <ul>
32   * <li>the instance can be constructed with an Avalon logger
33   * (by calling {@link #AvalonLogger(Logger)}). In this case, it acts
34   * as a simple thin wrapping implementation over the logger. This is
35   * particularly useful when using a property setter.
36   * </li>
37   * <li>the {@link #setDefaultLogger} class property can be called which
38   * sets the ancestral Avalon logger for this class. Any {@code AvalonLogger}
39   * instances created through the {@code LogFactory} mechanisms will output
40   * to child loggers of this {@code Logger}.
41   * </li>
42   * </ul>
43   * <p>
44   * <strong>Note:</strong> {@code AvalonLogger} does not implement Serializable
45   * because the constructors available for it make this impossible to achieve in all
46   * circumstances; there is no way to "reconnect" to an underlying Logger object on
47   * deserialization if one was just passed in to the constructor of the original
48   * object. This class <em>was</em> marked Serializable in the 1.0.4 release of
49   * commons-logging, but this never actually worked (a NullPointerException would
50   * be thrown as soon as the deserialized object was used), so removing this marker
51   * is not considered to be an incompatible change.
52   * </p>
53   *
54   * @deprecated Scheduled for removal because the Apache Avalon Project has been discontinued.
55   */
56  @Deprecated
57  public class AvalonLogger implements Log {
58  
59      /** Ancestral Avalon logger. */
60      private static volatile Logger defaultLogger;
61  
62      /**
63       * Sets the ancestral Avalon logger from which the delegating loggers will descend.
64       *
65       * @param logger the default avalon logger,
66       * in case there is no logger instance supplied in constructor
67       */
68      public static void setDefaultLogger(final Logger logger) {
69          defaultLogger = logger;
70      }
71  
72      /** Avalon logger used to perform log. */
73      private final transient Logger logger;
74  
75      /**
76       * Constructs an {@code AvalonLogger} that outputs to the given
77       * {@code Logger} instance.
78       *
79       * @param logger the Avalon logger implementation to delegate to
80       */
81      public AvalonLogger(final Logger logger) {
82          this.logger = logger;
83      }
84  
85      /**
86       * Constructs an {@code AvalonLogger} that will log to a child
87       * of the {@code Logger} set by calling {@link #setDefaultLogger}.
88       *
89       * @param name the name of the avalon logger implementation to delegate to
90       */
91      public AvalonLogger(final String name) {
92          Objects.requireNonNull(defaultLogger, "defaultLogger");
93          this.logger = defaultLogger.getChildLogger(name);
94      }
95  
96      /**
97       * Logs a message with {@code org.apache.avalon.framework.logger.Logger.debug}.
98       *
99       * @param message to log.
100      * @see org.apache.commons.logging.Log#debug(Object)
101      */
102     @Override
103     public void debug(final Object message) {
104         if (getLogger().isDebugEnabled()) {
105             getLogger().debug(String.valueOf(message));
106         }
107     }
108 
109     /**
110     * Logs a message with {@code org.apache.avalon.framework.logger.Logger.debug}.
111     *
112     * @param message to log
113     * @param t log this cause
114     * @see org.apache.commons.logging.Log#debug(Object, Throwable)
115      */
116     @Override
117     public void debug(final Object message, final Throwable t) {
118         if (getLogger().isDebugEnabled()) {
119             getLogger().debug(String.valueOf(message), t);
120         }
121     }
122 
123     /**
124      * Logs a message with {@code org.apache.avalon.framework.logger.Logger.error}.
125      *
126      * @param message to log
127      * @see org.apache.commons.logging.Log#error(Object)
128      */
129     @Override
130     public void error(final Object message) {
131         if (getLogger().isErrorEnabled()) {
132             getLogger().error(String.valueOf(message));
133         }
134     }
135 
136     /**
137      * Logs a message with {@code org.apache.avalon.framework.logger.Logger.error}.
138      *
139      * @param message to log
140      * @param t log this cause
141      * @see org.apache.commons.logging.Log#error(Object, Throwable)
142      */
143     @Override
144     public void error(final Object message, final Throwable t) {
145         if (getLogger().isErrorEnabled()) {
146             getLogger().error(String.valueOf(message), t);
147         }
148     }
149 
150     /**
151      * Logs a message with {@code org.apache.avalon.framework.logger.Logger.fatalError}.
152      *
153      * @param message to log
154      * @see org.apache.commons.logging.Log#fatal(Object)
155      */
156     @Override
157     public void fatal(final Object message) {
158         if (getLogger().isFatalErrorEnabled()) {
159             getLogger().fatalError(String.valueOf(message));
160         }
161     }
162 
163     /**
164      * Logs a message with {@code org.apache.avalon.framework.logger.Logger.fatalError}.
165      *
166      * @param message to log.
167      * @param t log this cause.
168      * @see org.apache.commons.logging.Log#fatal(Object, Throwable)
169      */
170     @Override
171     public void fatal(final Object message, final Throwable t) {
172         if (getLogger().isFatalErrorEnabled()) {
173             getLogger().fatalError(String.valueOf(message), t);
174         }
175     }
176 
177     /**
178      * Gets the Avalon logger implementation used to perform logging.
179      *
180      * @return avalon logger implementation
181      */
182     public Logger getLogger() {
183         return logger;
184     }
185 
186     /**
187      * Logs a message with {@code org.apache.avalon.framework.logger.Logger.info}.
188      *
189      * @param message to log
190      * @see org.apache.commons.logging.Log#info(Object)
191      */
192     @Override
193     public void info(final Object message) {
194         if (getLogger().isInfoEnabled()) {
195             getLogger().info(String.valueOf(message));
196         }
197     }
198 
199     /**
200      * Logs a message with {@code org.apache.avalon.framework.logger.Logger.info}.
201      *
202      * @param message to log
203      * @param t log this cause
204      * @see org.apache.commons.logging.Log#info(Object, Throwable)
205      */
206     @Override
207     public void info(final Object message, final Throwable t) {
208         if (getLogger().isInfoEnabled()) {
209             getLogger().info(String.valueOf(message), t);
210         }
211     }
212 
213     /**
214      * Is logging to {@code org.apache.avalon.framework.logger.Logger.debug} enabled?
215      *
216      * @see org.apache.commons.logging.Log#isDebugEnabled()
217      */
218     @Override
219     public boolean isDebugEnabled() {
220         return getLogger().isDebugEnabled();
221     }
222 
223     /**
224      * Is logging to {@code org.apache.avalon.framework.logger.Logger.error} enabled?
225      *
226      * @see org.apache.commons.logging.Log#isErrorEnabled()
227      */
228     @Override
229     public boolean isErrorEnabled() {
230         return getLogger().isErrorEnabled();
231     }
232 
233     /**
234      * Is logging to {@code org.apache.avalon.framework.logger.Logger.fatalError} enabled?
235      *
236      * @see org.apache.commons.logging.Log#isFatalEnabled()
237      */
238     @Override
239     public boolean isFatalEnabled() {
240         return getLogger().isFatalErrorEnabled();
241     }
242 
243     /**
244      * Is logging to {@code org.apache.avalon.framework.logger.Logger.info} enabled?
245      *
246      * @see org.apache.commons.logging.Log#isInfoEnabled()
247      */
248     @Override
249     public boolean isInfoEnabled() {
250         return getLogger().isInfoEnabled();
251     }
252 
253     /**
254      * Is logging to {@code org.apache.avalon.framework.logger.Logger.debug} enabled?
255      *
256      * @see org.apache.commons.logging.Log#isTraceEnabled()
257      */
258     @Override
259     public boolean isTraceEnabled() {
260         return getLogger().isDebugEnabled();
261     }
262 
263     /**
264      * Is logging to {@code org.apache.avalon.framework.logger.Logger.warn} enabled?
265      *
266      * @see org.apache.commons.logging.Log#isWarnEnabled()
267      */
268     @Override
269     public boolean isWarnEnabled() {
270         return getLogger().isWarnEnabled();
271     }
272 
273     /**
274      * Logs a message with {@code org.apache.avalon.framework.logger.Logger.debug}.
275      *
276      * @param message to log
277      * @see org.apache.commons.logging.Log#trace(Object)
278      */
279     @Override
280     public void trace(final Object message) {
281         if (getLogger().isDebugEnabled()) {
282             getLogger().debug(String.valueOf(message));
283         }
284     }
285 
286     /**
287      * Logs a message with {@code org.apache.avalon.framework.logger.Logger.debug}.
288      *
289      * @param message to log.
290      * @param t log this cause.
291      * @see org.apache.commons.logging.Log#trace(Object, Throwable)
292      */
293     @Override
294     public void trace(final Object message, final Throwable t) {
295         if (getLogger().isDebugEnabled()) {
296             getLogger().debug(String.valueOf(message), t);
297         }
298     }
299 
300     /**
301      * Logs a message with {@code org.apache.avalon.framework.logger.Logger.warn}.
302      *
303      * @param message to log
304      * @see org.apache.commons.logging.Log#warn(Object)
305      */
306     @Override
307     public void warn(final Object message) {
308         if (getLogger().isWarnEnabled()) {
309             getLogger().warn(String.valueOf(message));
310         }
311     }
312 
313     /**
314      * Logs a message with {@code org.apache.avalon.framework.logger.Logger.warn}.
315      *
316      * @param message to log
317      * @param t log this cause
318      * @see org.apache.commons.logging.Log#warn(Object, Throwable)
319      */
320     @Override
321     public void warn(final Object message, final Throwable t) {
322         if (getLogger().isWarnEnabled()) {
323             getLogger().warn(String.valueOf(message), t);
324         }
325     }
326 }