package ch.qos.logback.classic; import ch.qos.logback.classic.spi.LoggingEvent; import ch.qos.logback.core.read.ListAppender; import junit.framework.TestCase; public class AppenderWithLevelTest extends TestCase { public void testLoggerWithAppenderWithLowerLevel() { LoggerContext lc = new LoggerContext(); ListAppender listAppender = new ListAppender(); ListAppender listDebugAppender = new ListAppender(); listAppender.start(); listDebugAppender.start(); Logger root = lc.getLogger(LoggerContext.ROOT_NAME); root.setLevel(Level.INFO); root.addAppender(listAppender); root.addAppender(listDebugAppender, Level.DEBUG); root.debug("hello"); // Should have logger's level of info, so nothing appended assertEquals(0, listAppender.list.size()); // Should have own level of debug, so appended assertEquals(1, listDebugAppender.list.size()); } public void testSettingLevelOnLoggerAfterAddingAppenderWithLowerLevel() { LoggerContext lc = new LoggerContext(); ListAppender listAppender = new ListAppender(); ListAppender listDebugAppender = new ListAppender(); listAppender.start(); listDebugAppender.start(); Logger root = lc.getLogger(LoggerContext.ROOT_NAME); root.addAppender(listAppender); root.addAppender(listDebugAppender, Level.DEBUG); root.setLevel(Level.INFO); root.debug("hello"); // Should have logger's level of info, so nothing appended assertEquals(0, listAppender.list.size()); // Should have own level of debug, so appended assertEquals(1, listDebugAppender.list.size()); } public void testLoggerWithAppenderWithHigherLevel() { LoggerContext lc = new LoggerContext(); ListAppender listAppender = new ListAppender(); ListAppender listInfoAppender = new ListAppender(); listAppender.start(); listInfoAppender.start(); Logger root = lc.getLogger(LoggerContext.ROOT_NAME); root.setLevel(Level.DEBUG); root.addAppender(listAppender); root.addAppender(listInfoAppender, Level.INFO); root.debug("hello"); // Should have logger's level of debug, so appended assertEquals(1, listAppender.list.size()); // Should have own level of info, so nothing appended assertEquals(0, listInfoAppender.list.size()); } public void testSettingLevelOnLoggerAfterAddingAppenderWithHigherLevel() { LoggerContext lc = new LoggerContext(); ListAppender listAppender = new ListAppender(); ListAppender listInfoAppender = new ListAppender(); listAppender.start(); listInfoAppender.start(); Logger root = lc.getLogger(LoggerContext.ROOT_NAME); root.addAppender(listAppender); root.addAppender(listInfoAppender, Level.INFO); root.setLevel(Level.DEBUG); root.debug("hello"); // Should have logger's level of debug, so appended assertEquals(1, listAppender.list.size()); // Should have own level of info, so nothing appended assertEquals(0, listInfoAppender.list.size()); } public void testLoggerLevelIrreleventToAppendersWithLevelOnParentLogger() { LoggerContext lc = new LoggerContext(); ListAppender debugListAppender = new ListAppender(); debugListAppender.start(); ListAppender errorListAppender = new ListAppender(); errorListAppender.start(); Logger root = lc.getLogger(LoggerContext.ROOT_NAME); root.setLevel(Level.DEBUG); root.addAppender(debugListAppender, Level.DEBUG); root.addAppender(errorListAppender, Level.ERROR); Logger x = lc.getLogger("x"); x.setLevel(Level.INFO); x.debug("hello"); // Debug appender should get this, error appender not assertEquals(1, debugListAppender.list.size()); assertEquals(0, errorListAppender.list.size()); x.info("hello"); // Debug appender should get this, error appender not assertEquals(2, debugListAppender.list.size()); assertEquals(0, errorListAppender.list.size()); x.error("hello"); // Both appenders should get this assertEquals(3, debugListAppender.list.size()); assertEquals(1, errorListAppender.list.size()); } public void testAppenderWithLevelOnParentDoesNotChangeBehaviourOfNormalAppender() { LoggerContext lc = new LoggerContext(); ListAppender listDebugAppender = new ListAppender(); listDebugAppender.start(); ListAppender listAppender = new ListAppender(); listAppender.start(); Logger root = lc.getLogger(LoggerContext.ROOT_NAME); root.setLevel(Level.INFO); root.addAppender(listDebugAppender, Level.DEBUG); root.addAppender(listAppender); Logger x = lc.getLogger("x"); x.setLevel(Level.ERROR); x.debug("hello"); assertEquals(0, listAppender.list.size()); assertEquals(1, listDebugAppender.list.size()); } public void testLoggerWithLowerLevelOverridesAppenderWithLevelOnParentLogger() { LoggerContext lc = new LoggerContext(); ListAppender listAppender = new ListAppender(); listAppender.start(); Logger root = lc.getLogger(LoggerContext.ROOT_NAME); root.setLevel(Level.INFO); root.addAppender(listAppender, Level.INFO); Logger x = lc.getLogger("x"); x.setLevel(Level.DEBUG); x.debug("hello"); // Should have appender's level of info, so not appended assertEquals(0, listAppender.list.size()); x.info("hello again"); assertEquals(1, listAppender.list.size()); } public void testAppenderWithLowerLevelOverridesLevelOnParentLogger() { LoggerContext lc = new LoggerContext(); ListAppender listAppender = new ListAppender(); listAppender.start(); Logger root = lc.getLogger(LoggerContext.ROOT_NAME); root.setLevel(Level.INFO); Logger x = lc.getLogger("x"); x.addAppender(listAppender, Level.DEBUG); x.debug("hello"); // Should have appender's level of debug, so appended assertEquals(1, listAppender.list.size()); } public void testAppenderWithHigherLevelOverridesLevelOnParentLogger() { LoggerContext lc = new LoggerContext(); ListAppender listAppender = new ListAppender(); listAppender.start(); Logger root = lc.getLogger(LoggerContext.ROOT_NAME); root.setLevel(Level.DEBUG); Logger x = lc.getLogger("x"); x.addAppender(listAppender, Level.INFO); x.debug("hello"); // Should have appender's level of info, so not appended assertEquals(0, listAppender.list.size()); } public void testAppenderWithLevelOverridesAppenderWithLevelOnParentLogger() { LoggerContext lc = new LoggerContext(); ListAppender listAppender = new ListAppender(); listAppender.start(); Logger root = lc.getLogger(LoggerContext.ROOT_NAME); root.setLevel(Level.INFO); root.addAppender(listAppender, Level.INFO); Logger x = lc.getLogger("x"); x.addAppender(listAppender, Level.DEBUG); x.debug("hello"); // Should have latest appender's level of debug, so appended assertEquals(1, listAppender.list.size()); } public void testChangingParentLevelWhenChildHasAppenderWithLevel() { LoggerContext lc = new LoggerContext(); ListAppender listDebugAppender = new ListAppender(); listDebugAppender.start(); ListAppender listAppender = new ListAppender(); listAppender.start(); Logger x = lc.getLogger("x"); x.addAppender(listDebugAppender, Level.DEBUG); Logger root = lc.getLogger(LoggerContext.ROOT_NAME); root.setLevel(Level.INFO); root.addAppender(listAppender); x.debug("hello"); // Should have x's appender's level of debug, so appended assertEquals(1, listDebugAppender.list.size()); // Should have root's level of info, so not appended assertEquals(0, listAppender.list.size()); } }