View Javadoc

1   /*
2    * Copyright 2001-2004,2006 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */ 
16  
17  package org.apache.commons.logging.impl;
18  
19  import org.apache.avalon.framework.logger.Logger;
20  import org.apache.commons.logging.Log;
21  
22  /***
23   * <p>Implementation of commons-logging Log interface that delegates all
24   * logging calls to the Avalon logging abstraction: the Logger interface.
25   * </p>
26   * <p>
27   * There are two ways in which this class can be used:
28   * </p>
29   * <ul>
30   * <li>the instance can be constructed with an Avalon logger 
31   * (by calling {@link #AvalonLogger(Logger)}). In this case, it acts 
32   * as a simple thin wrapping implementation over the logger. This is 
33   * particularly useful when using a property setter.
34   * </li>
35   * <li>the {@link #setDefaultLogger} class property can be called which
36   * sets the ancesteral Avalon logger for this class. Any <code>AvalonLogger</code> 
37   * instances created through the <code>LogFactory</code> mechanisms will output
38   * to child loggers of this <code>Logger</code>.
39   * </li>
40   * </ul>
41   * <p>
42   * <strong>Note:</strong> <code>AvalonLogger</code> does not implement Serializable
43   * because the constructors available for it make this impossible to achieve in all
44   * circumstances; there is no way to "reconnect" to an underlying Logger object on
45   * deserialization if one was just passed in to the constructor of the original
46   * object. This class <i>was</i> marked Serializable in the 1.0.4 release of
47   * commons-logging, but this never actually worked (a NullPointerException would 
48   * be thrown as soon as the deserialized object was used), so removing this marker
49   * is not considered to be an incompatible change.
50   * </p>
51   * @author <a href="mailto:neeme@apache.org">Neeme Praks</a>
52   * @version $Revision: 399221 $ $Date: 2006-05-03 10:20:24 +0100 (Wed, 03 May 2006) $
53   */
54  public class AvalonLogger implements Log {
55  
56      /*** Ancesteral avalon logger  */ 
57      private static Logger defaultLogger = null;
58      /*** Avalon logger used to perform log */
59      private transient Logger logger = null;
60  
61      /***
62       * Constructs an <code>AvalonLogger</code> that outputs to the given
63       * <code>Logger</code> instance.
64       * @param logger the avalon logger implementation to delegate to
65       */
66      public AvalonLogger(Logger logger) {
67          this.logger = logger;
68      }
69  
70      /***
71       * Constructs an <code>AvalonLogger</code> that will log to a child
72       * of the <code>Logger</code> set by calling {@link #setDefaultLogger}.
73       * @param name the name of the avalon logger implementation to delegate to
74       */
75      public AvalonLogger(String name) {
76          if (defaultLogger == null)
77              throw new NullPointerException("default logger has to be specified if this constructor is used!");
78          this.logger = defaultLogger.getChildLogger(name);
79      }
80  
81      /***
82       * Gets the Avalon logger implementation used to perform logging.
83       * @return avalon logger implementation
84       */
85      public Logger getLogger() {
86          return logger;
87      }
88  
89      /***
90       * Sets the ancesteral Avalon logger from which the delegating loggers 
91       * will descend.
92       * @param logger the default avalon logger, 
93       * in case there is no logger instance supplied in constructor
94       */
95      public static void setDefaultLogger(Logger logger) {
96          defaultLogger = logger;
97      }
98  
99      /***
100     * Logs a message with
101     * <code>org.apache.avalon.framework.logger.Logger.debug</code>.
102     * 
103     * @param message to log
104     * @param t log this cause
105     * @see org.apache.commons.logging.Log#debug(Object, Throwable)
106      */
107     public void debug(Object message, Throwable t) {
108         if (getLogger().isDebugEnabled()) getLogger().debug(String.valueOf(message), t);
109     }
110 
111     /***
112      * Logs a message with
113      * <code>org.apache.avalon.framework.logger.Logger.debug</code>.
114      * 
115      * @param message to log.
116      * @see org.apache.commons.logging.Log#debug(Object)
117      */
118     public void debug(Object message) {
119         if (getLogger().isDebugEnabled()) getLogger().debug(String.valueOf(message));
120     }
121 
122     /***
123      * Logs a message with
124      * <code>org.apache.avalon.framework.logger.Logger.error</code>.
125      * 
126      * @param message to log
127      * @param t log this cause
128      * @see org.apache.commons.logging.Log#error(Object, Throwable)
129      */
130     public void error(Object message, Throwable t) {
131         if (getLogger().isErrorEnabled()) getLogger().error(String.valueOf(message), t);
132     }
133 
134     /***
135      * Logs a message with
136      * <code>org.apache.avalon.framework.logger.Logger.error</code>.
137      * 
138      * @param message to log
139      * @see org.apache.commons.logging.Log#error(Object)
140      */
141     public void error(Object message) {
142         if (getLogger().isErrorEnabled()) getLogger().error(String.valueOf(message));
143     }
144 
145     /***
146      * Logs a message with
147      * <code>org.apache.avalon.framework.logger.Logger.fatalError</code>.
148      * 
149      * @param message to log.
150      * @param t log this cause.
151      * @see org.apache.commons.logging.Log#fatal(Object, Throwable)
152      */
153     public void fatal(Object message, Throwable t) {
154         if (getLogger().isFatalErrorEnabled()) getLogger().fatalError(String.valueOf(message), t);
155     }
156 
157     /***
158      * Logs a message with
159      * <code>org.apache.avalon.framework.logger.Logger.fatalError</code>.
160      * 
161      * @param message to log
162      * @see org.apache.commons.logging.Log#fatal(Object)
163      */
164     public void fatal(Object message) {
165         if (getLogger().isFatalErrorEnabled()) getLogger().fatalError(String.valueOf(message));
166     }
167 
168     /***
169      * Logs a message with
170      * <code>org.apache.avalon.framework.logger.Logger.info</code>.
171      * 
172      * @param message to log
173      * @param t log this cause
174      * @see org.apache.commons.logging.Log#info(Object, Throwable)
175      */
176     public void info(Object message, Throwable t) {
177         if (getLogger().isInfoEnabled()) getLogger().info(String.valueOf(message), t);
178     }
179 
180     /***
181      * Logs a message with
182      * <code>org.apache.avalon.framework.logger.Logger.info</code>.
183      * 
184      * @param message to log
185      * @see org.apache.commons.logging.Log#info(Object)
186      */
187     public void info(Object message) {
188         if (getLogger().isInfoEnabled()) getLogger().info(String.valueOf(message));
189     }
190 
191     /***
192      * Is logging to 
193      * <code>org.apache.avalon.framework.logger.Logger.debug</code> enabled?
194      * @see org.apache.commons.logging.Log#isDebugEnabled()
195      */
196     public boolean isDebugEnabled() {
197         return getLogger().isDebugEnabled();
198     }
199 
200     /***
201      * Is logging to 
202      * <code>org.apache.avalon.framework.logger.Logger.error</code> enabled?
203      * @see org.apache.commons.logging.Log#isErrorEnabled()
204      */
205     public boolean isErrorEnabled() {
206         return getLogger().isErrorEnabled();
207     }
208 
209     /***
210      * Is logging to 
211      * <code>org.apache.avalon.framework.logger.Logger.fatalError</code> enabled?
212      * @see org.apache.commons.logging.Log#isFatalEnabled()
213      */
214     public boolean isFatalEnabled() {
215         return getLogger().isFatalErrorEnabled();
216     }
217 
218     /***
219      * Is logging to
220      * <code>org.apache.avalon.framework.logger.Logger.info</code> enabled?
221      * @see org.apache.commons.logging.Log#isInfoEnabled()
222      */
223     public boolean isInfoEnabled() {
224         return getLogger().isInfoEnabled();
225     }
226 
227     /***
228      * Is logging to 
229      * <code>org.apache.avalon.framework.logger.Logger.debug</code> enabled?
230      * @see org.apache.commons.logging.Log#isTraceEnabled()
231      */
232     public boolean isTraceEnabled() {
233         return getLogger().isDebugEnabled();
234     }
235 
236     /***
237      * Is logging to 
238      * <code>org.apache.avalon.framework.logger.Logger.warn</code> enabled?
239      * @see org.apache.commons.logging.Log#isWarnEnabled()
240      */
241     public boolean isWarnEnabled() {
242         return getLogger().isWarnEnabled();
243     }
244 
245     /***
246      * Logs a message with
247      * <code>org.apache.avalon.framework.logger.Logger.debug</code>.
248      * 
249      * @param message to log.
250      * @param t log this cause.
251      * @see org.apache.commons.logging.Log#trace(Object, Throwable)
252      */
253     public void trace(Object message, Throwable t) {
254         if (getLogger().isDebugEnabled()) getLogger().debug(String.valueOf(message), t);
255     }
256 
257     /***
258      * Logs a message with
259      * <code>org.apache.avalon.framework.logger.Logger.debug</code>.
260      * 
261      * @param message to log
262      * @see org.apache.commons.logging.Log#trace(Object)
263      */
264     public void trace(Object message) {
265         if (getLogger().isDebugEnabled()) getLogger().debug(String.valueOf(message));
266     }
267 
268     /***
269      * Logs a message with
270      * <code>org.apache.avalon.framework.logger.Logger.warn</code>.
271      * 
272      * @param message to log
273      * @param t log this cause
274      * @see org.apache.commons.logging.Log#warn(Object, Throwable)
275      */
276     public void warn(Object message, Throwable t) {
277         if (getLogger().isWarnEnabled()) getLogger().warn(String.valueOf(message), t);
278     }
279 
280     /***
281      * Logs a message with
282      * <code>org.apache.avalon.framework.logger.Logger.warn</code>.
283      * 
284      * @param message to log
285      * @see org.apache.commons.logging.Log#warn(Object)
286      */
287     public void warn(Object message) {
288         if (getLogger().isWarnEnabled()) getLogger().warn(String.valueOf(message));
289     }
290 
291 }