Search in sources :

Example 51 with ListAppender

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

the class LoggerTest method verify.

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

Example 52 with ListAppender

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

the class OptionalTest method verify.

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

Example 53 with ListAppender

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

the class YamlLayoutTest method testEscapeLayout.

@Test
public void testEscapeLayout() 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
    final AbstractJacksonLayout layout = YamlLayout.newBuilder().setLocationInfo(true).setProperties(true).setIncludeStacktrace(true).setConfiguration(configuration).build();
    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("Here is a quote ' and then a double quote \"");
    appender.stop();
    final List<String> list = appender.getMessages();
    this.checkAt("---", 0, list);
    this.checkContains("level: \"DEBUG\"", list);
    this.checkContains("message: \"Here is a quote ' and then a double quote \\\"\"", list);
    this.checkContains("loggerFqcn: \"" + AbstractLogger.class.getName() + "\"", 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.core.test.appender.ListAppender) Configuration(org.apache.logging.log4j.core.config.Configuration) ListAppender(org.apache.logging.log4j.core.test.appender.ListAppender) AbstractJacksonLayout(org.apache.logging.log4j.jackson.AbstractJacksonLayout) AbstractLogger(org.apache.logging.log4j.spi.AbstractLogger) Test(org.junit.Test)

Example 54 with ListAppender

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

the class XmlLayoutTest 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);
    }
    // set up appender
    final XmlLayout layout = XmlLayout.newBuilder().setLocationInfo(true).setProperties(true).setComplete(true).setCompact(false).setIncludeStacktrace(true).build();
    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"));
    final Marker marker = MarkerManager.getMarker("EVENT");
    this.rootLogger.error(marker, "marker test");
    appender.stop();
    final List<String> list = appender.getMessages();
    final String string = list.get(0);
    assertTrue("Incorrect header: " + string, string.equals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"));
    assertTrue("Incorrect footer", list.get(list.size() - 1).equals("</Events>"));
    this.checkContains("loggerFqcn=\"" + AbstractLogger.class.getName() + "\"", list);
    this.checkContains("level=\"DEBUG\"", list);
    this.checkContains(">starting mdc pattern test</Message>", list);
    // this.checkContains("<Message>starting mdc pattern test</Message>", list);
    // <Marker xmlns="" _class="org.apache.logging.log4j.MarkerManager..Log4jMarker" name="EVENT"/>
    this.checkContains("<Marker", list);
    this.checkContains("name=\"EVENT\"/>", 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.core.test.appender.ListAppender) ListAppender(org.apache.logging.log4j.core.test.appender.ListAppender) Marker(org.apache.logging.log4j.Marker) AbstractLogger(org.apache.logging.log4j.spi.AbstractLogger) Test(org.junit.Test)

Example 55 with ListAppender

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

the class LoggerTest method testIsTraceEnabled.

/**
 * Tests isTraceEnabled.
 */
@Test
public void testIsTraceEnabled() {
    final ListAppender appender = new ListAppender("List");
    appender.start();
    final Logger root = Logger.getRootLogger();
    try {
        ((org.apache.logging.log4j.core.Logger) root.getLogger()).addAppender(appender);
        root.setLevel(Level.INFO);
        final Logger tracer = Logger.getLogger("com.example.Tracer");
        tracer.setLevel(Level.TRACE);
        assertTrue(tracer.isTraceEnabled());
        assertFalse(root.isTraceEnabled());
        appender.stop();
    } finally {
        ((org.apache.logging.log4j.core.Logger) root.getLogger()).removeAppender(appender);
    }
}
Also used : ListAppender(org.apache.logging.log4j.core.test.appender.ListAppender) Test(org.junit.Test)

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