KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > grlea > log > adapters > slf4j > Slf4jAdapter


1 package org.grlea.log.adapters.slf4j;
2
3 // $Id: Slf4jAdapter.java,v 1.4 2006/07/13 12:44:57 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.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24 import org.slf4j.impl.MarkerIgnoringBase;
25 import org.slf4j.impl.MessageFormatter;
26
27 /**
28  * <p>
29  * An adapter for using <a HREF="https:/simple-log.dev.java.net/">Simple Log</a> within the
30  * <a HREF="http://www.slf4j.org/">SLF4J</a> package.
31  * </p>
32  *
33  * <p><b>Instructions for Use</b></p>
34  *
35  * <p>
36  * To use this <code>Slf4jAdapter</code> with SLF4J, you should only need to include the JARs for
37  * Simple Log (simple-log.jar) and for the SLF4J Adapter (simple-log-slf4j.jar) in your classpath
38  * and use the SLF4J {@link LoggerFactory} to obtain log instances. <b>Be careful</b> not to confuse
39  * 'slf4j-simple.jar' as being part of the arrangement. It is a SLF4J deliverable that simply prints
40  * output to <code>System.out</code>.
41  * </p>
42  *
43  * <p><b>Implementation Details</b></p>
44  *
45  * <p>
46  * The <code>Slf4jAdapter</code> always uses the
47  * {@link SimpleLog#defaultInstance() default SimpleLog}. It is anticipated this will do the job for
48  * the preverbial 99% of cases. If this is insufficient for your needs, please write to me and we
49  * can try and find a more flexible solution.
50  * </p>
51  *
52  * <p>
53  * The SLF4J levels map directly to the Simple Log levels:
54  * <table border="1" style="border: 1px solid black;">
55  * <tr> <th>SLF4J</th> <th>Simple Log</th> </tr>
56  * <tr> <td>{@link Logger#error(String) Error}</td> <td>{@link DebugLevel#L2_ERROR Error}</td> </tr>
57  * <tr> <td>{@link Logger#warn(String) Warn}</td> <td>{@link DebugLevel#L3_WARN Warn}</td> </tr>
58  * <tr> <td>{@link Logger#info(String) Info}</td> <td>{@link DebugLevel#L4_INFO Info}</td> </tr>
59  * <tr> <td>{@link Logger#debug(String) Debug}</td> <td>{@link DebugLevel#L5_DEBUG Debug}</td> </tr>
60  * </table>
61  * </p>
62  *
63  * <p><b>"Tracing"</b></p>
64  *
65  * <p>
66  * Note that the SLF4J has no notion of tracing whatsoever, so the tracing properties in Simple Log
67  * will have no bearing on the output.
68  * </p>
69  *
70  * <p><b>Logger Names</b></p>
71  *
72  * <p>
73  * The SLF4J API provides logger names that are Strings, while Simple Log prefers Class
74  * objects for naming its loggers. This adapter tries to rectify the discord by attempting to
75  * interpret logger names as fully qualified class names.
76  * Where this is not possible (because the logger name is not a class name), a
77  * <code>SimpleLogger</code> will be created using a source class of
78  * <code>org.slf4j.Logger</code> and with the provided logger name as the instance ID of the logger.
79  * (See {@link SimpleLogger#SimpleLogger(Class, Object)}.
80  * When the two-argument domain + sub-domain method is used and the domain is not a fully-qualified
81  * class name, the domain and sub-domain are concatenated with a period ('.') in between, e.g.
82  * <code>"domain.subdomain"</code>, and this is used as the instance ID.
83  * This means that, when writing properties to configure loggers that don't use class names, the
84  * properties should take the form:<pre>
85  * org.slf4j.Logger.<i>&lt;logger-name&gt;</i></pre>
86  * or<pre>
87  * org.slf4j.Logger.<i>&lt;domain&gt;.&lt;subdomain&gt;</i></pre>
88  * </p>
89  *
90  * <p>
91  * When logger names cannot be interpreted as class names, each logger name that cannot be
92  * interpreted will be printed with a warning to {@link System#err}. You can supress these warnings
93  * by setting the system property <code>org.grlea.log.adapters.slf4j.supressWarnings</code> to
94  * <code>true</code>.
95  * </p>
96  *
97  * <p><b>Object Rendering</b></p>
98  *
99  * <p>
100  * Messages submitted to the <code>Slf4jAdapter</code> using
101  * {@link #debug(String, Object) the format + argument(s) methods} are interpreted using the
102  * {@link MessageFormatter} that is provided in SLF4J.
103  * </p>
104  *
105  * @author Graham Lea
106  * @version $Revision: 1.4 $
107  */

