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);
}
}
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();
}
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());
}
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();
}
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);
}
}
Aggregations