001: /*
002: * Copyright (c) 2004-2005 SLF4J.ORG
003: * Copyright (c) 2004-2005 QOS.ch
004: *
005: * All rights reserved.
006: *
007: * Permission is hereby granted, free of charge, to any person obtaining
008: * a copy of this software and associated documentation files (the
009: * "Software"), to deal in the Software without restriction, including
010: * without limitation the rights to use, copy, modify, merge, publish,
011: * distribute, and/or sell copies of the Software, and to permit persons
012: * to whom the Software is furnished to do so, provided that the above
013: * copyright notice(s) and this permission notice appear in all copies of
014: * the Software and that both the above copyright notice(s) and this
015: * permission notice appear in supporting documentation.
016: *
017: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
018: * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
019: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
020: * OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
021: * HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY
022: * SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER
023: * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
024: * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
025: * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
026: *
027: * Except as contained in this notice, the name of a copyright holder
028: * shall not be used in advertising or otherwise to promote the sale, use
029: * or other dealings in this Software without prior written authorization
030: * of the copyright holder.
031: *
032: */
033:
034: package org.slf4j.impl;
035:
036: import java.util.logging.Level;
037: import java.util.logging.LogRecord;
038:
039: import org.slf4j.Logger;
040: import org.slf4j.Marker;
041: import org.slf4j.helpers.MarkerIgnoringBase;
042: import org.slf4j.helpers.MessageFormatter;
043: import org.slf4j.spi.LocationAwareLogger;
044:
045: /**
046: * A wrapper over {@link java.util.logging.Logger java.util.logging.Logger} in
047: * conformity with the {@link Logger} interface. Note that the logging levels
048: * mentioned in this class refer to those defined in the java.util.logging
049: * package.
050: *
051: * @author Ceki Gülcü
052: * @author Peter Royal
053: */
054: public final class JDK14LoggerAdapter extends MarkerIgnoringBase
055: implements LocationAwareLogger {
056: final java.util.logging.Logger logger;
057:
058: // WARN: JDK14LoggerAdapter constructor should have only package access so
059: // that only JDK14LoggerFactory be able to create one.
060: JDK14LoggerAdapter(java.util.logging.Logger logger) {
061: this .logger = logger;
062: }
063:
064: public String getName() {
065: return logger.getName();
066: }
067:
068: /**
069: * Is this logger instance enabled for the FINEST level?
070: *
071: * @return True if this Logger is enabled for level FINEST, false otherwise.
072: */
073: public boolean isTraceEnabled() {
074: return logger.isLoggable(Level.FINEST);
075: }
076:
077: /**
078: * Log a message object at level FINEST.
079: *
080: * @param msg -
081: * the message object to be logged
082: */
083: public void trace(String msg) {
084: if (logger.isLoggable(Level.FINEST)) {
085: log(SELF, Level.FINEST, msg, null);
086: }
087: }
088:
089: /**
090: * Log a message at level FINEST according to the specified format and
091: * argument.
092: *
093: * <p>
094: * This form avoids superfluous object creation when the logger is disabled
095: * for level FINEST.
096: * </p>
097: *
098: * @param format
099: * the format string
100: * @param arg
101: * the argument
102: */
103: public void trace(String format, Object arg) {
104: if (logger.isLoggable(Level.FINEST)) {
105: String msgStr = MessageFormatter.format(format, arg);
106: log(SELF, Level.FINEST, msgStr, null);
107: }
108: }
109:
110: /**
111: * Log a message at level FINEST according to the specified format and
112: * arguments.
113: *
114: * <p>
115: * This form avoids superfluous object creation when the logger is disabled
116: * for the FINEST level.
117: * </p>
118: *
119: * @param format
120: * the format string
121: * @param arg1
122: * the first argument
123: * @param arg2
124: * the second argument
125: */
126: public void trace(String format, Object arg1, Object arg2) {
127: if (logger.isLoggable(Level.FINEST)) {
128: String msgStr = MessageFormatter.format(format, arg1, arg2);
129: log(SELF, Level.FINEST, msgStr, null);
130: }
131: }
132:
133: /**
134: * Log a message at level FINEST according to the specified format and
135: * arguments.
136: *
137: * <p>
138: * This form avoids superfluous object creation when the logger is disabled
139: * for the FINEST level.
140: * </p>
141: *
142: * @param format
143: * the format string
144: * @param argArray
145: * an array of arguments
146: */
147: public void trace(String format, Object[] argArray) {
148: if (logger.isLoggable(Level.FINEST)) {
149: String msgStr = MessageFormatter.arrayFormat(format,
150: argArray);
151: log(SELF, Level.FINEST, msgStr, null);
152: }
153: }
154:
155: /**
156: * Log an exception (throwable) at level FINEST with an accompanying message.
157: *
158: * @param msg
159: * the message accompanying the exception
160: * @param t
161: * the exception (throwable) to log
162: */
163: public void trace(String msg, Throwable t) {
164: if (logger.isLoggable(Level.FINEST)) {
165: log(SELF, Level.FINEST, msg, t);
166: }
167: }
168:
169: /**
170: * Is this logger instance enabled for the FINE level?
171: *
172: * @return True if this Logger is enabled for level FINE, false otherwise.
173: */
174: public boolean isDebugEnabled() {
175: return logger.isLoggable(Level.FINE);
176: }
177:
178: /**
179: * Log a message object at level FINE.
180: *
181: * @param msg -
182: * the message object to be logged
183: */
184: public void debug(String msg) {
185: if (logger.isLoggable(Level.FINE)) {
186: log(SELF, Level.FINE, msg, null);
187: }
188: }
189:
190: /**
191: * Log a message at level FINE according to the specified format and argument.
192: *
193: * <p>
194: * This form avoids superfluous object creation when the logger is disabled
195: * for level FINE.
196: * </p>
197: *
198: * @param format
199: * the format string
200: * @param arg
201: * the argument
202: */
203: public void debug(String format, Object arg) {
204: if (logger.isLoggable(Level.FINE)) {
205: String msgStr = MessageFormatter.format(format, arg);
206: log(SELF, Level.FINE, msgStr, null);
207: }
208: }
209:
210: /**
211: * Log a message at level FINE according to the specified format and
212: * arguments.
213: *
214: * <p>
215: * This form avoids superfluous object creation when the logger is disabled
216: * for the FINE level.
217: * </p>
218: *
219: * @param format
220: * the format string
221: * @param arg1
222: * the first argument
223: * @param arg2
224: * the second argument
225: */
226: public void debug(String format, Object arg1, Object arg2) {
227: if (logger.isLoggable(Level.FINE)) {
228: String msgStr = MessageFormatter.format(format, arg1, arg2);
229: log(SELF, Level.FINE, msgStr, null);
230: }
231: }
232:
233: /**
234: * Log a message at level FINE according to the specified format and
235: * arguments.
236: *
237: * <p>
238: * This form avoids superfluous object creation when the logger is disabled
239: * for the FINE level.
240: * </p>
241: *
242: * @param format
243: * the format string
244: * @param argArray
245: * an array of arguments
246: */
247: public void debug(String format, Object[] argArray) {
248: if (logger.isLoggable(Level.FINE)) {
249: String msgStr = MessageFormatter.arrayFormat(format,
250: argArray);
251: log(SELF, Level.FINE, msgStr, null);
252: }
253: }
254:
255: /**
256: * Log an exception (throwable) at level FINE with an accompanying message.
257: *
258: * @param msg
259: * the message accompanying the exception
260: * @param t
261: * the exception (throwable) to log
262: */
263: public void debug(String msg, Throwable t) {
264: if (logger.isLoggable(Level.FINE)) {
265: log(SELF, Level.FINE, msg, t);
266: }
267: }
268:
269: /**
270: * Is this logger instance enabled for the INFO level?
271: *
272: * @return True if this Logger is enabled for the INFO level, false otherwise.
273: */
274: public boolean isInfoEnabled() {
275: return logger.isLoggable(Level.INFO);
276: }
277:
278: /**
279: * Log a message object at the INFO level.
280: *
281: * @param msg -
282: * the message object to be logged
283: */
284: public void info(String msg) {
285: if (logger.isLoggable(Level.INFO)) {
286: log(SELF, Level.INFO, msg, null);
287: }
288: }
289:
290: /**
291: * Log a message at level INFO according to the specified format and argument.
292: *
293: * <p>
294: * This form avoids superfluous object creation when the logger is disabled
295: * for the INFO level.
296: * </p>
297: *
298: * @param format
299: * the format string
300: * @param arg
301: * the argument
302: */
303: public void info(String format, Object arg) {
304: if (logger.isLoggable(Level.INFO)) {
305: String msgStr = MessageFormatter.format(format, arg);
306: log(SELF, Level.INFO, msgStr, null);
307: }
308: }
309:
310: /**
311: * Log a message at the INFO level according to the specified format and
312: * arguments.
313: *
314: * <p>
315: * This form avoids superfluous object creation when the logger is disabled
316: * for the INFO level.
317: * </p>
318: *
319: * @param format
320: * the format string
321: * @param arg1
322: * the first argument
323: * @param arg2
324: * the second argument
325: */
326: public void info(String format, Object arg1, Object arg2) {
327: if (logger.isLoggable(Level.INFO)) {
328: String msgStr = MessageFormatter.format(format, arg1, arg2);
329: log(SELF, Level.INFO, msgStr, null);
330: }
331: }
332:
333: /**
334: * Log a message at level INFO according to the specified format and
335: * arguments.
336: *
337: * <p>
338: * This form avoids superfluous object creation when the logger is disabled
339: * for the INFO level.
340: * </p>
341: *
342: * @param format
343: * the format string
344: * @param argArray
345: * an array of arguments
346: */
347: public void info(String format, Object[] argArray) {
348: if (logger.isLoggable(Level.INFO)) {
349: String msgStr = MessageFormatter.arrayFormat(format,
350: argArray);
351: log(SELF, Level.INFO, msgStr, null);
352: }
353: }
354:
355: /**
356: * Log an exception (throwable) at the INFO level with an accompanying
357: * message.
358: *
359: * @param msg
360: * the message accompanying the exception
361: * @param t
362: * the exception (throwable) to log
363: */
364: public void info(String msg, Throwable t) {
365: if (logger.isLoggable(Level.INFO)) {
366: log(SELF, Level.INFO, msg, t);
367: }
368: }
369:
370: /**
371: * Is this logger instance enabled for the WARNING level?
372: *
373: * @return True if this Logger is enabled for the WARNING level, false
374: * otherwise.
375: */
376: public boolean isWarnEnabled() {
377: return logger.isLoggable(Level.WARNING);
378: }
379:
380: /**
381: * Log a message object at the WARNING level.
382: *
383: * @param msg -
384: * the message object to be logged
385: */
386: public void warn(String msg) {
387: if (logger.isLoggable(Level.WARNING)) {
388: log(SELF, Level.WARNING, msg, null);
389: }
390: }
391:
392: /**
393: * Log a message at the WARNING level according to the specified format and
394: * argument.
395: *
396: * <p>
397: * This form avoids superfluous object creation when the logger is disabled
398: * for the WARNING level.
399: * </p>
400: *
401: * @param format
402: * the format string
403: * @param arg
404: * the argument
405: */
406: public void warn(String format, Object arg) {
407: if (logger.isLoggable(Level.WARNING)) {
408: String msgStr = MessageFormatter.format(format, arg);
409: log(SELF, Level.WARNING, msgStr, null);
410: }
411: }
412:
413: /**
414: * Log a message at the WARNING level according to the specified format and
415: * arguments.
416: *
417: * <p>
418: * This form avoids superfluous object creation when the logger is disabled
419: * for the WARNING level.
420: * </p>
421: *
422: * @param format
423: * the format string
424: * @param arg1
425: * the first argument
426: * @param arg2
427: * the second argument
428: */
429: public void warn(String format, Object arg1, Object arg2) {
430: if (logger.isLoggable(Level.WARNING)) {
431: String msgStr = MessageFormatter.format(format, arg1, arg2);
432: log(SELF, Level.WARNING, msgStr, null);
433: }
434: }
435:
436: /**
437: * Log a message at level WARNING according to the specified format and
438: * arguments.
439: *
440: * <p>
441: * This form avoids superfluous object creation when the logger is disabled
442: * for the WARNING level.
443: * </p>
444: *
445: * @param format
446: * the format string
447: * @param argArray
448: * an array of arguments
449: */
450: public void warn(String format, Object[] argArray) {
451: if (logger.isLoggable(Level.WARNING)) {
452: String msgStr = MessageFormatter.arrayFormat(format,
453: argArray);
454: log(SELF, Level.WARNING, msgStr, null);
455: }
456: }
457:
458: /**
459: * Log an exception (throwable) at the WARNING level with an accompanying
460: * message.
461: *
462: * @param msg
463: * the message accompanying the exception
464: * @param t
465: * the exception (throwable) to log
466: */
467: public void warn(String msg, Throwable t) {
468: if (logger.isLoggable(Level.WARNING)) {
469: log(SELF, Level.WARNING, msg, t);
470: }
471: }
472:
473: /**
474: * Is this logger instance enabled for level SEVERE?
475: *
476: * @return True if this Logger is enabled for level SEVERE, false otherwise.
477: */
478: public boolean isErrorEnabled() {
479: return logger.isLoggable(Level.SEVERE);
480: }
481:
482: /**
483: * Log a message object at the SEVERE level.
484: *
485: * @param msg -
486: * the message object to be logged
487: */
488: public void error(String msg) {
489: if (logger.isLoggable(Level.SEVERE)) {
490: log(SELF, Level.SEVERE, msg, null);
491: }
492: }
493:
494: /**
495: * Log a message at the SEVERE level according to the specified format and
496: * argument.
497: *
498: * <p>
499: * This form avoids superfluous object creation when the logger is disabled
500: * for the SEVERE level.
501: * </p>
502: *
503: * @param format
504: * the format string
505: * @param arg
506: * the argument
507: */
508: public void error(String format, Object arg) {
509: if (logger.isLoggable(Level.SEVERE)) {
510: String msgStr = MessageFormatter.format(format, arg);
511: log(SELF, Level.SEVERE, msgStr, null);
512: }
513: }
514:
515: /**
516: * Log a message at the SEVERE level according to the specified format and
517: * arguments.
518: *
519: * <p>
520: * This form avoids superfluous object creation when the logger is disabled
521: * for the SEVERE level.
522: * </p>
523: *
524: * @param format
525: * the format string
526: * @param arg1
527: * the first argument
528: * @param arg2
529: * the second argument
530: */
531: public void error(String format, Object arg1, Object arg2) {
532: if (logger.isLoggable(Level.SEVERE)) {
533: String msgStr = MessageFormatter.format(format, arg1, arg2);
534: log(SELF, Level.SEVERE, msgStr, null);
535: }
536: }
537:
538: /**
539: * Log a message at level INFO according to the specified format and
540: * arguments.
541: *
542: * <p>
543: * This form avoids superfluous object creation when the logger is disabled
544: * for the INFO level.
545: * </p>
546: *
547: * @param format
548: * the format string
549: * @param argArray
550: * an array of arguments
551: */
552: public void error(String format, Object[] argArray) {
553: if (logger.isLoggable(Level.SEVERE)) {
554: String msgStr = MessageFormatter.arrayFormat(format,
555: argArray);
556: log(SELF, Level.SEVERE, msgStr, null);
557: }
558: }
559:
560: /**
561: * Log an exception (throwable) at the SEVERE level with an accompanying
562: * message.
563: *
564: * @param msg
565: * the message accompanying the exception
566: * @param t
567: * the exception (throwable) to log
568: */
569: public void error(String msg, Throwable t) {
570: if (logger.isLoggable(Level.SEVERE)) {
571: log(SELF, Level.SEVERE, msg, t);
572: }
573: }
574:
575: /**
576: * Log the message at the specified level with the specified throwable if any.
577: * This method creates a LogRecord and fills in caller date before calling
578: * this instance's JDK14 logger.
579: *
580: * See bug report #13 for more details.
581: *
582: * @param level
583: * @param msg
584: * @param t
585: */
586: private void log(String callerFQCN, Level level, String msg,
587: Throwable t) {
588: // millis and thread are filled by the constructor
589: LogRecord record = new LogRecord(level, msg);
590: record.setLoggerName(getName());
591: record.setThrown(t);
592: fillCallerData(callerFQCN, record);
593: logger.log(record);
594:
595: }
596:
597: static String SELF = JDK14LoggerAdapter.class.getName();
598: static String SUPER = MarkerIgnoringBase.class.getName();
599:
600: /**
601: * Fill in caller data if possible.
602: *
603: * @param record
604: * The record to update
605: */
606: final private void fillCallerData(String callerFQCN,
607: LogRecord record) {
608: StackTraceElement[] steArray = new Throwable().getStackTrace();
609:
610: int selfIndex = -1;
611: for (int i = 0; i < steArray.length; i++) {
612: final String className = steArray[i].getClassName();
613: if (className.equals(callerFQCN) || className.equals(SUPER)) {
614: selfIndex = i;
615: break;
616: }
617: }
618:
619: int found = -1;
620: for (int i = selfIndex + 1; i < steArray.length; i++) {
621: final String className = steArray[i].getClassName();
622: if (!(className.equals(callerFQCN) || className
623: .equals(SUPER))) {
624: found = i;
625: break;
626: }
627: }
628:
629: if (found != -1) {
630: StackTraceElement ste = steArray[found];
631: // setting the class name has the side effect of setting
632: // the needToInferCaller variable to false.
633: record.setSourceClassName(ste.getClassName());
634: record.setSourceMethodName(ste.getMethodName());
635: }
636: }
637:
638: public void log(Marker marker, String callerFQCN, int level,
639: String message, Throwable t) {
640: Level julLevel;
641: switch (level) {
642: case LocationAwareLogger.TRACE_INT:
643: julLevel = Level.FINEST;
644: break;
645: case LocationAwareLogger.DEBUG_INT:
646: julLevel = Level.FINE;
647: break;
648: case LocationAwareLogger.INFO_INT:
649: julLevel = Level.INFO;
650: break;
651: case LocationAwareLogger.WARN_INT:
652: julLevel = Level.WARNING;
653: break;
654: case LocationAwareLogger.ERROR_INT:
655: julLevel = Level.SEVERE;
656: break;
657: default:
658: throw new IllegalStateException("Level number " + level
659: + " is not recognized.");
660: }
661: log(callerFQCN, julLevel, message, t);
662: }
663: }
|