KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > idaremedia > antx > feedback > LJDiagnosticsEmitter


1 /**
2  * $Id: LJDiagnosticsEmitter.java 180 2007-03-15 12:56:38Z ssmc $
3  * Copyright 2002-2003 iDare Media, Inc. All rights reserved.
4  *
5  * Originally written by iDare Media, Inc. for release into the public domain. This
6  * library, source form and binary form, is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License as published by the
8  * Free Software Foundation; either version 2.1 of the License, or (at your option) any
9  * later version.<p>
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU LGPL (GNU Lesser General Public License) for more details.<p>
14  *
15  * You should have received a copy of the GNU Lesser General Public License along with this
16  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite
17  * 330, Boston, MA 02111-1307 USA. The LGPL can be found online at
18  * http://www.fsf.org/copyleft/lesser.html
19  *
20  * This product has been influenced by several projects within the open-source community.
21  * The JWare developers wish to acknowledge the open-source community's support. For more
22  * information regarding the open-source products used within JWare, please visit the
23  * JWare website.
24  *----------------------------------------------------------------------------------------*
25  * WEBSITE- http://www.jware.info EMAIL- inquiries@jware.info
26  *----------------------------------------------------------------------------------------*
27  **/

28
29 package com.idaremedia.antx.feedback;
30
31 import org.apache.log4j.Level;
32 import org.apache.log4j.Logger;
33 import org.apache.log4j.LogManager;
34 import org.apache.log4j.spi.LoggerFactory;
35
36 import com.idaremedia.apis.DiagnosticsEmitter;
37 import com.idaremedia.apis.DiagnosticsEmitterFactory;
38
39 import com.idaremedia.antx.NoiseLevel;
40
41 /**
42  * Implementation of a {@linkplain DiagnosticsEmitter DiagnosticsEmitter} that uses a
43  * <a HREF="http://jakarta.apache.org/log4j">log4j</a> category as the underlying
44  * output-generating mechanism.
45  *
46  * @since JWare/AntX 0.1
47  * @author ssmc, &copy;2002-2003 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
48  * @version 0.5
49  * @.safety guarded
50  * @.group impl,infra
51  **/

