Search in sources :

Example 1 with ListAppender

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

the class TestHelpers method withContextFromTemplate.

/**
 * Provides a configuration using the given JSON event template to the given consumer.
 * <p>
 * A {@link ListAppender} (named {@code List}) wrapping the layout is used to store the log events.
 * The root logger level is set to {@link Level#ALL}.
 * </p>
 * @param configName a configuration name
 * @param eventTemplate a JSON template
 * @param consumer a consumer accepting the created logger context and the list appender
 */
public static void withContextFromTemplate(final String configName, final Object eventTemplate, final BiConsumer<LoggerContext, ListAppender> consumer) {
    // Create the configuration builder.
    final ConfigurationBuilder<BuiltConfiguration> configBuilder = ConfigurationBuilderFactory.newConfigurationBuilder().setStatusLevel(Level.ERROR).setConfigurationName(configName);
    // Create the configuration.
    final String eventTemplateJson = writeJson(eventTemplate);
    final String appenderName = "List";
    Configuration config = configBuilder.add(configBuilder.newAppender(appenderName, "List").addAttribute("raw", true).add(configBuilder.newLayout("JsonTemplateLayout").addAttribute("eventTemplate", eventTemplateJson))).add(configBuilder.newRootLogger(Level.ALL).add(configBuilder.newAppenderRef(appenderName))).build(false);
    // Initialize the configuration and pass it to the consumer.
    try (final LoggerContext loggerContext = Configurator.initialize(config)) {
        final ListAppender appender = loggerContext.getConfiguration().getAppender("List");
        consumer.accept(loggerContext, appender);
    }
}
Also used : DefaultConfiguration(org.apache.logging.log4j.core.config.DefaultConfiguration) Configuration(org.apache.logging.log4j.core.config.Configuration) BuiltConfiguration(org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration) ListAppender(org.apache.logging.log4j.core.test.appender.ListAppender) BuiltConfiguration(org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration) LoggerContext(org.apache.logging.log4j.core.LoggerContext)

Example 2 with ListAppender

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

the class LoggingTest method verify.

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

Example 3 with ListAppender

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

the class RoutingAppenderWithPurgingTest method routingTest.

@Test(timeout = 5000)
public void routingTest() throws InterruptedException {
    StructuredDataMessage msg = new StructuredDataMessage("1", "This is a test 1", "Service");
    EventLogger.logEvent(msg);
    final List<LogEvent> list = app.getEvents();
    assertNotNull("No events generated", list);
    assertEquals("Incorrect number of events. Expected 1, got " + list.size(), 1, list.size());
    msg = new StructuredDataMessage("2", "This is a test 2", "Service");
    EventLogger.logEvent(msg);
    msg = new StructuredDataMessage("3", "This is a test 3", "Service");
    EventLogger.logEvent(msg);
    // '2' is a referenced list appender
    final String[] files = { IDLE_LOG_FILE1, IDLE_LOG_FILE3, MANUAL_LOG_FILE1, MANUAL_LOG_FILE3 };
    assertFileExistance(files);
    Set<String> expectedAppenderKeys = new HashSet<>(2);
    expectedAppenderKeys.add("1");
    expectedAppenderKeys.add("3");
    assertEquals(expectedAppenderKeys, routingAppenderManual.getAppenders().keySet());
    assertFalse(((ListAppender) loggerContextRule.getAppender("ReferencedList")).getEvents().isEmpty());
    assertEquals("Incorrect number of appenders with IdlePurgePolicy.", 2, routingAppenderIdle.getAppenders().size());
    assertEquals("Incorrect number of appenders with IdlePurgePolicy with HangingAppender.", 2, routingAppenderIdleWithHangingAppender.getAppenders().size());
    assertEquals("Incorrect number of appenders manual purge.", 2, routingAppenderManual.getAppenders().size());
    Thread.sleep(3000);
    EventLogger.logEvent(msg);
    assertEquals("Incorrect number of appenders with IdlePurgePolicy.", 1, routingAppenderIdle.getAppenders().size());
    assertEquals("Incorrect number of appenders with manual purge.", 2, routingAppenderManual.getAppenders().size());
    routingAppenderManual.deleteAppender("1");
    routingAppenderManual.deleteAppender("2");
    routingAppenderManual.deleteAppender("3");
    assertEquals("Incorrect number of appenders with IdlePurgePolicy.", 1, routingAppenderIdle.getAppenders().size());
    assertEquals("Incorrect number of appenders with manual purge.", 0, routingAppenderManual.getAppenders().size());
    assertFalse("Reference based routes should not be stoppable", loggerContextRule.getAppender("ReferencedList").isStopped());
    msg = new StructuredDataMessage("5", "This is a test 5", "Service");
    EventLogger.logEvent(msg);
    assertEquals("Incorrect number of appenders with manual purge.", 1, routingAppenderManual.getAppenders().size());
    routingAppenderManual.deleteAppender("5");
    routingAppenderManual.deleteAppender("5");
    assertEquals("Incorrect number of appenders with manual purge.", 0, routingAppenderManual.getAppenders().size());
}
Also used : StructuredDataMessage(org.apache.logging.log4j.message.StructuredDataMessage) LogEvent(org.apache.logging.log4j.core.LogEvent) ListAppender(org.apache.logging.log4j.core.test.appender.ListAppender) HashSet(java.util.HashSet) Test(org.junit.Test)

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

Example 5 with ListAppender

use of org.apache.logging.log4j.core.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 Log logger = LogFactory.getLog("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 : Log(org.apache.commons.logging.Log) 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