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