Search in sources :

Example 31 with ListAppender

use of org.apache.logging.log4j.core.test.appender.ListAppender in project logging-log4j2 by apache.

the class Rfc5424LayoutTest method testEscape.

/**
 * Test case for escaping newlines and other SD PARAM-NAME special characters.
 */
@Test
public void testEscape() throws Exception {
    for (final Appender appender : root.getAppenders().values()) {
        root.removeAppender(appender);
    }
    // set up layout/appender
    final AbstractStringLayout layout = Rfc5424Layout.createLayout(Facility.LOCAL0, "Event", 3692, true, "RequestContext", null, null, true, "#012", "ATM", null, "key1, key2, locale", null, "loginId", null, true, null, null);
    final ListAppender appender = new ListAppender("List", null, layout, true, false);
    appender.start();
    // set appender on root and set level to debug
    root.addAppender(appender);
    root.setLevel(Level.DEBUG);
    ThreadContext.put("loginId", "JohnDoe");
    // output starting message
    root.debug("starting mdc pattern test");
    root.debug("empty mdc");
    ThreadContext.put("escaped", "Testing escaping \n \" ] \"");
    root.debug("filled mdc");
    ThreadContext.put("ipAddress", "192.168.0.120");
    ThreadContext.put("locale", Locale.US.getDisplayName());
    try {
        final StructuredDataMessage msg = new StructuredDataMessage("Transfer@18060", "Transfer Complete", "Audit");
        msg.put("ToAccount", "123456");
        msg.put("FromAccount", "123457");
        msg.put("Amount", "200.00");
        root.info(MarkerManager.getMarker("EVENT"), msg);
        List<String> list = appender.getMessages();
        assertTrue(list.get(0).endsWith(line1), "Expected line 1 to end with: " + line1 + " Actual " + list.get(0));
        assertTrue(list.get(1).endsWith(line2), "Expected line 2 to end with: " + line2 + " Actual " + list.get(1));
        assertTrue(list.get(2).endsWith(lineEscaped3), "Expected line 3 to end with: " + lineEscaped3 + " Actual " + list.get(2));
        assertTrue(list.get(3).endsWith(lineEscaped4), "Expected line 4 to end with: " + lineEscaped4 + " Actual " + list.get(3));
        appender.clear();
        ThreadContext.remove("loginId");
        root.debug("This is a test");
        list = appender.getMessages();
        assertTrue(list.isEmpty(), "No messages expected, found " + list.size());
    } finally {
        root.removeAppender(appender);
        appender.stop();
    }
}
Also used : Appender(org.apache.logging.log4j.core.Appender) ListAppender(org.apache.logging.log4j.core.test.appender.ListAppender) StructuredDataMessage(org.apache.logging.log4j.message.StructuredDataMessage) ListAppender(org.apache.logging.log4j.core.test.appender.ListAppender) Test(org.junit.jupiter.api.Test)

Example 32 with ListAppender

use of org.apache.logging.log4j.core.test.appender.ListAppender in project logging-log4j2 by apache.

the class DefaultRouteScriptAppenderTest method getListAppender.

private ListAppender getListAppender() {
    final String key = "Service2";
    final RoutingAppender routingAppender = getRoutingAppender();
    Assert.assertTrue(routingAppender.isStarted());
    final Map<String, AppenderControl> appenders = routingAppender.getAppenders();
    final AppenderControl appenderControl = appenders.get(key);
    assertNotNull("No appender control generated for '" + key + "'; appenders = " + appenders, appenderControl);
    final ListAppender listAppender = (ListAppender) appenderControl.getAppender();
    return listAppender;
}
Also used : RoutingAppender(org.apache.logging.log4j.core.appender.routing.RoutingAppender) AppenderControl(org.apache.logging.log4j.core.config.AppenderControl) ListAppender(org.apache.logging.log4j.core.test.appender.ListAppender)

Example 33 with ListAppender

use of org.apache.logging.log4j.core.test.appender.ListAppender in project logging-log4j2 by apache.

the class CsvLogEventLayoutTest method testLayout.

private void testLayout(final CSVFormat format, final AbstractCsvLayout layout, final String header, final String footer) {
    final Map<String, Appender> appenders = root.getAppenders();
    for (final Appender appender : appenders.values()) {
        root.removeAppender(appender);
    }
    // set up appender
    final ListAppender appender = new ListAppender("List", null, layout, true, false);
    appender.start();
    // set appender on root and set level to debug
    root.addAppender(appender);
    root.setLevel(Level.DEBUG);
    root.debug("one={}, two={}, three={}", 1, 2, 3);
    root.info("Hello");
    appender.stop();
    final List<String> list = appender.getMessages();
    final boolean hasHeaderSerializer = layout.getHeaderSerializer() != null;
    final boolean hasFooterSerializer = layout.getFooterSerializer() != null;
    final int headerOffset = hasHeaderSerializer ? 1 : 0;
    final String event0 = list.get(0 + headerOffset);
    final String event1 = list.get(1 + headerOffset);
    final char del = format.getDelimiter();
    Assert.assertTrue(event0, event0.contains(del + "DEBUG" + del));
    final String quote = del == ',' ? "\"" : "";
    Assert.assertTrue(event0, event0.contains(del + quote + "one=1, two=2, three=3" + quote + del));
    Assert.assertTrue(event1, event1.contains(del + "INFO" + del));
    if (hasHeaderSerializer && header == null) {
        Assert.fail();
    }
    if (!hasHeaderSerializer && header != null) {
        Assert.fail();
    }
    if (hasFooterSerializer && footer == null) {
        Assert.fail();
    }
    if (!hasFooterSerializer && footer != null) {
        Assert.fail();
    }
    if (hasHeaderSerializer) {
        Assert.assertEquals(list.toString(), header, list.get(0));
    }
    if (hasFooterSerializer) {
        Assert.assertEquals(list.toString(), footer, list.get(list.size() - 1));
    }
}
Also used : Appender(org.apache.logging.log4j.core.Appender) ListAppender(org.apache.logging.log4j.core.test.appender.ListAppender) ListAppender(org.apache.logging.log4j.core.test.appender.ListAppender)