52
53 public class LJDiagnosticsEmitter implements DiagnosticsEmitter
54 {
55     /**
56      * Creates a new root diagnostics emitter. This emitter uses
57      * the LogManager's root logger.
58      **/

59     public LJDiagnosticsEmitter()
60     {
61         m_logger = LogManager.getRootLogger();
62     }
63
64
65     /**
66      * Creates a new diagnostics emitter for an existing Log4j logger.
67      * @param logger the underlying logger (non-null)
68      **/

69     public LJDiagnosticsEmitter(Logger logger)
70     {
71         if (logger==null) {
72             throw new IllegalArgumentException JavaDoc();
73         }
74         m_logger = logger;
75     }
76
77
78     /**
79      * Creates a new diagnostics emitter for a particular grouping.
80      * @param grpId this emitters log4j category (non-null)
81      **/

82     public LJDiagnosticsEmitter(String JavaDoc grpId)
83     {
84         if (grpId==null) {
85             throw new IllegalArgumentException JavaDoc();
86         }
87         m_logger = LogManager.getLogger(grpId);
88     }
89
90
91     /**
92      * Creates a new diagnostics emitter for a particular grouping
93      * using a special Logger factory to create the underlying Logger.
94      * @param lf the Logger factory (non-null)
95      * @param grpId the target class (non-null)
96      **/

97     public LJDiagnosticsEmitter(LoggerFactory lf, String JavaDoc grpId)
98     {
99         if (grpId==null || lf==null) {
100             throw new IllegalArgumentException JavaDoc();
101         }
102         m_logger = LogManager.getLogger(grpId, lf);
103     }
104
105
106
107     /**
108      * Converts an Antisque msg-level to its log4j equivalent.
109      **/

110     public static final Level levelFrom(int nl, Level dflt)
111     {
112         switch (nl) {
113             case NoiseLevel.FATAL_INDEX:
114                 return Level.FATAL;
115             case NoiseLevel.ERROR_INDEX:
116                 return Level.ERROR;
117             case NoiseLevel.WARNING_INDEX:
118                 return Level.WARN;
119             case NoiseLevel.INFO_INDEX:
120                 return Level.INFO;
121             case NoiseLevel.VERBOSE_INDEX:
122                 return Level.INFO;
123             case NoiseLevel.DEBUG_INDEX:
124                 return Level.DEBUG;
125             default:
126                 return dflt;
127         }
128     }
129
130
131     /**
132      * Determines if this emitter will ignore requests at given
133      * noise level.
134      **/

135     public boolean emits(int level)
136     {
137         return m_logger.isEnabledFor(levelFrom(level,Level.INFO));
138     }
139
140
141     /**
142      * Determines if this emitter will ignore requests at given
143      * Log4J Level.
144      **/

145     public boolean emits(Level w)
146     {
147         return m_logger.isEnabledFor(w);
148     }
149
150
151     /**
152      * Returns the underlying Logger's category name.
153      * @since JWare/AntX 0.3b4
154      **/

155     public String JavaDoc identifier()
156     {
157         return m_logger.getName();
158     }
159
160
161
162     /**
163      * Records a user-categorized message and an optional call
164      * stack.
165      * @since JWare/AntX 0.3b4
166      **/

167     public void report(int nl, Object JavaDoc msg, Throwable JavaDoc t)
168     {
169         m_logger.log(FQCN,levelFrom(nl,Level.INFO),msg,t);
170     }
171
172
173
174     /**
175      * Records an unrecoverable failure has occured.
176      **/

177     public void failure(Object JavaDoc msg)
178     {
179         m_logger.log(FQCN,Level.FATAL,msg,null);
180     }
181
182
183     /**
184      * Records an unrecoverable failure has occured including the
185      * stack trace of given exception.
186      **/

187     public void failure(Throwable JavaDoc t, Object JavaDoc msg)
188     {
189         m_logger.log(FQCN,Level.FATAL,msg,t);
190     }
191
192
193
194
195     /**
196      * Records an unexpected problem condition has occured.
197      **/

198     public void error(Object JavaDoc msg)
199     {
200         m_logger.log(FQCN,Level.ERROR,msg,null);
201     }
202
203
204     /**
205      * Records an unexpected problem condition has occured including
206      * the call stack of given exception.
207      **/

208     public void error(Throwable JavaDoc t, Object JavaDoc msg)
209     {
210         m_logger.log(FQCN,Level.ERROR,msg,t);
211     }
212
213
214
215     /**
216      * Records an atypical but locally recoverable problem has occured.
217      **/

218     public void warning(Object JavaDoc msg)
219     {
220         m_logger.log(FQCN,Level.WARN,msg,null);
221     }
222
223
224     /**
225      * Records an atypical but locally recoverable problem has occured
226      * including the given stack trace informatin.
227      **/

228     public void warning(Throwable JavaDoc t, Object JavaDoc msg)
229     {
230         m_logger.log(FQCN,Level.WARN,msg,t);
231     }
232
233
234
235     /**
236      * Records an alert notification. This level is not natively supported
237      * by log4j; for now mapped to {@linkplain #warning(Object) warning}.
238      **/

239     public void alert(Object JavaDoc msg)
240     {
241         m_logger.log(FQCN,Level.WARN,msg,null);
242     }
243
244
245     /**
246      * Records an alert notification including stack trace of
247      * given exception. This level is not natively supported by log4j; for
248      * now mapped to {@linkplain #warning(Throwable,Object) warning}.
249      **/

250     public void alert(Throwable JavaDoc t, Object JavaDoc msg)
251     {
252         m_logger.log(FQCN,Level.WARN,msg,t);
253     }
254
255
256     /**
257      * Records a status or progress information message.
258      **/

259     public void note(Object JavaDoc msg)
260     {
261         m_logger.log(FQCN,Level.INFO,msg,null);
262     }
263
264
265
266     /**
267      * Records an internal diagnostics message.
268      **/

269     public void trace(Object JavaDoc msg)
270     {
271         m_logger.log(FQCN,Level.DEBUG,msg,null);
272     }
273
274
275     /**
276      * Records an internal diagnostics message including stack
277      * trace of given exception.
278      **/

279     public void trace(Throwable JavaDoc t, Object JavaDoc msg)
280     {
281         m_logger.log(FQCN,Level.DEBUG,msg,t);
282     }
283
284
285     /**
286      * Records a very fine-grained trace message. For AntX,
287      * same as a {@linkplain #trace(Object) trace}.
288      **/

289     public final void finetrace(Object JavaDoc msg)
290     {
291         trace(msg);
292     }
293
294
295     /**
296      * Records a very fine-grained trace message including
297      * stack trace of given exception. For AntX, same as
298      * {@linkplain #trace(Throwable,Object) trace}.
299      **/

300     public final void finetrace(Throwable JavaDoc t, Object JavaDoc msg)
301     {
302         trace(t,msg);
303     }
304
305
306
307     /**
308      * Returns the underlying logger for this emitter object.
309      * Never returns <i>null</i>.
310      **/

311     public final Logger getLogger()
312     {
313         return m_logger;
314     }
315
316
317     /**
318      * Returns the fully-qualified-class-name of this emitter's
319      * wrapper class (or if no wrapper, this class's name).
320      **/

321     public final String JavaDoc getFQCN()
322     {
323         return FQCN;
324     }
325
326
327     /**
328      * Initializes the fully-qualified-class-name of this emitter's
329      * wrapper class. Should be done once at (wrapper's) construction time.
330      * @param fqcn the class name (non-null)
331      **/

332     public final void setFQCN(String JavaDoc fqcn)
333     {
334         if (fqcn==null) {
335             throw new IllegalArgumentException JavaDoc();
336         }
337         FQCN = fqcn;;
338     }
339
340
341     /**
342      * Builtin factory for instance of LJDiagnostisEmitters.
343      **/

344     public static final DiagnosticsEmitterFactory FACTORY=
345         new DiagnosticsEmitterFactory() {
346                 public DiagnosticsEmitter newEmitter() {
347                     return new LJDiagnosticsEmitter();
348                 }
349                 public DiagnosticsEmitter newEmitter(String JavaDoc grpId) {
350                     return new LJDiagnosticsEmitter(grpId);
351                 }
352             };
353
354
355     /** The underlying log4j logger stuff. **/
356     private final Logger m_logger;
357     private String JavaDoc FQCN = LJDiagnosticsEmitter.class.getName();
358 }
359
360 /* end-of-LJDiagnosticsEmitter.java */
361
Popular Tags