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