Example 34 with ListAppender

use of org.apache.logging.log4j.core.test.appender.ListAppender in project logging-log4j2 by apache.

the class CsvParameterLayoutTest method testLayoutNormalApi.

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));
}
Also used : ListAppender(org.apache.logging.log4j.core.test.appender.ListAppender) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 35 with ListAppender

use of org.apache.logging.log4j.core.test.appender.ListAppender in project logging-log4j2 by apache.

the class HtmlLayoutTest method testLayout.

private void testLayout(final boolean includeLocation) throws Exception {
    final Map<String, Appender> appenders = root.getAppenders();
    for (final Appender appender : appenders.values()) {
        root.removeAppender(appender);
    }
    // set up appender
    final HtmlLayout layout = HtmlLayout.newBuilder().setLocationInfo(includeLocation).build();
    final ListAppender appender = new ListAppender("List", null, layout, true, false);
    appender.start();
    // set appender on root and set level to debug
    root.addAppender(appender);
    root.setLevel(Level.DEBUG);
    // output starting message
    root.debug("starting mdc pattern test");
    root.debug("empty mdc");
    root.debug("First line\nSecond line");
    ThreadContext.put("key1", "value1");
    ThreadContext.put("key2", "value2");
    root.debug("filled mdc");
    ThreadContext.remove("key1");
    ThreadContext.remove("key2");
    root.error("finished mdc pattern test", new NullPointerException("test"));
    appender.stop();
    final List<String> list = appender.getMessages();
    final StringBuilder sb = new StringBuilder();
    for (final String string : list) {
        sb.append(string);
    }
    final String html = sb.toString();
    assertTrue(list.size() > 85, "Incorrect number of lines. Require at least 85 " + list.size());
    final String string = list.get(3);
    assertEquals("<meta charset=\"UTF-8\"/>", string, "Incorrect header: " + string);
    assertEquals("<title>Log4j Log Messages</title>", list.get(4), "Incorrect title");
    assertEquals("</body></html>", list.get(list.size() - 1), "Incorrect footer");
    if (includeLocation) {
        assertEquals(list.get(50), multiLine, "Incorrect multiline");
        assertTrue(html.contains("HtmlLayoutTest.java:"), "Missing location");
        assertEquals(list.get(71), body, "Incorrect body");
    } else {
        assertFalse(html.contains("<td>HtmlLayoutTest.java:"), "Location should not be in the output table");
    }
    for (final Appender app : appenders.values()) {
        root.addAppender(app);
    }
}
Also used : Appender(org.apache.logging.log4j.core.Appender) ListAppender(org.apache.logging.log4j.core.test.appender.ListAppender) ListAppender(org.apache.logging.log4j.core.test.appender.ListAppender)

Aggregations

ListAppender (org.apache.logging.log4j.core.test.appender.ListAppender)60 Test (org.junit.Test)24 Appender (org.apache.logging.log4j.core.Appender)21 Test (org.junit.jupiter.api.Test)15 Configuration (org.apache.logging.log4j.core.config.Configuration)6 StructuredDataMessage (org.apache.logging.log4j.message.StructuredDataMessage)5 AbstractLogger (org.apache.logging.log4j.spi.AbstractLogger)5 ConsoleAppender (org.apache.logging.log4j.core.appender.ConsoleAppender)4 KeyValuePair (org.apache.logging.log4j.core.util.KeyValuePair)4 AbstractJacksonLayout (org.apache.logging.log4j.jackson.AbstractJacksonLayout)4 Marker (org.apache.logging.log4j.Marker)3 LogEvent (org.apache.logging.log4j.core.LogEvent)3 Logger (org.apache.logging.log4j.core.Logger)3 DefaultConfiguration (org.apache.logging.log4j.core.config.DefaultConfiguration)3 ExtendedLogger (org.apache.logging.log4j.spi.ExtendedLogger)3 CountDownLatch (java.util.concurrent.CountDownLatch)2 Logger (java.util.logging.Logger)2 Log (org.apache.commons.logging.Log)2 LoggerContext (org.apache.logging.log4j.core.LoggerContext)2 RoutingAppender (org.apache.logging.log4j.core.appender.routing.RoutingAppender)2