Search in sources :

Example 26 with ListAppender

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

the class CallerInformationTest method testMethodLogger.

@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);
    }
}
Also used : ListAppender(org.apache.logging.log4j.test.appender.ListAppender) Logger(org.slf4j.Logger) Test(org.junit.Test)

Example 27 with ListAppender

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

the class RoutesScriptAppenderTest 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 : AppenderControl(org.apache.logging.log4j.core.config.AppenderControl) ListAppender(org.apache.logging.log4j.test.appender.ListAppender)

Example 28 with ListAppender

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

the class JsonLayoutTest method testLayout.

/**
     * Test case for MDC conversion pattern.
     */
@Test
public void testLayout() throws Exception {
    final Map<String, Appender> appenders = this.rootLogger.getAppenders();
    for (final Appender appender : appenders.values()) {
        this.rootLogger.removeAppender(appender);
    }
    final Configuration configuration = rootLogger.getContext().getConfiguration();
    // set up appender
    // Use [[ and ]] to test header and footer (instead of [ and ])
    final boolean propertiesAsList = false;
    // @formatter:off
    final AbstractJacksonLayout layout = JsonLayout.newBuilder().setConfiguration(configuration).setLocationInfo(true).setProperties(true).setPropertiesAsList(propertiesAsList).setComplete(true).setCompact(false).setEventEol(false).setHeader("[[".getBytes(Charset.defaultCharset())).setFooter("]]".getBytes(Charset.defaultCharset())).setIncludeStacktrace(true).build();
    // @formatter:on
    final ListAppender appender = new ListAppender("List", null, layout, true, false);
    appender.start();
    // set appender on root and set level to debug
    this.rootLogger.addAppender(appender);
    this.rootLogger.setLevel(Level.DEBUG);
    // output starting message
    this.rootLogger.debug("starting mdc pattern test");
    this.rootLogger.debug("empty mdc");
    ThreadContext.put("key1", "value1");
    ThreadContext.put("key2", "value2");
    this.rootLogger.debug("filled mdc");
    ThreadContext.remove("key1");
    ThreadContext.remove("key2");
    this.rootLogger.error("finished mdc pattern test", new NullPointerException("test"));
    appender.stop();
    final List<String> list = appender.getMessages();
    this.checkAt("[[", 0, list);
    this.checkAt("{", 1, list);
    this.checkContains("\"loggerFqcn\" : \"" + AbstractLogger.class.getName() + "\",", list);
    this.checkContains("\"level\" : \"DEBUG\",", list);
    this.checkContains("\"message\" : \"starting mdc pattern test\",", list);
    for (final Appender app : appenders.values()) {
        this.rootLogger.addAppender(app);
    }
}
Also used : Appender(org.apache.logging.log4j.core.Appender) ListAppender(org.apache.logging.log4j.test.appender.ListAppender) Configuration(org.apache.logging.log4j.core.config.Configuration) ListAppender(org.apache.logging.log4j.test.appender.ListAppender) AbstractLogger(org.apache.logging.log4j.spi.AbstractLogger) Test(org.junit.Test)

Example 29 with ListAppender

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

the class PatternLayoutLookupDateTest method testDateLookupInMessage.

@Test
public void testDateLookupInMessage() {
    final String template = "${date:YYYY-MM-dd}";
    context.getLogger(PatternLayoutLookupDateTest.class.getName()).info(template);
    final ListAppender listAppender = context.getListAppender("List");
    final String string = listAppender.getMessages().get(0);
    Assert.assertFalse(string, string.contains(template));
}
Also used : ListAppender(org.apache.logging.log4j.test.appender.ListAppender) Test(org.junit.Test)

Example 30 with ListAppender

use of org.apache.logging.log4j.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.test.appender.ListAppender) ListAppender(org.apache.logging.log4j.test.appender.ListAppender)

Aggregations

ListAppender (org.apache.logging.log4j.test.appender.ListAppender)64 Test (org.junit.Test)46 Appender (org.apache.logging.log4j.core.Appender)17 Logger (org.apache.logging.log4j.Logger)8 LogEvent (org.apache.logging.log4j.core.LogEvent)6 Configuration (org.apache.logging.log4j.core.config.Configuration)5 AbstractLogger (org.apache.logging.log4j.spi.AbstractLogger)5 Marker (org.apache.logging.log4j.Marker)4 Logger (org.apache.logging.log4j.core.Logger)4 KeyValuePair (org.apache.logging.log4j.core.util.KeyValuePair)4 StructuredDataMessage (org.apache.logging.log4j.message.StructuredDataMessage)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 IOException (java.io.IOException)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 Logger (java.util.logging.Logger)2 Log (org.apache.commons.logging.Log)2 AppenderControl (org.apache.logging.log4j.core.config.AppenderControl)2 Logger (org.slf4j.Logger)2 InputStream (java.io.InputStream)1 ObjectInputStream (java.io.ObjectInputStream)1