Search in sources :

Example 41 with ListAppender

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

the class CsvParameterLayoutTest method testLogJsonArgument.

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

Example 42 with ListAppender

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

the class Log4j2SpringBootInitTest method testEnvironment.

@Test
public void testEnvironment() {
    LoggerContext context = (LoggerContext) LogManager.getContext(false);
    ListAppender app = context.getConfiguration().getAppender("Out");
    assertNotNull(app);
    assertEquals(1, app.getMessages().size());
    assertEquals("Started: log4j-spring-boot", app.getMessages().get(0));
}
Also used : ListAppender(org.apache.logging.log4j.core.test.appender.ListAppender) LoggerContext(org.apache.logging.log4j.core.LoggerContext) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 43 with ListAppender

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

the class CatchingTagTest method verify.

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

Example 44 with ListAppender

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

the class SpringProfileTest method prodTest.

@Test
public void prodTest() {
    env.setActiveProfiles("prod");
    loggerContext.setConfigLocation(new File(CONFIG).toURI());
    assertNotNull(loggerContext);
    Appender app = loggerContext.getConfiguration().getAppender("Out");
    assertNotNull(app);
    assertTrue(app instanceof ListAppender);
}
Also used : ConsoleAppender(org.apache.logging.log4j.core.appender.ConsoleAppender) ListAppender(org.apache.logging.log4j.core.test.appender.ListAppender) Appender(org.apache.logging.log4j.core.Appender) ListAppender(org.apache.logging.log4j.core.test.appender.ListAppender) File(java.io.File) Test(org.junit.Test)

Example 45 with ListAppender

use of org.apache.logging.log4j.core.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);
    return (ListAppender) appenderControl.getAppender();
}
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)

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