View Javadoc

1   /*
2    * Copyright 2001-2004 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 java.io.Serializable;
20  import org.apache.avalon.framework.logger.Logger;
21  import org.apache.commons.logging.Log;
22  
23  /***
24   * <p>Implementation of commons-logging Log interface that delegates all
25   * logging calls to the Avalon logging abstraction: the Logger interface.
26   * </p>
27   * <p>
28   * There are two ways in which this class can be used:
29   * </p>
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 ancesteral Avalon logger for this class. Any <code>AvalonLogger</code> 
38   * instances created through the <code>LogFactory</code> mechanisms will output
39   * to child loggers of this <code>Logger</code>.
40   * </li>
41   * </ul>
42   *
43   * @author <a href="mailto:neeme@apache.org">Neeme Praks</a>
44   * @version $Revision: 1.9 $ $Date: 2004/06/01 19:56:20 $
45   */
46  public class AvalonLogger implements Log, Serializable {
47  
48      /*** Ancesteral avalon logger  */ 
49      private static Logger defaultLogger = null;
50      /*** Avalon logger used to perform log */
51      private transient Logger logger = null;
52      /*** The name of this logger */
53      private String name = null;
54  
55      /***
56       * Constructs an <code>AvalonLogger</code> that outputs to the given
57       * <code>Logger</code> instance.
58       * @param logger the avalon logger implementation to delegate to
59       */
60      public AvalonLogger(Logger logger) {
61          this.name = name;
62          this.logger = logger;
63      }
64  
65      /***
66       * Constructs an <code>AvalonLogger</code> that will log to a child
67       * of the <code>Logger</code> set by calling {@link #setDefaultLogger}.
68       * @param name the name of the avalon logger implementation to delegate to
69       */
70      public AvalonLogger(String name) {
71          if (defaultLogger == null)
72              throw new NullPointerException("default logger has to be specified if this constructor is used!");
73          this.logger = getLogger();
74      }
75  
76      /***
77       * Gets the Avalon logger implementation used to perform logging.
78       * @return avalon logger implementation
79       */
80      public Logger getLogger() {
81          if (logger == null) {
82              logger = defaultLogger.getChildLogger(name);
83          }
84          return logger;
85      }
86  
87      /***
88       * Sets the ancesteral Avalon logger from which the delegating loggers 
89       * will descend.
90       * @param logger the default avalon logger, 
91       * in case there is no logger instance supplied in constructor
92       */
93      public static void setDefaultLogger(Logger logger) {
94          defaultLogger = logger;
95      }
96  
97      /***
98       * @see org.apache.commons.logging.Log#debug(java.lang.Object, java.lang.Throwable)
99       */
100     public void debug(Object o, Throwable t) {
101         if (getLogger().isDebugEnabled()) getLogger().debug(String.valueOf(o), t);
102     }
103 
104     /***
105      * @see org.apache.commons.logging.Log#debug(java.lang.Object)
106      */
107     public void debug(Object o) {
108         if (getLogger().isDebugEnabled()) getLogger().debug(String.valueOf(o));
109     }
110 
111     /***
112      * @see org.apache.commons.logging.Log#error(java.lang.Object, java.lang.Throwable)
113      */
114     public void error(Object o, Throwable t) {
115         if (getLogger().isErrorEnabled()) getLogger().error(String.valueOf(o), t);
116     }
117 
118     /***
119      * @see org.apache.commons.logging.Log#error(java.lang.Object)
120      */
121     public void error(Object o) {
122         if (getLogger().isErrorEnabled()) getLogger().error(String.valueOf(o));
123     }
124 
125     /***
126      * @see org.apache.commons.logging.Log#fatal(java.lang.Object, java.lang.Throwable)
127      */
128     public void fatal(Object o, Throwable t) {
129         if (getLogger().isFatalErrorEnabled()) getLogger().fatalError(String.valueOf(o), t);
130     }
131 
132     /***
133      * @see org.apache.commons.logging.Log#fatal(java.lang.Object)
134      */
135     public void fatal(Object o) {
136         if (getLogger().isFatalErrorEnabled()) getLogger().fatalError(String.valueOf(o));
137     }
138 
139     /***
140      * @see org.apache.commons.logging.Log#info(java.lang.Object, java.lang.Throwable)
141      */
142     public void info(Object o, Throwable t) {
143         if (getLogger().isInfoEnabled()) getLogger().info(String.valueOf(o), t);
144     }
145 
146     /***
147      * @see org.apache.commons.logging.Log#info(java.lang.Object)
148      */
149     public void info(Object o) {
150         if (getLogger().isInfoEnabled()) getLogger().info(String.valueOf(o));
151     }
152 
153     /***
154      * @see org.apache.commons.logging.Log#isDebugEnabled()
155      */
156     public boolean isDebugEnabled() {
157         return getLogger().isDebugEnabled();
158     }
159 
160     /***
161      * @see org.apache.commons.logging.Log#isErrorEnabled()
162      */
163     public boolean isErrorEnabled() {
164         return getLogger().isErrorEnabled();
165     }
166 
167     /***
168      * @see org.apache.commons.logging.Log#isFatalEnabled()
169      */
170     public boolean isFatalEnabled() {
171         return getLogger().isFatalErrorEnabled();
172     }
173 
174     /***
175      * @see org.apache.commons.logging.Log#isInfoEnabled()
176      */
177     public boolean isInfoEnabled() {
178         return getLogger().isInfoEnabled();
179     }
180 
181     /***
182      * @see org.apache.commons.logging.Log#isTraceEnabled()
183      */
184     public boolean isTraceEnabled() {
185         return getLogger().isDebugEnabled();
186     }
187 
188     /***
189      * @see org.apache.commons.logging.Log#isWarnEnabled()
190      */
191     public boolean isWarnEnabled() {
192         return getLogger().isWarnEnabled();
193     }
194 
195     /***
196      * @see org.apache.commons.logging.Log#trace(java.lang.Object, java.lang.Throwable)
197      */
198     public void trace(Object o, Throwable t) {
199         if (getLogger().isDebugEnabled()) getLogger().debug(String.valueOf(o), t);
200     }
201 
202     /***
203      * @see org.apache.commons.logging.Log#trace(java.lang.Object)
204      */
205     public void trace(Object o) {
206         if (getLogger().isDebugEnabled()) getLogger().debug(String.valueOf(o));
207     }
208 
209     /***
210      * @see org.apache.commons.logging.Log#warn(java.lang.Object, java.lang.Throwable)
211      */
212     public void warn(Object o, Throwable t) {
213         if (getLogger().isWarnEnabled()) getLogger().warn(String.valueOf(o), t);
214     }
215 
216     /***
217      * @see org.apache.commons.logging.Log#warn(java.lang.Object)
218      */
219     public void warn(Object o) {
220         if (getLogger().isWarnEnabled()) getLogger().warn(String.valueOf(o));
221     }
222 
223 }