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 370631 2006-01-19 20:50:46Z rdonkin $
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       * Logs a message with <code>org.apache.log.Priority.DEBUG</code>.
89       * 
90       * @param message to log
91       * @see org.apache.commons.logging.Log#trace(Object)
92      */
93      public void trace(Object message) {
94          debug(message);
95      }
96  
97  
98      /***
99       * Logs a message with <code>org.apache.log.Priority.DEBUG</code>.
100      * 
101      * @param message to log
102      * @param t log this cause
103      * @see org.apache.commons.logging.Log#trace(Object, Throwable)
104      */
105     public void trace(Object message, Throwable t) {
106         debug(message, t);
107     }
108 
109 
110     /***
111      * Logs a message with <code>org.apache.log.Priority.DEBUG</code>.
112      * 
113      * @param message to log
114      * @see org.apache.commons.logging.Log#debug(Object)
115      */
116     public void debug(Object message) {
117         if (message != null) {
118             getLogger().debug(String.valueOf(message));
119         }
120     }
121 
122 
123     /***
124      * Logs a message with <code>org.apache.log.Priority.DEBUG</code>.
125      * 
126      * @param message to log
127      * @param t log this cause
128      * @see org.apache.commons.logging.Log#debug(Object, Throwable)
129      */
130     public void debug(Object message, Throwable t) {
131         if (message != null) {
132             getLogger().debug(String.valueOf(message), t);
133         }
134     }
135 
136 
137     /***
138      * Logs a message with <code>org.apache.log.Priority.INFO</code>.
139      * 
140      * @param message to log
141      * @see org.apache.commons.logging.Log#info(Object)
142      */
143     public void info(Object message) {
144         if (message != null) {
145             getLogger().info(String.valueOf(message));
146         }
147     }
148 
149 
150     /***
151      * Logs a message with <code>org.apache.log.Priority.INFO</code>.
152      * 
153      * @param message to log
154      * @param t log this cause
155      * @see org.apache.commons.logging.Log#info(Object, Throwable)
156      */
157     public void info(Object message, Throwable t) {
158         if (message != null) {
159             getLogger().info(String.valueOf(message), t);
160         }
161     }
162 
163 
164     /***
165      * Logs a message with <code>org.apache.log.Priority.WARN</code>.
166      * 
167      * @param message to log
168      * @see org.apache.commons.logging.Log#warn(Object)
169      */
170     public void warn(Object message) {
171         if (message != null) {
172             getLogger().warn(String.valueOf(message));
173         }
174     }
175 
176 
177     /***
178      * Logs a message with <code>org.apache.log.Priority.WARN</code>.
179      * 
180      * @param message to log
181      * @param t log this cause
182      * @see org.apache.commons.logging.Log#warn(Object, Throwable)
183      */
184     public void warn(Object message, Throwable t) {
185         if (message != null) {
186             getLogger().warn(String.valueOf(message), t);
187         }
188     }
189 
190 
191     /***
192      * Logs a message with <code>org.apache.log.Priority.ERROR</code>.
193      * 
194      * @param message to log
195      * @see org.apache.commons.logging.Log#error(Object)
196      */
197     public void error(Object message) {
198         if (message != null) {
199             getLogger().error(String.valueOf(message));
200         }
201     }
202 
203 
204     /***
205      * Logs a message with <code>org.apache.log.Priority.ERROR</code>.
206      * 
207      * @param message to log
208      * @param t log this cause
209      * @see org.apache.commons.logging.Log#error(Object, Throwable)
210      */
211     public void error(Object message, Throwable t) {
212         if (message != null) {
213             getLogger().error(String.valueOf(message), t);
214         }
215     }
216 
217 
218     /***
219      * Logs a message with <code>org.apache.log.Priority.FATAL_ERROR</code>.
220      * 
221      * @param message to log
222      * @see org.apache.commons.logging.Log#fatal(Object)
223      */
224     public void fatal(Object message) {
225         if (message != null) {
226             getLogger().fatalError(String.valueOf(message));
227         }
228     }
229 
230 
231     /***
232      * Logs a message with <code>org.apache.log.Priority.FATAL_ERROR</code>.
233      * 
234      * @param message to log
235      * @param t log this cause
236      * @see org.apache.commons.logging.Log#fatal(Object, Throwable)
237      */
238     public void fatal(Object message, Throwable t) {
239         if (message != null) {
240             getLogger().fatalError(String.valueOf(message), t);
241         }
242     }
243 
244 
245     /***
246      * Checks whether the <code>LogKit</code> logger will log messages of priority <code>DEBUG</code>.
247      */
248     public boolean isDebugEnabled() {
249         return getLogger().isDebugEnabled();
250     }
251 
252 
253     /***
254      * Checks whether the <code>LogKit</code> logger will log messages of priority <code>ERROR</code>.
255      */
256     public boolean isErrorEnabled() {
257         return getLogger().isErrorEnabled();
258     }
259 
260 
261     /***
262      * Checks whether the <code>LogKit</code> logger will log messages of priority <code>FATAL_ERROR</code>.
263      */
264     public boolean isFatalEnabled() {
265         return getLogger().isFatalErrorEnabled();
266     }
267 
268 
269     /***
270      * Checks whether the <code>LogKit</code> logger will log messages of priority <code>INFO</code>.
271      */
272     public boolean isInfoEnabled() {
273         return getLogger().isInfoEnabled();
274     }
275 
276 
277     /***
278      * Checks whether the <code>LogKit</code> logger will log messages of priority <code>DEBUG</code>.
279      */
280     public boolean isTraceEnabled() {
281         return getLogger().isDebugEnabled();
282     }
283 
284 
285     /***
286      * Checks whether the <code>LogKit</code> logger will log messages of priority <code>WARN</code>.
287      */
288     public boolean isWarnEnabled() {
289         return getLogger().isWarnEnabled();
290     }
291 
292 
293 }