KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > util > LoggingUtil


1 package org.objectweb.petals.util;
2
3 import org.objectweb.util.monolog.api.BasicLevel;
4 import org.objectweb.util.monolog.api.Logger;
5
6 /**
7  * This class is used to format various logs. it can be used as a static class
8  * or as a wrapper object of a logger. The methods add to the resulting message
9  * a "ClassName-MethodName" information. (only for DEBUG and INFO level)
10  *
11  * @author alouis
12  *
13  */

14 public class LoggingUtil {
15
16     private Logger logger;
17
18     public LoggingUtil(Logger logger) {
19         this.logger = logger;
20     }
21
22     private LoggingUtil() {
23     }
24
25     /**
26      * if log is enabled, Log as <i>DEBUG</i> : [CALL ]
27      *
28      * @param log
29      */

30     public static void call(Logger log) {
31         if (log != null && log.isOn()) {
32             log.log(BasicLevel.LEVEL_DEBUG, "[CALL ] " + classAndMethod());
33         }
34     }
35
36     /**
37      * if log is enabled, Log as <i>DEBUG</i> : [CALL ]
38      *
39      * @param log
40      * @param msg
41      */

42     public static void call(Logger log, Object JavaDoc msg) {
43         if (log != null && log.isOn()) {
44             log.log(BasicLevel.LEVEL_DEBUG, "[CALL ] " + classAndMethod() + " "
45                 + msg);
46         }
47     }
48
49     /**
50      * if log is enabled, Log as <i>DEBUG</i> the message
51      *
52      * @param log
53      * @param msg
54      */

55     public static void debug(Logger log, Object JavaDoc message) {
56         if (log != null && log.isOn()) {
57             log.log(BasicLevel.LEVEL_DEBUG, classAndMethod() + " " + message);
58         }
59     }
60
61     /**
62      * if log is enabled, Log as <i>DEBUG</i> : [ END ]
63      *
64      * @param log
65      */

66     public static void end(Logger log) {
67         if (log != null && log.isOn()) {
68             log.log(BasicLevel.LEVEL_DEBUG, "[ END ] " + classAndMethod());
69         }
70     }
71
72     /**
73      * if log is enabled, Log as <i>DEBUG</i> the message : [ END ]msg
74      *
75      * @param log
76      * @param msg
77      */

78     public static void end(Logger log, Object JavaDoc msg) {
79         if (log != null && log.isOn()) {
80             log.log(BasicLevel.LEVEL_DEBUG, "[ END ] " + classAndMethod() + " "
81                 + msg);
82         }
83     }
84
85     /**
86      * if log is enabled, Log as <i>ERROR</i> the message
87      *
88      * @param log
89      * @param msg
90      */

91     public static void error(Logger log, Object JavaDoc message) {
92         if (log != null && log.isOn()) {
93             log.log(BasicLevel.LEVEL_ERROR, message);
94         }
95     }
96
97     /**
98      * if log is enabled, Log as <i>ERROR</i> the exception
99      *
100      * @param log
101      * @param msg
102      * @param throwable
103      */

104     public static void error(Logger log, Object JavaDoc message, Throwable JavaDoc throwable) {
105         if (log != null && log.isOn()) {
106             log.log(BasicLevel.LEVEL_ERROR, message, throwable);
107         }
108     }
109
110     /**
111      * if log is enabled, Log as <i>INFO</i> the message
112      *
113      * @param log
114      * @param msg
115      */

116     public static void info(Logger log, Object JavaDoc message) {
117         if (log != null && log.isOn()) {
118             log.log(BasicLevel.LEVEL_INFO, classAndMethod() + " " + message);
119         }
120     }
121
122     /**
123      * if log is enabled, Log as <i>INFO</i> the message and error
124      *
125      * @param log
126      * @param msg
127      * @param error
128      */

129     public static void info(Logger log, Object JavaDoc message, Throwable JavaDoc error) {
130         if (log != null && log.isOn()) {
131             log.log(BasicLevel.LEVEL_INFO, classAndMethod() + " " + message,
132                 error);
133         }
134     }
135
136     /**
137      * if log is enabled, Log as <i>DEBUG</i> : [START]
138      *
139      * @param log
140      */

141     public static void start(Logger log) {
142         if (log != null && log.isOn()) {
143             log.log(BasicLevel.LEVEL_DEBUG, "[START] " + classAndMethod());
144         }
145     }
146
147     /**
148      * if log is enabled, Log as <i>DEBUG</i> the message : [START]msg
149      *
150      * @param log
151      * @param msg
152      */

153     public static void start(Logger log, Object JavaDoc msg) {
154         if (log != null && log.isOn()) {
155             log.log(BasicLevel.LEVEL_DEBUG, "[START] " + classAndMethod() + " "
156                 + msg);
157         }
158     }
159
160     /**
161      * if log is enabled, Log as warning the message
162      *
163      * @param log
164      * @param msg
165      */

166     public static void warning(Logger log, Object JavaDoc message) {
167         if (log != null && log.isOn()) {
168             log.log(BasicLevel.LEVEL_WARN, message);
169         }
170     }
171
172     /**
173      * if log is enabled, Log as warning the exception
174      *
175      * @param log
176      * @param msg
177      * @param throwable
178      */

179     public static void warning(Logger log, Object JavaDoc message, Throwable JavaDoc throwable) {
180         if (log != null && log.isOn()) {
181             log.log(BasicLevel.LEVEL_WARN, message, throwable);
182         }
183     }
184
185     // //////////////////////////////////////////////////////////
186
// Static part
187
// //////////////////////////////////////////////////////////
188

189     /**
190      * Create an exception and analyse it in order to find the class and method
191      * that called the LoggingUtil method.
192      *
193      * @return
194      */

195     private static String JavaDoc classAndMethod() {
196         String JavaDoc result = null;
197
198         // throw and Parse an exception to find in the stack
199
// the method that called the Loggingutil
200

201         Throwable JavaDoc t = new Throwable JavaDoc().fillInStackTrace();
202
203         StackTraceElement JavaDoc[] ste = t.getStackTrace();
204
205         if (ste != null && ste.length > 2) {
206             StackTraceElement JavaDoc element = ste[2];
207
208             // If the 2nd element in the stack is this class, get the 3rd
209
if (element.getClassName().endsWith(LoggingUtil.class.getName())) {
210                 element = ste[3];
211             }
212             String JavaDoc className = element.getClassName();
213
214             // remove the package name of the ClassName
215
int index = className.lastIndexOf(".");
216
217             if (index > -1) {
218                 className = className.substring(index + 1, className.length());
219             }
220
221             result = className + "." + element.getMethodName() + "()";
222         }
223         return result;
224     }
225
226     /**
227      * if log is enabled, Log as <i>DEBUG</i> : [CALL ]
228      */

229     public void call() {
230         call(logger);
231     }
232
233     /**
234      * if log is enabled, Log as <i>DEBUG</i> the message : [CALL ]msg
235      *
236      * @param msg
237      */

238     public void call(Object JavaDoc msg) {
239         call(logger, msg);
240     }
241
242     /**
243      * if log is enabled, Log as <i>DEBUG</i> the message
244      *
245      * @param msg
246      */

247     public void debug(Object JavaDoc message) {
248         debug(logger, message);
249     }
250
251     /**
252      * if log is enabled, Log as <i>DEBUG</i> : [ END ]
253      */

254     public void end() {
255         end(logger);
256     }
257
258     /**
259      * if log is enabled, Log as <i>DEBUG</i> the message : [ END ]msg
260      *
261      * @param msg
262      */

263     public void end(Object JavaDoc msg) {
264         end(logger, msg);
265     }
266
267     /**
268      * if log is enabled, Log as <i>ERROR</i> the message
269      *
270      * @param msg
271      */

272     public void error(Object JavaDoc message) {
273         error(logger, message);
274     }
275
276     /**
277      * if log is enabled, Log as <i>ERROR</i> the exception
278      *
279      * @param msg
280      * @param throwable
281      */

282     public void error(Object JavaDoc message, Throwable JavaDoc throwable) {
283         error(logger, message, throwable);
284     }
285
286     public String JavaDoc getName() {
287         return this.logger.getName();
288     }
289
290     /**
291      * if log is enabled, Log as <i>INFO</i> the message
292      *
293      * @param msg
294      */

295     public void info(Object JavaDoc message) {
296         info(logger, message);
297     }
298
299     /**
300      * if log is enabled, Log as <i>INFO</i> the message and error
301      *
302      * @param msg
303      * @param error
304      */

305     public void info(Object JavaDoc message, Throwable JavaDoc error) {
306         info(logger, message, error);
307     }
308
309     /**
310      * if log is enabled, Log as <i>DEBUG</i> : [START]
311      */

312     public void start() {
313         start(logger);
314     }
315
316     /**
317      * if log is enabled, Log as <i>DEBUG</i> the message : [START]msg
318      *
319      * @param msg
320      */

321     public void start(Object JavaDoc msg) {
322         start(logger, msg);
323     }
324
325     /**
326      * if log is enabled, Log as warning the message
327      *
328      * @param msg
329      */

330     public void warning(Object JavaDoc message) {
331         warning(logger, message);
332     }
333
334     /**
335      * if log is enabled, Log as warning the exception
336      *
337      * @param msg
338      * @param throwable
339      */

340     public void warning(Object JavaDoc message, Throwable JavaDoc throwable) {
341         warning(logger, message, throwable);
342     }
343 }
344
Popular Tags