Java Code Examples for org.apache.logging.log4j.test.appender.ListAppender#getMessages()
The following examples show how to use
org.apache.logging.log4j.test.appender.ListAppender#getMessages() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Log4j2_2195_Test.java From logging-log4j2 with Apache License 2.0 | 6 votes |
@Test public void test() { logger.info("This is a test.", new Exception("Test exception!")); ListAppender listAppender = loggerContextRule.getListAppender("ListAppender"); Assert.assertNotNull(listAppender); List<String> events = listAppender.getMessages(); Assert.assertNotNull(events); Assert.assertEquals(1, events.size()); String logEvent = events.get(0); Assert.assertNotNull(logEvent); Assert.assertFalse("\"org.junit\" should not be here", logEvent.contains("org.junit")); Assert.assertFalse("\"org.eclipse\" should not be here", logEvent.contains("org.eclipse")); // Layout<? extends Serializable> layout = listAppender.getLayout(); PatternLayout pLayout = (PatternLayout) layout; Assert.assertNotNull(pLayout); Serializer eventSerializer = pLayout.getEventSerializer(); Assert.assertNotNull(eventSerializer); // Assert.assertTrue("Missing \"|\"", logEvent.contains("|")); }
Example 2
Source File: AbstractScriptFilterTest.java From logging-log4j2 with Apache License 2.0 | 6 votes |
@Test public void testJavascriptFilter() throws Exception { final Logger logger = LogManager.getLogger("TestJavaScriptFilter"); logger.traceEntry(); logger.info("This should not be logged"); ThreadContext.put("UserId", "JohnDoe"); logger.info("This should be logged"); ThreadContext.clearMap(); final ListAppender app = getContext().getListAppender("List"); final List<String> messages = app.getMessages(); try { assertNotNull("No Messages", messages); assertTrue("Incorrect number of messages. Expected 2, Actual " + messages.size(), messages.size() == 2); } finally { app.clear(); } }
Example 3
Source File: LoggerTest.java From logging-log4j2 with Apache License 2.0 | 6 votes |
@Test @SuppressWarnings("deprecation") public void testLog() { final PatternLayout layout = PatternLayout.newBuilder().setPattern("%d %C %L %m").build(); final ListAppender appender = new ListAppender("List", null, layout, false, false); appender.start(); final Logger root = Logger.getRootLogger(); try { ((org.apache.logging.log4j.core.Logger) root.getLogger()).addAppender(appender); root.setLevel(Level.INFO); final MyLogger log = new MyLogger(root); log.logInfo("This is a test", null); root.log(Priority.INFO, "Test msg2", null); root.log(Priority.INFO, "Test msg3"); final List<String> msgs = appender.getMessages(); assertTrue("Incorrect number of messages", msgs.size() == 3); final String msg = msgs.get(0); assertTrue("Message contains incorrect class name: " + msg, msg.contains(LoggerTest.class.getName())); appender.stop(); } finally { ((org.apache.logging.log4j.core.Logger) root.getLogger()).removeAppender(appender); } }
Example 4
Source File: LogWithRouteTest.java From logging-log4j2 with Apache License 2.0 | 6 votes |
@Test public void testMDC() throws Exception { MDC.put("Type", "Service"); MDC.put("Name", "John Smith"); try { final Logger logger = Logger.getLogger("org.apache.test.logging"); logger.debug("This is a test"); final ListAppender listApp = (ListAppender) CTX.getAppender("List"); assertNotNull(listApp); final List<String> msgs = listApp.getMessages(); assertNotNull("No messages received", msgs); assertTrue(msgs.size() == 1); assertTrue("Type is missing", msgs.get(0).contains("Type=Service")); assertTrue("Name is missing", msgs.get(0).contains("Name=John Smith")); } finally { MDC.remove("Type"); MDC.remove("Name"); } }
Example 5
Source File: LogWithMDCTest.java From logging-log4j2 with Apache License 2.0 | 6 votes |
@Test public void testMDC() throws Exception { MDC.put("Key1", "John"); MDC.put("Key2", "Smith"); try { final Logger logger = Logger.getLogger("org.apache.test.logging"); logger.debug("This is a test"); final ListAppender listApp = (ListAppender) CTX.getAppender("List"); assertNotNull(listApp); final List<String> msgs = listApp.getMessages(); assertNotNull("No messages received", msgs); assertTrue(msgs.size() == 1); assertTrue("Key1 is missing", msgs.get(0).contains("Key1=John")); assertTrue("Key2 is missing", msgs.get(0).contains("Key2=Smith")); } finally { MDC.remove("Key1"); MDC.remove("Key2"); } }
Example 6
Source File: PatternSelectorTest.java From logging-log4j2 with Apache License 2.0 | 6 votes |
@Test public void testJavaScriptPatternSelector() throws Exception { final org.apache.logging.log4j.Logger logger = LogManager.getLogger("TestJavaScriptPatternSelector"); final org.apache.logging.log4j.Logger logger2 = LogManager.getLogger("JavascriptNoLocation"); logger.traceEntry(); logger.info("Hello World"); logger2.info("No location information"); logger.traceExit(); final ListAppender app = (ListAppender) context.getRequiredAppender("List3"); assertNotNull("No ListAppender", app); final List<String> messages = app.getMessages(); assertNotNull("No Messages", messages); assertTrue("Incorrect number of messages. Expected 4, Actual " + messages.size() + ": " + messages, messages.size() == 4); String expect = "[TRACE] TestJavaScriptPatternSelector ====== " + "o.a.l.l.c.PatternSelectorTest.testJavaScriptPatternSelector:85 Enter ======" + Strings.LINE_SEPARATOR; assertEquals(expect, messages.get(0)); expect = "[INFO ] TestJavaScriptPatternSelector " + "o.a.l.l.c.PatternSelectorTest.testJavaScriptPatternSelector.86 Hello World" + Strings.LINE_SEPARATOR; assertEquals(expect, messages.get(1)); assertEquals("[INFO ] JavascriptNoLocation No location information" + Strings.LINE_SEPARATOR, messages.get(2)); app.clear(); }
Example 7
Source File: IoBuilderCallerInfoTesting.java From logging-log4j2 with Apache License 2.0 | 5 votes |
public void assertMessages(final String msg, final int size, final String methodName) { final ListAppender appender = ctx.getListAppender("ClassAndMethod"); assertEquals(msg + ".size", size, appender.getMessages().size()); for (final String message : appender.getMessages()) { assertEquals(msg + " has incorrect caller info", this.getClass().getName() + '.' + methodName, message); } }
Example 8
Source File: EnterTagTest.java From logging-log4j2 with Apache License 2.0 | 5 votes |
private void verify(final String expected) { final ListAppender listApp = context.getListAppender("List"); final List<String> events = listApp.getMessages(); try { assertEquals("Incorrect number of messages.", 1, events.size()); assertEquals("Incorrect message.", "o.a.l.l.t.EnterTagTest " + expected, events.get(0)); } finally { listApp.clear(); } }
Example 9
Source File: CallerInformationTest.java From logging-log4j2 with Apache License 2.0 | 5 votes |
@Test public void testClassLogger() throws Exception { final ListAppender app = ctx.getListAppender("Class").clear(); final Logger logger = Logger.getLogger("ClassLogger"); logger.info("Ignored message contents."); logger.warning("Verifying the caller class is still correct."); logger.severe("Hopefully nobody breaks me!"); final List<String> messages = app.getMessages(); assertEquals("Incorrect number of messages.", 3, messages.size()); for (final String message : messages) { assertEquals("Incorrect caller class name.", this.getClass().getName(), message); } }
Example 10
Source File: CallerInformationTest.java From logging-log4j2 with Apache License 2.0 | 5 votes |
@Test public void testClassLogger() throws Exception { final ListAppender app = ctx.getListAppender("Class").clear(); final Logger logger = Logger.getLogger("ClassLogger"); 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.getMessages(); assertEquals("Incorrect number of messages.", 3, messages.size()); for (final String message : messages) { assertEquals("Incorrect caller class name.", this.getClass().getName(), message); } }
Example 11
Source File: CallerInformationTest.java From logging-log4j2 with Apache License 2.0 | 5 votes |
@Test public void testMethodLogger() throws Exception { final ListAppender app = context.getListAppender("Method").clear(); final Logger logger = context.getLogger("MethodLogger"); logger.info("More messages."); logger.warn("CATASTROPHE INCOMING!"); logger.error("ZOMBIES!!!"); logger.fatal("brains~~~"); logger.info("Itchy. Tasty."); final List<String> messages = app.getMessages(); assertEquals("Incorrect number of messages.", 5, messages.size()); for (final String message : messages) { assertEquals("Incorrect caller method name.", "testMethodLogger", message); } }
Example 12
Source File: IoBuilderTest.java From logging-log4j2 with Apache License 2.0 | 5 votes |
@Test public void testNoArgBuilderCallerClassInfo() throws Exception { try (final PrintStream ps = IoBuilder.forLogger().buildPrintStream()) { ps.println("discarded"); final ListAppender app = context.getListAppender("IoBuilderTest"); final List<String> messages = app.getMessages(); assertThat(messages, not(empty())); assertThat(messages, hasSize(1)); final String message = messages.get(0); assertThat(message, startsWith(getClass().getName() + ".testNoArgBuilderCallerClassInfo")); app.clear(); } }
Example 13
Source File: CallerInformationTest.java From logging-log4j2 with Apache License 2.0 | 5 votes |
@Test public void testClassLogger() throws Exception { final ListAppender app = ctx.getListAppender("Class").clear(); final Logger logger = LoggerFactory.getLogger("ClassLogger"); 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.getMessages(); assertEquals("Incorrect number of messages.", 3, messages.size()); for (final String message : messages) { assertEquals("Incorrect caller class name.", this.getClass().getName(), message); } }
Example 14
Source File: OptionalTest.java From logging-log4j2 with Apache License 2.0 | 5 votes |
private void verify(final String name, final String expected) { final ListAppender listApp = CTX.getListAppender(name); final List<String> events = listApp.getMessages(); assertTrue("Incorrect number of messages. Expected 1 Actual " + events.size(), events.size()== 1); final String actual = events.get(0); assertEquals("Incorrect message. Expected " + expected + ". Actual " + actual, expected, actual); listApp.clear(); }
Example 15
Source File: CallerInformationTest.java From logging-log4j2 with Apache License 2.0 | 5 votes |
@Test public void testClassLogger() throws Exception { final ListAppender app = ctx.getListAppender("Class").clear(); final Logger logger = Logger.getLogger("ClassLogger"); 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.getMessages(); assertEquals("Incorrect number of messages.", 3, messages.size()); for (final String message : messages) { assertEquals("Incorrect caller class name.", this.getClass().getName(), message); } }
Example 16
Source File: CallerInformationTest.java From logging-log4j2 with Apache License 2.0 | 5 votes |
@Test public void testClassLogger() throws Exception { final ListAppender app = context.getListAppender("Class").clear(); final Logger logger = context.getLogger("ClassLogger"); 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.getMessages(); assertEquals("Incorrect number of messages.", 3, messages.size()); for (final String message : messages) { assertEquals("Incorrect caller class name.", this.getClass().getName(), message); } }
Example 17
Source File: CallerInformationTest.java From logging-log4j2 with Apache License 2.0 | 5 votes |
@Test public void testMethodLogger() throws Exception { final ListAppender app = ctx.getListAppender("Method").clear(); final Logger logger = LoggerFactory.getLogger("MethodLogger"); logger.info("More messages."); logger.warn("CATASTROPHE INCOMING!"); logger.error("ZOMBIES!!!"); logger.warn("brains~~~"); logger.info("Itchy. Tasty."); final List<String> messages = app.getMessages(); assertEquals("Incorrect number of messages.", 5, messages.size()); for (final String message : messages) { assertEquals("Incorrect caller method name.", "testMethodLogger", message); } }
Example 18
Source File: CsvParameterLayoutTest.java From logging-log4j2 with Apache License 2.0 | 5 votes |
static void testLayoutNormalApi(final Logger root, final AbstractCsvLayout layout, final boolean messageApi) throws Exception { removeAppenders(root); // set up appender final ListAppender appender = new ListAppender("List", null, layout, true, false); appender.start(); appender.countDownLatch = new CountDownLatch(4); // set appender on root and set level to debug root.addAppender(appender); root.setLevel(Level.DEBUG); // output messages if (messageApi) { logDebugObjectArrayMessage(root); } else { logDebugNormalApi(root); } final int msgCount = 4; if (appender.getMessages().size() < msgCount) { // wait until background thread finished processing appender.countDownLatch.await(10, TimeUnit.SECONDS); } assertEquals("Background thread did not finish processing: msg count", msgCount, appender.getMessages().size()); // don't stop appender until background thread is done appender.stop(); final List<String> list = appender.getMessages(); final char d = layout.getFormat().getDelimiter(); Assert.assertEquals("1" + d + "2" + d + "3", list.get(0)); Assert.assertEquals("2" + d + "3", list.get(1)); Assert.assertEquals("5" + d + "6", list.get(2)); Assert.assertEquals("7" + d + "8" + d + "9" + d + "10", list.get(3)); }
Example 19
Source File: XmlLoggerPropsTest.java From logging-log4j2 with Apache License 2.0 | 5 votes |
@Test public void testWithProps() { final ListAppender listAppender = context.getListAppender("List"); assertNotNull("No List Appender", listAppender); try { assertThat(context.getConfiguration(), is(instanceOf(XmlConfiguration.class))); Logger logger = LogManager.getLogger(XmlLoggerPropsTest.class); logger.debug("Test with props"); logger = LogManager.getLogger("tiny.bubbles"); logger.debug("Test on root"); final List<String> events = listAppender.getMessages(); assertTrue("No events", events.size() > 0); assertTrue("Incorrect number of events", events.size() == 2); assertThat(events.get(0), allOf( containsString("user="), containsString("phrasex=****"), containsString("test=test"), containsString("test2=test2default"), containsString("test3=Unknown"), containsString("test4=test"), containsString("test5=test"), containsString("attribKey=attribValue"), containsString("duplicateKey=nodeValue") )); assertThat(events.get(1), allOf( containsString("user="), containsString("phrasex=****"), containsString("test=test"), containsString("test2=test2default"), containsString("test3=Unknown"), containsString("test4=test"), containsString("test5=test"), containsString("attribKey=attribValue"), containsString("duplicateKey=nodeValue") )); } finally { System.clearProperty("test"); } }
Example 20
Source File: CallerInformationTest.java From logging-log4j2 with Apache License 2.0 | 5 votes |
@Test public void testMethodLogger() throws Exception { final ListAppender app = ctx.getListAppender("Method").clear(); final Logger logger = LoggerFactory.getLogger("MethodLogger"); logger.info("More messages."); logger.warn("CATASTROPHE INCOMING!"); logger.error("ZOMBIES!!!"); logger.warn("brains~~~"); logger.info("Itchy. Tasty."); final List<String> messages = app.getMessages(); assertEquals("Incorrect number of messages.", 5, messages.size()); for (final String message : messages) { assertEquals("Incorrect caller method name.", "testMethodLogger", message); } }