Java Code Examples for org.apache.logging.log4j.test.appender.ListAppender#clear()

The following examples show how to use org.apache.logging.log4j.test.appender.ListAppender#clear() . 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: AbstractScriptFilterTest.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@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 2
Source File: CsvParameterLayoutTest.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@Test
public void testLogJsonArgument() throws InterruptedException {
    final ListAppender appender = (ListAppender) init.getAppender("List");
    appender.countDownLatch = new CountDownLatch(4);
    appender.clear();
    final Logger logger = (Logger) LogManager.getRootLogger();
    final String json = "{\"id\":10,\"name\":\"Alice\"}";
    logger.error("log:{}", json);
    // wait until background thread finished processing
    final int msgCount = 1;
    if (appender.getMessages().size() < msgCount) {
        appender.countDownLatch.await(5, 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 String eventStr = list.get(0).toString();
    Assert.assertTrue(eventStr, eventStr.contains(json));
}
 
Example 3
Source File: CategoryTest.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@Test
public void testClassName() {
    final Category category = Category.getInstance("TestCategory");
    final Layout<String> layout = PatternLayout.newBuilder().withPattern("%d %p %C{1.} [%t] %m%n").build();
    final ListAppender appender = new ListAppender("List2", null, layout, false, false);
    appender.start();
    category.setAdditivity(false);
    ((org.apache.logging.log4j.core.Logger) category.getLogger()).addAppender(appender);
    category.error("Test Message");
    final List<String> msgs = appender.getMessages();
    assertTrue("Incorrect number of messages. Expected 1 got " + msgs.size(), msgs.size() == 1);
    final String msg = msgs.get(0);
    appender.clear();
    final String threadName = Thread.currentThread().getName();
    final String expected = "ERROR o.a.l.CategoryTest [" + threadName + "] Test Message" + Strings.LINE_SEPARATOR;
    assertTrue("Incorrect message " + Strings.dquote(msg) + " expected " + Strings.dquote(expected), msg.endsWith(expected));
}
 
Example 4
Source File: CategoryTest.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@Test
public void testClassName() {
    final Category category = Category.getInstance("TestCategory");
    final Layout<String> layout = PatternLayout.newBuilder().setPattern("%d %p %C{1.} [%t] %m%n").build();
    final ListAppender appender = new ListAppender("List2", null, layout, false, false);
    appender.start();
    category.setAdditivity(false);
    ((org.apache.logging.log4j.core.Logger) category.getLogger()).addAppender(appender);
    category.error("Test Message");
    final List<String> msgs = appender.getMessages();
    assertTrue("Incorrect number of messages. Expected 1 got " + msgs.size(), msgs.size() == 1);
    final String msg = msgs.get(0);
    appender.clear();
    final String threadName = Thread.currentThread().getName();
    final String expected = "ERROR o.a.l.CategoryTest [" + threadName + "] Test Message" + Strings.LINE_SEPARATOR;
    assertTrue("Incorrect message " + Strings.dquote(msg) + " expected " + Strings.dquote(expected), msg.endsWith(expected));
}
 
Example 5
Source File: AbstractScriptFilterTest.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@Test
public void testGroovyFilter() throws Exception {
    final Logger logger = LogManager.getLogger("TestGroovyFilter");
    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");
    try {
        final List<String> messages = app.getMessages();
        assertNotNull("No Messages", messages);
        assertTrue("Incorrect number of messages. Expected 2, Actual " + messages.size(), messages.size() == 2);
    } finally {
        app.clear();
    }
}
 
Example 6
Source File: PatternSelectorTest.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@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: PatternSelectorTest.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@Test
public void testScriptPatternSelector() throws Exception {
    final org.apache.logging.log4j.Logger logger = LogManager.getLogger("TestScriptPatternSelector");
    final org.apache.logging.log4j.Logger logger2 = LogManager.getLogger("NoLocation");
    logger.traceEntry();
    logger.info("Hello World");
    logger2.info("No location information");
    logger.traceExit();
    final ListAppender app = (ListAppender) context.getRequiredAppender("List2");
    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] TestScriptPatternSelector ====== " +
            "o.a.l.l.c.PatternSelectorTest.testScriptPatternSelector:62 Enter ======" + Strings.LINE_SEPARATOR;
    assertEquals(expect, messages.get(0));
    expect = "[INFO ] TestScriptPatternSelector o.a.l.l.c.PatternSelectorTest.testScriptPatternSelector.63 " +
            "Hello World" + Strings.LINE_SEPARATOR;
    assertEquals(expect, messages.get(1));
    assertEquals("[INFO ] NoLocation No location information" + Strings.LINE_SEPARATOR, messages.get(2));
    app.clear();
}
 
Example 8
Source File: PatternSelectorTest.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@Test
public void testMarkerPatternSelector() throws Exception {
    final org.apache.logging.log4j.Logger logger = LogManager.getLogger("TestMarkerPatternSelector");
    logger.traceEntry();
    logger.info("Hello World");
    logger.traceExit();
    final ListAppender app = (ListAppender) context.getRequiredAppender("List");
    assertNotNull("No ListAppender", app);
    final List<String> messages = app.getMessages();
    assertNotNull("No Messages", messages);
    assertTrue("Incorrect number of messages. Expected 3, Actual " + messages.size() + ": " + messages, messages.size() == 3);
    final String expect = String.format("[TRACE] TestMarkerPatternSelector ====== "
            + "o.a.l.l.c.PatternSelectorTest.testMarkerPatternSelector:43 Enter ======%n");
    assertEquals(expect, messages.get(0));
    assertEquals("[INFO ] TestMarkerPatternSelector Hello World" + Strings.LINE_SEPARATOR, messages.get(1));
    app.clear();
}
 
Example 9
Source File: LoggingTest.java    From gcp-ingestion with Mozilla Public License 2.0 6 votes vote down vote up
@Test
public void canWriteLogs() {
  final ListAppender appender = logs.getListAppender("STDOUT");

  // emit warning with exception from an async CompletableFuture
  CompletableFuture.runAsync(() -> LoggerFactory.getLogger(LoggingTest.class).warn("msg",
      new UncheckedIOException(new IOException("test"))), ForkJoinPool.commonPool()).join();
  assertThat(appender.getMessages(), containsInAnyOrder(allOf(containsString("LoggingTest"),
      containsString("\"level\":\"WARN\""), containsString("\"message\":\"msg\""), containsString(
          "\"extendedStackTrace\":\"java.io.UncheckedIOException: java.io.IOException: test"))));

  // emit error without exception from an async CompletableFuture
  appender.clear();
  LoggerFactory.getLogger(LoggingTest.class).error("test message");
  assertThat(appender.getMessages(),
      containsInAnyOrder(allOf(containsString("LoggingTest"),
          containsString("\"level\":\"ERROR\""), containsString("\"message\":\"test message\""),
          not(containsString("extendedStackTrace")))));
}
 
Example 10
Source File: OptionalTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
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 11
Source File: PropertyTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Test
public void testEmptyAttribute() throws Exception {
    final org.apache.logging.log4j.Logger logger = LogManager.getLogger();
    logger.info("msg");

    final ListAppender app = (ListAppender) context.getRequiredAppender("List");
    assertNotNull("No ListAppender", app);

    final List<String> messages = app.getMessages();
    assertNotNull("No Messages", messages);
    assertEquals("message count" + messages, 1, messages.size());

    //<Property name="emptyElementKey" />
    //<Property name="emptyAttributeKey" value="" />
    //<Property name="emptyAttributeKey2" value=""></Property>
    //<Property name="elementKey">elementValue</Property>
    //<Property name="attributeKey" value="attributeValue" />
    //<Property name="attributeWithEmptyElementKey" value="attributeValue2"></Property>
    //<Property name="bothElementAndAttributeKey" value="attributeValue3">elementValue3</Property>
    final String expect = "1=elementValue" + // ${sys:elementKey}
            ",2=" + // ${sys:emptyElementKey}
            ",a=" + // ${sys:emptyAttributeKey}
            ",b=" + // ${sys:emptyAttributeKey2}
            ",3=attributeValue" + // ${sys:attributeKey}
            ",4=attributeValue2" + // ${sys:attributeWithEmptyElementKey}
            ",5=elementValue3,m=msg"; // ${sys:bothElementAndAttributeKey}
    assertEquals(expect, messages.get(0));
    app.clear();
}
 
Example 12
Source File: LoggingTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
private void verify(final String expected) {
    final ListAppender listApp = ListAppender.getListAppender("List");
    assertNotNull("Missing Appender", listApp);
    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 13
Source File: LoggerTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
private void verify(final String name, final String expected) {
    final ListAppender listApp = context.getListAppender(name);
    final List<String> events = listApp.getMessages();
    assertThat(events, hasSize(1));
    final String actual = events.get(0);
    assertThat(actual, equalTo(expected));
    listApp.clear();
}
 
Example 14
Source File: LoggerTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
private void verify(final String name, final String expected) {
    final ListAppender listApp = ctx.getListAppender(name);
    assertNotNull("Missing Appender", listApp);
    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: IoBuilderTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@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 16
Source File: ExitTagTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
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.ExitTagTest " + expected, events.get(0));
    }
    finally
    {
        listApp.clear();
    }
}
 
Example 17
Source File: LoggingMessageTagSupportTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
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.LoggingMessageTagSupportTest " + expected, events.get(0));
    }
    finally
    {
        listApp.clear();
    }
}
 
Example 18
Source File: EnterTagTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
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 19
Source File: CatchingTagTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
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.CatchingTagTest " + expected, events.get(0));
    }
    finally
    {
        listApp.clear();
    }
}
 
Example 20
Source File: LoggerTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
private void verify(final String name, final String expected) {
    final ListAppender listApp = ctx.getListAppender(name);
    assertNotNull("Missing Appender", listApp);
    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();
}