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