KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > grlea > log > adapters > commons > CommonsLoggingAdapter


1 package org.grlea.log.adapters.commons;
2
3 // $Id: CommonsLoggingAdapter.java,v 1.3 2006/07/16 22:50:23 grlea Exp $
4
// Copyright (c) 2004-2006 Graham Lea. All rights reserved.
5

6 // Licensed under the Apache License, Version 2.0 (the "License");
7
// you may not use this file except in compliance with the License.
8
// You may obtain a copy of the License at
9
//
10
// http://www.apache.org/licenses/LICENSE-2.0
11
//
12
// Unless required by applicable law or agreed to in writing, software
13
// distributed under the License is distributed on an "AS IS" BASIS,
14
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
// See the License for the specific language governing permissions and
16
// limitations under the License.
17

18 import org.grlea.log.DebugLevel;
19 import org.grlea.log.SimpleLog;
20 import org.grlea.log.SimpleLogger;
21
22 import org.apache.commons.logging.Log;
23
24 /**
25  * <p>
26  * An adapter for using <a HREF="https:/simple-log.dev.java.net/">Simple Log</a> within the
27  * Jakarta <a HREF="http://jakarta.apache.org/commons/logging/">Commons Logging</a> package.
28  * </p>
29  *
30  * <p><b>Instructions for Use</b></p>
31  *
32  * <p>
33  * To use this <code>CommonsLoggingAdapter</code> with Commons Logging, simply
34  * {@link System#setProperty set the system property} '<code>org.apache.commons.logging.Log</code>'
35  * to '<code>org.grlea.log.adapters.commons.CommonsLoggingAdapter</code>'.
36  * </p>
37  *
38  * <p><b>Implementation Details</b></p>
39  *
40  * <p>
41  * The <code>CommonsLoggingAdapter</code> always uses the
42  * {@link SimpleLog#defaultInstance() default SimpleLog}. It is anticipated this will do the job for
43  * the preverbial 99% of cases. If this is insufficient for your needs, please write to me and we
44  * can try and find a more flexible solution.
45  * </p>
46  *
47  * <p>
48  * The Commons Logging levels are mapped to the Simple Log levels in this manner:
49  * <table border="1" style="border: 1px solid black;">
50  * <tr> <th>Commons Logging</th> <th>Simple Log</th> </tr>
51  * <tr> <td>{@link Log#fatal(Object) Fatal}</td> <td>{@link DebugLevel#L1_FATAL Fatal}</td> </tr>
52  * <tr> <td>{@link Log#error(Object) Error}</td> <td>{@link DebugLevel#L2_ERROR Error}</td> </tr>
53  * <tr> <td>{@link Log#warn(Object) Warn}</td> <td>{@link DebugLevel#L3_WARN Warn}</td> </tr>
54  * <tr> <td>{@link Log#info(Object) Info}</td> <td>{@link DebugLevel#L4_INFO Info}</td> </tr>
55  * <tr> <td>{@link Log#debug(Object) Debug}</td> <td>{@link DebugLevel#L5_DEBUG Debug}</td> </tr>
56  * <tr> <td><b>{@link Log#trace(Object) Trace}</b></td>
57  * <td><b>{@link DebugLevel#L6_VERBOSE Verbose}</b></td> </tr>
58  * </table>
59  * </p>
60  *
61  * <p><b>"Tracing"</b></p>
62  *
63  * <p>
64  * Note that the Commons Logging 'Trace' level is <b>not</b> matched to Simple
65  * Log's tracing facility (i.e. {@link SimpleLogger}'s {@link SimpleLogger#entry entry()} and
66  * {@link SimpleLogger#exit exit()} methdos), but to the {@link DebugLevel#L6_VERBOSE Verbose}
67  * level. This is because the Commons Logging API allows any kind of object or exception to be
68  * logged at the Trace level (rather than just method names), and provides no information
69  * regarding whether the log is for a method entry or exit.
70  * </p>
71  *
72  * <p><b>Logger Names</b></p>
73  *
74  * <p>
75  * The Commons Logging API provides logger names that are Strings, while Simple Log prefers Class
76  * objects for naming its loggers. This adapter tries to rectify the discord by attempting to
77  * interpret logger names as fully qualified class names. Where this is not possible (because the
78  * logger name is not a class name), a <code>SimpleLogger</code> will be created using a source
79  * class of <code>org.apache.commons.logging.Log</code> and with the provided logger name as the
80  * instance ID of the logger. (See {@link SimpleLogger#SimpleLogger(Class, Object)}. This means
81  * that, when writing properties to configure loggers that don't use class names, the properties
82  * should take the form:<pre>
83  * org.apache.commons.logging.Log.<i>&lt;logger-name&gt;</i></pre>
84  * </p>
85  *
86  * <p>
87  * When logger names cannot be interpreted as class names, each logger name that cannot be
88  * interpreted will be printed with a warning to {@link System#err}. You can supress these warnings
89  * by setting the system property <code>org.grlea.log.adapters.commons.supressWarnings</code> to
90  * <code>true</code>.
91  * </p>
92  *
93  * <p><b>Object Rendering</b></p>
94  *
95  * <p>
96  * Because Simple Log has a notion of logging message types (i.e. debug message, debug object and
97  * debug exception) but Commons Logging does not, the generic 'message' objects passed in to this
98  * adapter are passed on to Simple Log in this manner:
99  * <ul>
100  * <li>
101  * Any String passed in to either form of a logging method (e.g. {@link #fatal(Object)}
102  * or {@link #fatal(Object, Throwable)}) is passed onto the {@link SimpleLogger#db} method.
103  * </li>
104  * <li>
105  * Any other {@link Object} passed in as the 'message' argument is passed onto the
106  * {@link SimpleLogger#dbo} method as an object whose vaue is to be debugged, with the empty
107  * string as the object name.
108  * </li>
109  * <li>
110  * Throwables passed as the second argument in methods of the form
111  * {@link #fatal(Object, Throwable)} result in an additional call to the
112  * {@link SimpleLogger#dbe} method.
113  * </li>
114  * </ul>
115  * </p>
116  *
117  * @author Graham Lea
118  * @version $Revision: 1.3 $
119  */

