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  
18  package org.apache.commons.logging.impl;
19  
20  import java.io.Serializable;
21  import org.apache.log.Logger;
22  import org.apache.log.Hierarchy;
23  import org.apache.commons.logging.Log;
24  
25  /***
26   * <p>Implementation of <code>org.apache.commons.logging.Log</code>
27   * that wraps the <a href="http://avalon.apache.org/logkit/">avalon-logkit</a>
28   * logging system. Configuration of <code>LogKit</code> is left to the user.
29   * </p>
30   *
31   * <p><code>LogKit</code> accepts only <code>String</code> messages.
32   * Therefore, this implementation converts object messages into strings
33   * by called their <code>toString()</code> method before logging them.</p>
34   *
35   * @author <a href="mailto:sanders@apache.org">Scott Sanders</a>
36   * @author Robert Burrell Donkin
37   * @version $Id: LogKitLogger.java,v 1.9 2004/06/01 19:56:46 rdonkin Exp $
38   */
39  
40  public class LogKitLogger implements Log, Serializable {
41  
42  
43      // ------------------------------------------------------------- Attributes
44  
45  
46      /*** Logging goes to this <code>LogKit</code> logger */
47      protected transient Logger logger = null;
48  
49      /*** Name of this logger */
50      protected String name = null;
51  
52  
53      // ------------------------------------------------------------ Constructor
54  
55  
56      /***
57       * Construct <code>LogKitLogger</code> which wraps the <code>LogKit</code>
58       * logger with given name.
59       *
60       * @param name log name
61       */
62      public LogKitLogger(String name) {
63          this.name = name;
64          this.logger = getLogger();
65      }
66  
67  
68      // --------------------------------------------------------- Public Methods
69  
70  
71      /***
72       * <p>Return the underlying Logger we are using.</p>
73       */
74      public Logger getLogger() {
75  
76          if (logger == null) {
77              logger = Hierarchy.getDefaultHierarchy().getLoggerFor(name);
78          }
79          return (logger);
80  
81      }
82  
83  
84      // ----------------------------------------------------- Log Implementation
85  
86  
87      /***
88       * Log message to <code>LogKit</code> logger with <code>DEBUG</code> priority.
89       */
90      public void trace(Object message) {
91          debug(message);
92      }
93  
94  
95      /***
96       * Log error to <code>LogKit</code> logger with <code>DEBUG</code> priority.
97       */
98      public void trace(Object message, Throwable t) {
99          debug(message, t);
100     }
101 
102 
103     /***
104      * Log message to <code>LogKit</code> logger with <code>DEBUG</code> priority.
105      */
106     public void debug(Object message) {
107         if (message != null) {
108             getLogger().debug(String.valueOf(message));
109         }
110     }
111 
112 
113     /***
114      * Log error to <code>LogKit</code> logger with <code>DEBUG</code> priority.
115      */
116     public void debug(Object message, Throwable t) {
117         if (message != null) {
118             getLogger().debug(String.valueOf(message), t);
119         }
120     }
121 
122 
123     /***
124      * Log message to <code>LogKit</code> logger with <code>INFO</code> priority.
125      */
126     public void info(Object message) {
127         if (message != null) {
128             getLogger().info(String.valueOf(message));
129         }
130     }
131 
132 
133     /***
134      * Log error to <code>LogKit</code> logger with <code>INFO</code> priority.
135      */
136     public void info(Object message, Throwable t) {
137         if (message != null) {
138             getLogger().info(String.valueOf(message), t);
139         }
140     }
141 
142 
143     /***
144      * Log message to <code>LogKit</code> logger with <code>WARN</code> priority.
145      */
146     public void warn(Object message) {
147         if (message != null) {
148             getLogger().warn(String.valueOf(message));
149         }
150     }
151 
152 
153     /***
154      * Log error to <code>LogKit</code> logger with <code>WARN</code> priority.
155      */
156     public void warn(Object message, Throwable t) {
157         if (message != null) {
158             getLogger().warn(String.valueOf(message), t);
159         }
160     }
161 
162 
163     /***
164      * Log message to <code>LogKit</code> logger with <code>ERROR</code> priority.
165      */
166     public void error(Object message) {
167         if (message != null) {
168             getLogger().error(String.valueOf(message));
169         }
170     }
171 
172 
173     /***
174      * Log error to <code>LogKit</code> logger with <code>ERROR</code> priority.
175      */
176     public void error(Object message, Throwable t) {
177         if (message != null) {
178             getLogger().error(String.valueOf(message), t);
179         }
180     }
181 
182 
183     /***
184      * Log message to <code>LogKit</code> logger with <code>FATAL_ERROR</code> priority.
185      */
186     public void fatal(Object message) {
187         if (message != null) {
188             getLogger().fatalError(String.valueOf(message));
189         }
190     }
191 
192 
193     /***
194      * Log error to <code>LogKit</code> logger with <code>FATAL_ERROR</code> priority.
195      */
196     public void fatal(Object message, Throwable t) {
197         if (message != null) {
198             getLogger().fatalError(String.valueOf(message), t);
199         }
200     }
201 
202 
203     /***
204      * Check whether the <code>LogKit</code> logger will log messages of priority <code>DEBUG</code>.
205      */
206     public boolean isDebugEnabled() {
207         return getLogger().isDebugEnabled();
208     }
209 
210 
211     /***
212      * Check whether the <code>LogKit</code> logger will log messages of priority <code>ERROR</code>.
213      */
214     public boolean isErrorEnabled() {
215         return getLogger().isErrorEnabled();
216     }
217 
218 
219     /***
220      * Check whether the <code>LogKit</code> logger will log messages of priority <code>FATAL_ERROR</code>.
221      */
222     public boolean isFatalEnabled() {
223         return getLogger().isFatalErrorEnabled();
224     }
225 
226 
227     /***
228      * Check whether the <code>LogKit</code> logger will log messages of priority <code>INFO</code>.
229      */
230     public boolean isInfoEnabled() {
231         return getLogger().isInfoEnabled();
232     }
233 
234 
235     /***
236      * Check whether the <code>LogKit</code> logger will log messages of priority <code>DEBUG</code>.
237      */
238     public boolean isTraceEnabled() {
239         return getLogger().isDebugEnabled();
240     }
241 
242 
243     /***
244      * Check whether the <code>LogKit</code> logger will log messages of priority <code>WARN</code>.
245      */
246     public boolean isWarnEnabled() {
247         return getLogger().isWarnEnabled();
248     }
249 
250 
251 }