108 public class
109 Slf4jAdapter
110 extends MarkerIgnoringBase
111 {
112    /** The logger this <code>Slf4jAdapter</code> will output to. */
113    private final SimpleLogger log;
114
115    /** The name of this logger. */
116    private final String JavaDoc loggerName;
117
118    /**
119     * Creates a new <code>Slf4jAdapter</code> that will output to the provided {@link SimpleLogger}.
120     *
121     * @param log the logger this <code>Slf4jAdapter</code> will output to.
122     *
123     * @param loggerName the name of this logger
124     */

125    Slf4jAdapter(SimpleLogger log, String JavaDoc loggerName)
126    {
127       this.log = log;
128       this.loggerName = loggerName;
129    }
130
131    public String JavaDoc
132    getName()
133    {
134       return loggerName;
135    }
136
137    public boolean
138    isErrorEnabled()
139    {
140       return log.wouldLog(DebugLevel.L2_ERROR);
141    }
142
143    public boolean
144    isWarnEnabled()
145    {
146       return log.wouldLog(DebugLevel.L3_WARN);
147    }
148
149    public boolean
150    isInfoEnabled()
151    {
152       return log.wouldLog(DebugLevel.L4_INFO);
153    }
154
155    public boolean
156    isDebugEnabled()
157    {
158       return log.wouldLog(DebugLevel.L5_DEBUG);
159    }
160
161    public void
162    error(String JavaDoc message)
163    {
164       log.db(DebugLevel.L2_ERROR, message);
165    }
166
167    public void
168    error(String JavaDoc messageFormat, Object JavaDoc argument)
169    {
170       if (log.wouldLog(DebugLevel.L2_ERROR))
171       {
172          log.db(DebugLevel.L2_ERROR, MessageFormatter.format(messageFormat, argument));
173       }
174    }
175
176    public void
177    error(String JavaDoc messageFormat, Object JavaDoc argument1, Object JavaDoc argument2)
178    {
179       if (log.wouldLog(DebugLevel.L2_ERROR))
180       {
181          log.db(DebugLevel.L2_ERROR, MessageFormatter.format(messageFormat, argument1, argument2));
182       }
183    }
184
185    public void
186    error(String JavaDoc messageFormat, Object JavaDoc[] arguments)
187    {
188       if (log.wouldLog(DebugLevel.L2_ERROR))
189       {
190          log.db(DebugLevel.L2_ERROR, MessageFormatter.format(messageFormat, arguments));
191       }
192    }
193
194    public void
195    error(String JavaDoc message, Throwable JavaDoc exception)
196    {
197       log.db(DebugLevel.L2_ERROR, message);
198       log.dbe(DebugLevel.L2_ERROR, exception);
199    }
200
201    public void
202    warn(String JavaDoc message)
203    {
204       log.db(DebugLevel.L3_WARN, message);
205    }
206
207    public void
208    warn(String JavaDoc messageFormat, Object JavaDoc argument)
209    {
210       if (log.wouldLog(DebugLevel.L3_WARN))
211       {
212          log.db(DebugLevel.L3_WARN, MessageFormatter.format(messageFormat, argument));
213       }
214    }
215
216    public void
217    warn(String JavaDoc messageFormat, Object JavaDoc argument1, Object JavaDoc argument2)
218    {
219       if (log.wouldLog(DebugLevel.L3_WARN))
220       {
221          log.db(DebugLevel.L3_WARN, MessageFormatter.format(messageFormat, argument1, argument2));
222       }
223    }
224
225    public void
226    warn(String JavaDoc messageFormat, Object JavaDoc[] arguments)
227    {
228       if (log.wouldLog(DebugLevel.L3_WARN))
229       {
230          log.db(DebugLevel.L3_WARN, MessageFormatter.format(messageFormat, arguments));
231       }
232    }
233
234    public void
235    warn(String JavaDoc message, Throwable JavaDoc exception)
236    {
237       log.db(DebugLevel.L3_WARN, message);
238       log.dbe(DebugLevel.L3_WARN, exception);
239    }
240
241    public void
242    info(String JavaDoc message)
243    {
244       log.db(DebugLevel.L4_INFO, message);
245    }
246
247    public void
248    info(String JavaDoc messageFormat, Object JavaDoc argument)
249    {
250       if (log.wouldLog(DebugLevel.L4_INFO))
251       {
252          log.db(DebugLevel.L4_INFO, MessageFormatter.format(messageFormat, argument));
253       }
254    }
255
256    public void
257    info(String JavaDoc messageFormat, Object JavaDoc argument1, Object JavaDoc argument2)
258    {
259       if (log.wouldLog(DebugLevel.L4_INFO))
260       {
261          log.db(DebugLevel.L4_INFO, MessageFormatter.format(messageFormat, argument1, argument2));
262       }
263    }
264
265    public void
266    info(String JavaDoc messageFormat, Object JavaDoc[] arguments)
267    {
268       if (log.wouldLog(DebugLevel.L4_INFO))
269       {
270          log.db(DebugLevel.L4_INFO, MessageFormatter.format(messageFormat, arguments));
271       }
272    }
273
274    public void
275    info(String JavaDoc message, Throwable JavaDoc exception)
276    {
277       log.db(DebugLevel.L4_INFO, message);
278       log.dbe(DebugLevel.L4_INFO, exception);
279    }
280
281    public void
282    debug(String JavaDoc message)
283    {
284       log.db(DebugLevel.L5_DEBUG, message);
285    }
286
287    public void
288    debug(String JavaDoc messageFormat, Object JavaDoc argument)
289    {
290       if (log.wouldLog(DebugLevel.L5_DEBUG))
291       {
292          log.db(DebugLevel.L5_DEBUG, MessageFormatter.format(messageFormat, argument));
293       }
294    }
295
296    public void
297    debug(String JavaDoc messageFormat, Object JavaDoc argument1, Object JavaDoc argument2)
298    {
299       if (log.wouldLog(DebugLevel.L5_DEBUG))
300       {
301          log.db(DebugLevel.L5_DEBUG, MessageFormatter.format(messageFormat, argument1, argument2));
302       }
303    }
304
305    public void
306    debug(String JavaDoc messageFormat, Object JavaDoc[] arguments)
307    {
308       if (log.wouldLog(DebugLevel.L5_DEBUG))
309       {
310          log.db(DebugLevel.L5_DEBUG, MessageFormatter.format(messageFormat, arguments));
311       }
312    }
313
314    public void
315    debug(String JavaDoc message, Throwable JavaDoc exception)
316    {
317       log.db(DebugLevel.L5_DEBUG, message);
318       log.dbe(DebugLevel.L5_DEBUG, exception);
319    }
320 }
Popular Tags