120 public class
121 CommonsLoggingAdapter
122 implements Log
123 {
124    private static final String JavaDoc SUPRESS_WARNINGS_PROPERTY = "org.grlea.log.adapters.commons.supressWarnings";
125
126    private static SimpleLog log = null;
127
128    private final SimpleLogger logger;
129
130    public
131    CommonsLoggingAdapter(String JavaDoc loggerName)
132    {
133       initLog();
134
135       SimpleLogger newLogger;
136       try
137       {
138          Class JavaDoc loggingClass = Class.forName(loggerName);
139          newLogger = new SimpleLogger(loggingClass);
140       }
141       catch (Exception JavaDoc e)
142       {
143          boolean supressWarnings = false;
144          try
145          {
146             String JavaDoc supressWarningsString = System.getProperty(SUPRESS_WARNINGS_PROPERTY);
147             supressWarnings =
148                supressWarningsString != null && supressWarningsString.toLowerCase().equals("true");
149          }
150          catch (Exception JavaDoc e1)
151          {
152             System.err.println("WARNING: Simple Log (CommonsLoggingAdapter): " +
153                                "Failed to read system property '" + SUPRESS_WARNINGS_PROPERTY + "'");
154          }
155
156          if (!supressWarnings)
157          {
158             System.err.println("WARNING: Simple Log (CommonsLoggingAdapter): " +
159                                "Failed to find class for logger name '" + loggerName + "'. " +
160                                "Using class '" + Log.class.getName() + "' and instanceId '" +
161                                loggerName +"'.");
162          }
163
164          newLogger = new SimpleLogger(Log.class, loggerName);
165       }
166
167       this.logger = newLogger;
168    }
169
170    private static void
171    initLog()
172    {
173       if (log == null)
174       {
175          setLog(SimpleLog.defaultInstance());
176       }
177    }
178
179    public static void
180    setLog(SimpleLog simpleLog)
181    {
182       log = simpleLog;
183    }
184
185    public boolean
186    isFatalEnabled()
187    {
188       return logger.wouldLog(DebugLevel.L1_FATAL);
189    }
190
191    public boolean
192    isErrorEnabled()
193    {
194       return logger.wouldLog(DebugLevel.L2_ERROR);
195    }
196
197    public boolean
198    isWarnEnabled()
199    {
200       return logger.wouldLog(DebugLevel.L3_WARN);
201    }
202
203    public boolean
204    isInfoEnabled()
205    {
206       return logger.wouldLog(DebugLevel.L4_INFO);
207    }
208
209    public boolean
210    isDebugEnabled()
211    {
212       return logger.wouldLog(DebugLevel.L5_DEBUG);
213    }
214
215    public boolean
216    isTraceEnabled()
217    {
218       return logger.isTracing();
219    }
220
221    public void
222    fatal(Object JavaDoc message)
223    {
224       if (logger.wouldLog(DebugLevel.L1_FATAL))
225       {
226          log(DebugLevel.L1_FATAL, message);
227       }
228    }
229
230    public void
231    fatal(Object JavaDoc message, Throwable JavaDoc t)
232    {
233       if (logger.wouldLog(DebugLevel.L1_FATAL))
234       {
235          log(DebugLevel.L1_FATAL, message);
236          logger.dbe(DebugLevel.L1_FATAL, t);
237       }
238    }
239
240    public void
241    error(Object JavaDoc message)
242    {
243       if (logger.wouldLog(DebugLevel.L2_ERROR))
244       {
245          log(DebugLevel.L2_ERROR, message);
246       }
247    }
248
249    public void
250    error(Object JavaDoc message, Throwable JavaDoc t)
251    {
252       if (logger.wouldLog(DebugLevel.L2_ERROR))
253       {
254          log(DebugLevel.L2_ERROR, message);
255          logger.dbe(DebugLevel.L2_ERROR, t);
256       }
257    }
258
259    public void
260    warn(Object JavaDoc message)
261    {
262       if (logger.wouldLog(DebugLevel.L3_WARN))
263       {
264          log(DebugLevel.L3_WARN, message);
265       }
266    }
267
268    public void
269    warn(Object JavaDoc message, Throwable JavaDoc t)
270    {
271       if (logger.wouldLog(DebugLevel.L3_WARN))
272       {
273          log(DebugLevel.L3_WARN, message);
274          logger.dbe(DebugLevel.L3_WARN, t);
275       }
276    }
277
278    public void
279    info(Object JavaDoc message)
280    {
281       if (logger.wouldLog(DebugLevel.L4_INFO))
282       {
283          log(DebugLevel.L4_INFO, message);
284       }
285    }
286
287    public void
288    info(Object JavaDoc message, Throwable JavaDoc t)
289    {
290       if (logger.wouldLog(DebugLevel.L4_INFO))
291       {
292          log(DebugLevel.L4_INFO, message);
293          logger.dbe(DebugLevel.L4_INFO, t);
294       }
295    }
296
297    public void
298    debug(Object JavaDoc message)
299    {
300       if (logger.wouldLog(DebugLevel.L5_DEBUG))
301       {
302          log(DebugLevel.L5_DEBUG, message);
303       }
304    }
305
306    public void
307    debug(Object JavaDoc message, Throwable JavaDoc t)
308    {
309       if (logger.wouldLog(DebugLevel.L5_DEBUG))
310       {
311          log(DebugLevel.L5_DEBUG, message);
312          logger.dbe(DebugLevel.L5_DEBUG, t);
313       }
314    }
315
316    public void
317    trace(Object JavaDoc message)
318    {
319       if (logger.wouldLog(DebugLevel.L6_VERBOSE))
320       {
321          log(DebugLevel.L6_VERBOSE, message);
322       }
323    }
324
325    public void
326    trace(Object JavaDoc message, Throwable JavaDoc t)
327    {
328       if (logger.wouldLog(DebugLevel.L6_VERBOSE))
329       {
330          log(DebugLevel.L6_VERBOSE, message);
331          logger.dbe(DebugLevel.L6_VERBOSE, t);
332       }
333    }
334
335    private void
336    log(DebugLevel level, Object JavaDoc message)
337    {
338       if (message instanceof String JavaDoc)
339          logger.db(level, (String JavaDoc) message);
340       else
341          logger.dbo(level, "", message);
342    }
343 }
Popular Tags