KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jdo > spi > persistence > utility > logging > LoggerJDK14


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.jdo.spi.persistence.utility.logging;
25
26 import java.util.logging.Level JavaDoc;
27 import java.util.logging.LogManager JavaDoc;
28
29 import java.lang.StringBuffer JavaDoc;
30
31 /**
32  * This class is used with JDK 1.4 (and higher) programs to log messages from
33  * jdo components. It extends a java.util.logging.Logger which does
34  * the actual logging.
35  *
36  * @author Craig Russell
37  * @version 1.0
38  */

39 public class LoggerJDK14 extends java.util.logging.Logger JavaDoc implements Logger {
40
41     /** Class that issued logging call; set by inferCaller. */
42     protected String JavaDoc sourceClassName;
43
44     /** Method that issued logging call; set by inferCaller. */
45     protected String JavaDoc sourceMethodName;
46
47     /** Creates new LoggerJDK14. The Thread context class loader or the
48      * loader which loaded this class must be able to load the bundle.
49      * @param loggerName the full domain name of this logger
50      * @param bundleName the bundle name for message translation
51      */

52     public LoggerJDK14(String JavaDoc loggerName, String JavaDoc bundleName) {
53         super (loggerName, bundleName);
54     }
55
56     /** Return whether logging is enabled
57      * at the FINE level. This method
58      * is not exact because to make it
59      * accurately reflect the logging level
60      * we would have to include the JDK 1.4
61      * java.util.logging.Level class.
62      * @return whether logging is enabled at the fine level.
63      */

64     public boolean isLoggable() {
65         return isLoggable(Level.FINE);
66     }
67     
68     /**
69      * Log a FINE message.
70      * <p>
71      * If the logger is currently enabled for the FINE message
72      * level then the given message is forwarded to all the
73      * registered output Handler objects.
74      * <p>
75      * @param msg The string message (or a key in the message catalog)
76      * @param o Objects to be inserted into the message
77      */

78     public void fine(String JavaDoc msg, Object JavaDoc[] o) {
79         if (isLoggable(Level.FINE)) {
80             inferCaller();
81             logp(Level.FINE, sourceClassName, sourceMethodName, msg, o);
82         }
83     }
84
85     /**
86      * Log a FINE message.
87      * <p>
88      * If the logger is currently enabled for the FINE message
89      * level then the given message is forwarded to all the
90      * registered output Handler objects.
91      * <p>
92      * @param msg The string message (or a key in the message catalog)
93      * @param o1 A parameter to be inserted into the message
94      */

95     public void fine(String JavaDoc msg, Object JavaDoc o1) {
96         if (isLoggable(Level.FINE)) {
97             inferCaller();
98             logp(Level.FINE, sourceClassName, sourceMethodName, msg, o1);
99         }
100     }
101     
102
103     /**
104      * Log a FINE message.
105      * <p>
106      * If the logger is currently enabled for the FINE message
107      * level then the given message is forwarded to all the
108      * registered output Handler objects.
109      * <p>
110      * @param msg The string message (or a key in the message catalog)
111      * @param o1 A parameter to be inserted into the message
112      * @param o2 A parameter to be inserted into the message
113      */

114     public void fine(String JavaDoc msg, Object JavaDoc o1, Object JavaDoc o2) {
115         if (isLoggable(Level.FINE)) {
116             inferCaller();
117             logp(Level.FINE, sourceClassName, sourceMethodName, msg, new Object JavaDoc[]{o1, o2});
118         }
119     }
120     
121     /**
122      * Log a FINE message.
123      * <p>
124      * If the logger is currently enabled for the FINE message
125      * level then the given message is forwarded to all the
126      * registered output Handler objects.
127      * <p>
128      * @param msg The string message (or a key in the message catalog)
129      * @param o1 A parameter to be inserted into the message
130      * @param o2 A parameter to be inserted into the message
131      * @param o3 A parameter to be inserted into the message
132      */

133     public void fine(String JavaDoc msg, Object JavaDoc o1, Object JavaDoc o2, Object JavaDoc o3) {
134         if (isLoggable(Level.FINE)) {
135             inferCaller();
136             logp(Level.FINE, sourceClassName, sourceMethodName, msg, new Object JavaDoc[]{o1, o2, o3});
137         }
138     }
139     
140     /**
141      * Log a FINER message.
142      * <p>
143      * If the logger is currently enabled for the FINER message
144      * level then the given message is forwarded to all the
145      * registered output Handler objects.
146      * <p>
147      * @param msg The string message (or a key in the message catalog)
148      * @param o Objects to be inserted into the message
149      */

150     public void finer(String JavaDoc msg, Object JavaDoc[] o) {
151         if (isLoggable(Level.FINER)) {
152             inferCaller();
153             logp(Level.FINER, sourceClassName, sourceMethodName, msg, o);
154         }
155     }
156
157     /**
158      * Log a FINER message.
159      * <p>
160      * If the logger is currently enabled for the FINER message
161      * level then the given message is forwarded to all the
162      * registered output Handler objects.
163      * <p>
164      * @param msg The string message (or a key in the message catalog)
165      * @param o1 A parameter to be inserted into the message
166      */

167     public void finer(String JavaDoc msg, Object JavaDoc o1) {
168         if (isLoggable(Level.FINER)) {
169             inferCaller();
170             logp(Level.FINER, sourceClassName, sourceMethodName, msg, o1);
171         }
172     }
173     
174     
175     /**
176      * Log a FINER message.
177      * <p>
178      * If the logger is currently enabled for the FINER message
179      * level then the given message is forwarded to all the
180      * registered output Handler objects.
181      * <p>
182      * @param msg The string message (or a key in the message catalog)
183      * @param o1 A parameter to be inserted into the message
184      * @param o2 A parameter to be inserted into the message
185      */

186     public void finer(String JavaDoc msg, Object JavaDoc o1, Object JavaDoc o2) {
187         if (isLoggable(Level.FINER)) {
188             inferCaller();
189             logp(Level.FINER, sourceClassName, sourceMethodName, msg,
190                  new Object JavaDoc[]{o1, o2});
191         }
192     }
193     
194     /**
195      * Log a FINER message.
196      * <p>
197      * If the logger is currently enabled for the FINER message
198      * level then the given message is forwarded to all the
199      * registered output Handler objects.
200      * <p>
201      * @param msg The string message (or a key in the message catalog)
202      * @param o1 A parameter to be inserted into the message
203      * @param o2 A parameter to be inserted into the message
204      * @param o3 A parameter to be inserted into the message
205      */

206     public void finer(String JavaDoc msg, Object JavaDoc o1, Object JavaDoc o2, Object JavaDoc o3) {
207         if (isLoggable(Level.FINER)) {
208             inferCaller();
209             logp(Level.FINER, sourceClassName, sourceMethodName, msg,
210                  new Object JavaDoc[]{o1, o2, o3});
211         }
212     }
213     
214
215     /**
216      * Log a FINEST message.
217      * <p>
218      * If the logger is currently enabled for the FINEST message
219      * level then the given message is forwarded to all the
220      * registered output Handler objects.
221      * <p>
222      * @param msg The string message (or a key in the message catalog)
223      * @param o Objects to be inserted into the message
224      */

225     public void finest(String JavaDoc msg, Object JavaDoc[] o) {
226         if (isLoggable(Level.FINEST)) {
227             inferCaller();
228             logp(Level.FINEST, sourceClassName, sourceMethodName, msg, o);
229         }
230     }
231
232     /**
233      * Log a FINEST message.
234      * <p>
235      * If the logger is currently enabled for the FINEST message
236      * level then the given message is forwarded to all the
237      * registered output Handler objects.
238      * <p>
239      * @param msg The string message (or a key in the message catalog)
240      * @param o1 A parameter to be inserted into the message
241      */

242     public void finest(String JavaDoc msg, Object JavaDoc o1) {
243         if (isLoggable(Level.FINEST)) {
244             inferCaller();
245             logp(Level.FINEST, sourceClassName, sourceMethodName, msg, o1);
246         }
247     }
248     
249     /**
250      * Log a FINEST message.
251      * <p>
252      * If the logger is currently enabled for the FINEST message
253      * level then the given message is forwarded to all the
254      * registered output Handler objects.
255      * <p>
256      * @param msg The string message (or a key in the message catalog)
257      * @param o1 A parameter to be inserted into the message
258      * @param o2 A parameter to be inserted into the message
259      */

260     public void finest(String JavaDoc msg, Object JavaDoc o1, Object JavaDoc o2) {
261         if (isLoggable(Level.FINEST)) {
262             inferCaller();
263             logp(Level.FINEST, sourceClassName, sourceMethodName, msg,
264                  new Object JavaDoc[]{o1, o2});
265         }
266     }
267     
268     /**
269      * Log a FINEST message.
270      * <p>
271      * If the logger is currently enabled for the FINEST message
272      * level then the given message is forwarded to all the
273      * registered output Handler objects.
274      * <p>
275      * @param msg The string message (or a key in the message catalog)
276      * @param o1 A parameter to be inserted into the message
277      * @param o2 A parameter to be inserted into the message
278      * @param o3 A parameter to be inserted into the message
279      */

280     public void finest(String JavaDoc msg, Object JavaDoc o1, Object JavaDoc o2, Object JavaDoc o3) {
281         if (isLoggable(Level.FINEST)) {
282             inferCaller();
283             logp(Level.FINEST, sourceClassName, sourceMethodName, msg,
284                  new Object JavaDoc[]{o1, o2, o3});
285         }
286     }
287     
288     /** Prepare a printable version of this instance.
289      * @return the String representation of this object
290      */

291     public String JavaDoc toString() {
292         StringBuffer JavaDoc buf = new StringBuffer JavaDoc ("LoggerJDK14: "); //NOI18N
293
buf.append (" name: "); buf.append (getName()); //NOI18N
294
buf.append (", super: "); buf.append (super.toString()); //NOI18N
295
buf.append (", logging level: "); buf.append (getLevel()); //NOI18N
296
return buf.toString();
297     }
298     
299     /**
300      * Log a message.
301      * <p>
302      * If the logger is currently enabled for the message
303      * level then the given message is forwarded to all the
304      * registered output Handler objects.
305      * <p>
306      * @param level The level for this message
307      * @param msg The string message (or a key in the message catalog)
308      * @param o1 A parameter to be inserted into the message
309      */

310     public void log(int level, String JavaDoc msg, Object JavaDoc o1) {
311         Level JavaDoc lvl = convertLevel(level);
312         if (isLoggable(lvl)) {
313             inferCaller();
314             logp(lvl, sourceClassName, sourceMethodName, msg, o1);
315         }
316     }
317     
318     /**
319      * Log a message.
320      * <p>
321      * If the logger is currently enabled for the message
322      * level then the given message is forwarded to all the
323      * registered output Handler objects.
324      * <p>
325      * @param level The level for this message
326      * @param msg The string message (or a key in the message catalog)
327      * @param o1 A parameter to be inserted into the message
328      * @param o2 A parameter to be inserted into the message
329      */

330     public void log(int level, String JavaDoc msg, Object JavaDoc o1, Object JavaDoc o2) {
331         Level JavaDoc lvl = convertLevel(level);
332         if (isLoggable(lvl)) {
333             inferCaller();
334             logp(lvl, sourceClassName, sourceMethodName, msg, new Object JavaDoc[]{o1, o2});
335         }
336     }
337     
338     /**
339      * Log a message.
340      * <p>
341      * If the logger is currently enabled for the message
342      * level then the given message is forwarded to all the
343      * registered output Handler objects.
344      * <p>
345      * @param level The level for this message
346      * @param msg The string message (or a key in the message catalog)
347      * @param o1 A parameter to be inserted into the message
348      * @param o2 A parameter to be inserted into the message
349      * @param o3 A parameter to be inserted into the message
350      */

351     public void log(int level, String JavaDoc msg, Object JavaDoc o1, Object JavaDoc o2, Object JavaDoc o3) {
352         Level JavaDoc lvl = convertLevel(level);
353         if (isLoggable(lvl)) {
354             inferCaller();
355             logp(lvl, sourceClassName, sourceMethodName, msg,
356                  new Object JavaDoc[] {o1, o2, o3});
357         }
358     }
359     
360     /**
361      * Log a message.
362      * <p>
363      * If the logger is currently enabled for the message
364      * level then the given message is forwarded to all the
365      * registered output Handler objects.
366      * <p>
367      * @param level The level for this message
368      * @param msg The string message (or a key in the message catalog)
369      * @param o Objects to be inserted into the message
370      */

371     public void log(int level, String JavaDoc msg, Object JavaDoc[] o) {
372         Level JavaDoc lvl = convertLevel(level);
373         if (isLoggable(lvl)) {
374             inferCaller();
375             logp(lvl, sourceClassName, sourceMethodName, msg, o);
376         }
377     }
378     
379     /**
380      * Log a message.
381      * <p>
382      * If the logger is currently enabled for the message
383      * level then the given message is forwarded to all the
384      * registered output Handler objects.
385      * <p>
386      * @param level The level for this message
387      * @param msg The string message (or a key in the message catalog)
388      */

389     public void log(int level, String JavaDoc msg) {
390         Level JavaDoc lvl = convertLevel(level);
391         if (isLoggable(lvl)) {
392             inferCaller();
393             logp(lvl, sourceClassName, sourceMethodName, msg);
394         }
395     }
396     
397
398     /**
399      * Log a message.
400      * <p>
401      * If the logger is currently enabled for the message
402      * level then the given message, and the exception dump,
403      * is forwarded to all the
404      * registered output Handler objects.
405      * <p>
406      * @param level The level for this message
407      * @param msg The string message (or a key in the message catalog)
408      * @param thrown The exception to log
409      */

410     public void log(int level, String JavaDoc msg, Throwable JavaDoc thrown ){
411         Level JavaDoc lvl = convertLevel(level);
412         if (isLoggable(lvl)) {
413             inferCaller();
414             logp(lvl, sourceClassName, sourceMethodName, msg, thrown);
415         }
416     }
417
418     /**
419      * Check if a message of the given level would actually be logged
420      * by this logger. This check is based on the Loggers effective level,
421      * which may be inherited from its parent.
422      *
423      * @return true if the given message level is currently being logged.
424      * @param levelValue the level to check
425      */

426     public boolean isLoggable(int levelValue) {
427         return isLoggable(convertLevel(levelValue));
428     }
429     
430     /** Convert an int level used by jdo logger to the Level instance used
431      * by JDK 1.4 logger.
432      * This is done to allow components to use logging outside the JDK 1.4
433      * environment.
434      * @param level the level to convert
435      * @return the Level instance corresponding to the int level
436      */

437     protected Level JavaDoc convertLevel(int level) {
438         int index = level/100;
439         switch (index) {
440             case 3: return Level.FINEST;
441             case 4: return Level.FINER;
442             case 5: return Level.FINE;
443             case 7: return Level.CONFIG;
444             case 8: return Level.INFO;
445             case 9: return Level.WARNING;
446             case 10: return Level.SEVERE;
447             default: return Level.CONFIG;
448         }
449     }
450
451     /**
452      * Method to infer the caller's class name and method name.
453      * The method analyses the current stack trace, to find the method that
454      * issued the logger call. It stores the callers class and method name
455      * into fields sourceClassName and sourceMethodName.
456      */

457     protected void inferCaller() {
458         // Get the stack trace.
459
StackTraceElement JavaDoc[] stack = (new Throwable JavaDoc()).getStackTrace();
460         // Search for the first frame before the "Logger" class.
461
for(int ix = 0; ix < stack.length; ix++) {
462             StackTraceElement JavaDoc frame = stack[ix];
463             String JavaDoc cname = frame.getClassName();
464             if (!isLoggerClass(cname)) {
465                 // We've found the relevant frame.
466
sourceClassName = cname;
467                 sourceMethodName = frame.getMethodName();
468                 return;
469             }
470         }
471     }
472
473     /**
474      * This method is a helper method for {@link #inferCaller}. It returns
475      * <code>true</code> if the specified class name denotes a logger class
476      * that should be ignored when analysing the stack trace to infer the
477      * caller of a log message.
478      * @param className the class name to be checked.
479      * @return <code>true</code> if the specified name denotes a logger class;
480      * <code>false</code> otherwise.
481      */

482     protected boolean isLoggerClass(String JavaDoc className)
483     {
484         return "com.sun.jdo.spi.persistence.utility.logging.LoggerJDK14". //NOI18N
485
equals(className);
486     }
487 }
488
Popular Tags