Author: rgoers Date: Tue Mar 3 08:26:07 2009 New Revision: 1284 Modified: slf4j/trunk/slf4j-ext/src/main/java/org/slf4j/ext/XLogger.java slf4j/trunk/slf4j-ext/src/test/java/org/slf4j/dummyExt/XLoggerTest.java
Log: Fix for bug 128 - Allow logging level to be specified on catching and throwing Modified: slf4j/trunk/slf4j-ext/src/main/java/org/slf4j/ext/XLogger.java ============================================================================== --- slf4j/trunk/slf4j-ext/src/main/java/org/slf4j/ext/XLogger.java (original) +++ slf4j/trunk/slf4j-ext/src/main/java/org/slf4j/ext/XLogger.java Tue Mar 3 08:26:07 2009 @@ -47,6 +47,30 @@ ENTRY_MESSAGE_ARRAY[4] = ENTRY_MESSAGE_4; } + public enum Level { + TRACE("TRACE", LocationAwareLogger.TRACE_INT), + DEBUG("DEBUG", LocationAwareLogger.DEBUG_INT), + INFO("INFO", LocationAwareLogger.INFO_INT), + WARN("WARN", LocationAwareLogger.WARN_INT), + ERROR("ERROR", LocationAwareLogger.ERROR_INT); + + private final String name; + private final int level; + + public String toString() { + return this.name; + } + + public int intValue() { + return this.level; + } + + private Level(String name, int level) { + this.name = name; + this.level = level; + } + } + /** * Given an underlying logger, construct an XLogger * @@ -106,7 +130,7 @@ } /** - * Log an exception being thrown + * Log an exception being thrown. The generated log event uses Level ERROR. * * @param throwable * the exception being caught. @@ -119,7 +143,21 @@ } /** - * Log an exception being caught + * Log an exception being thrown allowing the log level to be specified. + * + * @param level the logging level to use. + * @param throwable + * the exception being caught. + */ + public void throwing(Level level, Throwable throwable) { + if (instanceofLAL) { + ((LocationAwareLogger) logger).log(THROWING_MARKER, FQCN, + level.level, "throwing", throwable); + } + } + + /** + * Log an exception being caught. The generated log event uses Level ERROR. * * @param throwable * the exception being caught. @@ -131,6 +169,20 @@ } } + /** + * Log an exception being caught allowing the log level to be specified. + * + * @param level the logging level to use. + * @param throwable + * the exception being caught. + */ + public void catching(Level level, Throwable throwable) { + if (instanceofLAL) { + ((LocationAwareLogger) logger).log(CATCHING_MARKER, FQCN, + level.level, "catching", throwable); + } + } + private static String buildMessagePattern(int len) { StringBuilder sb = new StringBuilder(); sb.append(" entry with ("); Modified: slf4j/trunk/slf4j-ext/src/test/java/org/slf4j/dummyExt/XLoggerTest.java ============================================================================== --- slf4j/trunk/slf4j-ext/src/test/java/org/slf4j/dummyExt/XLoggerTest.java (original) +++ slf4j/trunk/slf4j-ext/src/test/java/org/slf4j/dummyExt/XLoggerTest.java Tue Mar 3 08:26:07 2009 @@ -68,6 +68,12 @@ assertEquals(t.toString(), le.getThrowableStrRep()[0]); } + void verifyWithLevelAndException(LoggingEvent le, XLogger.Level level, String expectedMsg, Throwable t) { + verify(le, expectedMsg); + assertEquals(t.toString(), le.getThrowableStrRep()[0]); + assertEquals(le.getLevel().toString(), level.toString()); + } + public void testEntering() { XLogger logger = XLoggerFactory.getXLogger("UnitTest"); logger.entry(); @@ -96,8 +102,12 @@ XLogger logger = XLoggerFactory.getXLogger("UnitTest"); Throwable t = new UnsupportedOperationException("Test"); logger.throwing(t); - assertEquals(1, listAppender.list.size()); + logger.throwing(XLogger.Level.DEBUG,t); + assertEquals(2, listAppender.list.size()); verifyWithException((LoggingEvent) listAppender.list.get(0), "throwing", t); + LoggingEvent event = (LoggingEvent)listAppender.list.get(1); + verifyWithLevelAndException((LoggingEvent) listAppender.list.get(1), XLogger.Level.DEBUG, + "throwing", t); } public void testCaught() { @@ -110,14 +120,17 @@ } catch (Exception ex) { t = ex; logger.catching(ex); + logger.catching(XLogger.Level.DEBUG, ex); } verifyWithException((LoggingEvent) listAppender.list.get(0), "catching", t); + verifyWithLevelAndException((LoggingEvent) listAppender.list.get(1), XLogger.Level.DEBUG, + "catching", t); } // See http://bugzilla.slf4j.org/show_bug.cgi?id=114 public void testLocationExtraction_Bug114() { XLogger logger = XLoggerFactory.getXLogger("UnitTest"); - int line = 121; // next line is line number 121 + int line = 134; // next line is line number 134 logger.exit(); logger.debug("hello"); _______________________________________________ dev mailing list dev@slf4j.org http://www.slf4j.org/mailman/listinfo/dev