@Test public void eval() throws JoranException { configure(ClassicTestConstants.JORAN_INPUT_PREFIX + "callerData.xml"); String msg = "hello world"; logger.debug("toto"); logger.debug(msg); StringListAppender<ILoggingEvent> slAppender = (StringListAppender<ILoggingEvent>) loggerContext .getLogger("root").getAppender("STR_LIST"); assertNotNull(slAppender); assertEquals(2, slAppender.strList.size()); assertTrue(slAppender.strList.get(0).contains(" DEBUG - toto")); String str1 = slAppender.strList.get(1); assertTrue(str1.contains("Caller+0")); assertTrue(str1.contains(" DEBUG - hello world")); }
@Test public void localPropertiesShouldBeVisible() throws JoranException { String mdcKey = "localProperty"; String mdcVal = "" + diff; String msg = "localPropertiesShouldBeVisible"; String prefix = "Y"; configure(SIFT_FOLDER_PREFIX + "propertyPropagation.xml"); MDC.put(mdcKey, mdcVal); logger.debug(msg); SiftingAppender sa = (SiftingAppender) root.getAppender("SIFT"); StringListAppender<ILoggingEvent> listAppender = (StringListAppender<ILoggingEvent>) sa .getAppenderTracker().find(mdcVal); assertNotNull(listAppender); List<String> strList = listAppender.strList; assertEquals(1, listAppender.strList.size()); assertEquals(prefix + msg, strList.get(0)); }
@Test public void propertyDefinedWithinSiftElementShouldBeVisible() throws JoranException { String mdcKey = "propertyDefinedWithinSift"; String mdcVal = "" + diff; String msg = "propertyDefinedWithinSiftElementShouldBeVisible"; String prefix = "Y"; configure(SIFT_FOLDER_PREFIX + "propertyDefinedInSiftElement.xml"); MDC.put(mdcKey, mdcVal); logger.debug(msg); SiftingAppender sa = (SiftingAppender) root.getAppender("SIFT"); StringListAppender<ILoggingEvent> listAppender = (StringListAppender<ILoggingEvent>) sa .getAppenderTracker().find(mdcVal); assertNotNull(listAppender); List<String> strList = listAppender.strList; assertEquals(1, listAppender.strList.size()); assertEquals(prefix + msg, strList.get(0)); }
@Test public void compositePropertyShouldCombineWithinAndWithoutSiftElement() throws JoranException { String mdcKey = "compositeProperty"; String mdcVal = "" + diff; String msg = "compositePropertyShouldCombineWithinAndWithoutSiftElement"; String prefix = "composite"; configure(SIFT_FOLDER_PREFIX + "compositeProperty.xml"); MDC.put(mdcKey, mdcVal); logger.debug(msg); SiftingAppender sa = (SiftingAppender) root.getAppender("SIFT"); StringListAppender<ILoggingEvent> listAppender = (StringListAppender<ILoggingEvent>) sa .getAppenderTracker().find(mdcVal); assertNotNull(listAppender); List<String> strList = listAppender.strList; assertEquals(1, listAppender.strList.size()); assertEquals(prefix + msg, strList.get(0)); }
@Test public void testConversionRuleSupportInPatternLayout() throws JoranException { configure(ClassicTestConstants.JORAN_INPUT_PREFIX + "conversionRule/patternLayout0.xml"); root.getAppender("LIST"); String msg = "Simon says"; logger.debug(msg); StringListAppender<ILoggingEvent> sla = (StringListAppender<ILoggingEvent>) root.getAppender("LIST"); assertNotNull(sla); assertEquals(1, sla.strList.size()); assertEquals(SampleConverter.SAMPLE_STR + " - " + msg, sla.strList.get(0)); }
@Test public void replaceWithJoran() throws JoranException { configure(ClassicTestConstants.JORAN_INPUT_PREFIX + "pattern/replace0.xml"); StatusPrinter.print(lc); root.getAppender("LIST"); String msg = "And the number is 4111111111110000, expiring on 12/2010"; logger.debug(msg); StringListAppender<ILoggingEvent> sla = (StringListAppender<ILoggingEvent>) root.getAppender("LIST"); assertNotNull(sla); assertEquals(1, sla.strList.size()); assertEquals("And the number is XXXX, expiring on 12/2010", sla.strList.get(0)); }
@Test public void defaultLayoutRule() throws JoranException { configure(SIFT_FOLDER_PREFIX + "defaultLayoutRule.xml"); logger.debug("hello"); SiftingAppender ha = (SiftingAppender) root.getAppender("SIFT"); StringListAppender<ILoggingEvent> listAppender = (StringListAppender<ILoggingEvent>) ha .getAppenderTracker().find("default"); assertNotNull(listAppender); List<String> strList = listAppender.strList; assertEquals(1, strList.size()); assertEquals("DEBUG hello", strList.get(0)); }
@Test public void testConversionRuleSupportInHtmlLayout() throws JoranException { configure(ClassicTestConstants.JORAN_INPUT_PREFIX + "conversionRule/htmlLayout0.xml"); root.getAppender("LIST"); String msg = "Simon says"; root.debug(msg); StringListAppender<ILoggingEvent> sla = (StringListAppender<ILoggingEvent>) root .getAppender("LIST"); assertNotNull(sla); StatusPrinter.print(lc); assertEquals(1, sla.strList.size()); assertFalse(sla.strList.get(0).contains("PARSER_ERROR")); }
@Test public void defaultLayout() throws Exception { configure(TeztConstants.TEST_DIR_PREFIX + "input/joran/defaultLayout.xml"); StringListAppender<IAccessEvent> listAppender = (StringListAppender<IAccessEvent>) context .getAppender("STR_LIST"); IAccessEvent event = DummyAccessEventBuilder.buildNewAccessEvent(); listAppender.doAppend(event); assertEquals(1, listAppender.strList.size()); // the result contains a line separator at the end assertTrue(listAppender.strList.get(0).startsWith("testMethod")); }
@BeforeClass public static void setupClass() throws Exception { slf4jLogger = LoggerFactory.getLogger(LoggerTest.class); context = ((ch.qos.logback.classic.Logger) slf4jLogger).getLoggerContext(); configure(CONFIG); logger = LogManager.getLogger(LoggerTest.class); assertTrue("Incorrect SLF4J Logger", ((SLF4JLogger) logger).getLogger() == slf4jLogger); root = LogManager.getRootLogger(); rootLogger = context.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME); list = (StringListAppender<ILoggingEvent>) rootLogger.getAppender("LIST"); rootLogger.detachAppender("console"); }
public static StringListAppender<ILoggingEvent> getListAppender(final SLF4JLogger slf4jLogger, final String name) { final Logger logger = slf4jLogger.getLogger(); if (!(logger instanceof AppenderAttachable)) { throw new AssertionError("SLF4JLogger.getLogger() did not return an instance of AppenderAttachable"); } @SuppressWarnings("unchecked") final AppenderAttachable<ILoggingEvent> attachable = (AppenderAttachable<ILoggingEvent>) logger; return getListAppender(attachable, name); }
@Test public void testClassLogger() throws Exception { final SLF4JLogger logger = (SLF4JLogger) LogManager.getLogger("ClassLogger"); final StringListAppender<ILoggingEvent> app = TestUtil.getListAppender(logger, "Class"); logger.info("Ignored message contents."); logger.warn("Verifying the caller class is still correct."); logger.error("Hopefully nobody breaks me!"); final List<String> messages = app.strList; assertEquals("Incorrect number of messages.", 3, messages.size()); for (final String message : messages) { assertEquals("Incorrect caller class name.", this.getClass().getName(), message); } }
@Test public void testMethodLogger() throws Exception { final SLF4JLogger logger = (SLF4JLogger) LogManager.getLogger("MethodLogger"); final StringListAppender<ILoggingEvent> app = TestUtil.getListAppender(logger, "Method"); logger.info("More messages."); logger.warn("CATASTROPHE INCOMING!"); logger.error("ZOMBIES!!!"); logger.warn("brains~~~"); logger.info("Itchy. Tasty."); final List<String> messages = app.strList; assertEquals("Incorrect number of messages.", 5, messages.size()); for (final String message : messages) { assertEquals("Incorrect caller method name.", "testMethodLogger", message); } }
public static StringListAppender<ILoggingEvent> getListAppender(final AppenderAttachable<ILoggingEvent> logger, final String name) { return (StringListAppender<ILoggingEvent>) logger.getAppender(name); }