use of org.apache.logging.log4j.core.test.appender.ListAppender in project logging-log4j2 by apache.
the class Rfc5424LayoutTest method testEscape.
/**
* Test case for escaping newlines and other SD PARAM-NAME special characters.
*/
@Test
public void testEscape() throws Exception {
for (final Appender appender : root.getAppenders().values()) {
root.removeAppender(appender);
}
// set up layout/appender
final AbstractStringLayout layout = Rfc5424Layout.createLayout(Facility.LOCAL0, "Event", 3692, true, "RequestContext", null, null, true, "#012", "ATM", null, "key1, key2, locale", null, "loginId", null, true, null, null);
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);
ThreadContext.put("loginId", "JohnDoe");
// output starting message
root.debug("starting mdc pattern test");
root.debug("empty mdc");
ThreadContext.put("escaped", "Testing escaping \n \" ] \"");
root.debug("filled mdc");
ThreadContext.put("ipAddress", "192.168.0.120");
ThreadContext.put("locale", Locale.US.getDisplayName());
try {
final StructuredDataMessage msg = new StructuredDataMessage("Transfer@18060", "Transfer Complete", "Audit");
msg.put("ToAccount", "123456");
msg.put("FromAccount", "123457");
msg.put("Amount", "200.00");
root.info(MarkerManager.getMarker("EVENT"), msg);
List<String> list = appender.getMessages();
assertTrue(list.get(0).endsWith(line1), "Expected line 1 to end with: " + line1 + " Actual " + list.get(0));
assertTrue(list.get(1).endsWith(line2), "Expected line 2 to end with: " + line2 + " Actual " + list.get(1));
assertTrue(list.get(2).endsWith(lineEscaped3), "Expected line 3 to end with: " + lineEscaped3 + " Actual " + list.get(2));
assertTrue(list.get(3).endsWith(lineEscaped4), "Expected line 4 to end with: " + lineEscaped4 + " Actual " + list.get(3));
appender.clear();
ThreadContext.remove("loginId");
root.debug("This is a test");
list = appender.getMessages();
assertTrue(list.isEmpty(), "No messages expected, found " + list.size());
} finally {
root.removeAppender(appender);
appender.stop();
}
}
use of org.apache.logging.log4j.core.test.appender.ListAppender in project logging-log4j2 by apache.
the class DefaultRouteScriptAppenderTest 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.core.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));
}
}
use of org.apache.logging.log4j.core.test.appender.ListAppender in project logging-log4j2 by apache.
the class CsvParameterLayoutTest method testLayoutNormalApi.
static void testLayoutNormalApi(final Logger root, final AbstractCsvLayout layout, final boolean messageApi) throws Exception {
removeAppenders(root);
// set up appender
final ListAppender appender = new ListAppender("List", null, layout, true, false);
appender.start();
appender.countDownLatch = new CountDownLatch(4);
// set appender on root and set level to debug
root.addAppender(appender);
root.setLevel(Level.DEBUG);
// output messages
if (messageApi) {
logDebugObjectArrayMessage(root);
} else {
logDebugNormalApi(root);
}
final int msgCount = 4;
if (appender.getMessages().size() < msgCount) {
// wait until background thread finished processing
appender.countDownLatch.await(10, 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 char d = layout.getFormat().getDelimiter();
Assert.assertEquals("1" + d + "2" + d + "3", list.get(0));
Assert.assertEquals("2" + d + "3", list.get(1));
Assert.assertEquals("5" + d + "6", list.get(2));
Assert.assertEquals("7" + d + "8" + d + "9" + d + "10", list.get(3));
}
use of org.apache.logging.log4j.core.test.appender.ListAppender in project logging-log4j2 by apache.
the class HtmlLayoutTest method testLayout.
private void testLayout(final boolean includeLocation) throws Exception {
final Map<String, Appender> appenders = root.getAppenders();
for (final Appender appender : appenders.values()) {
root.removeAppender(appender);
}
// set up appender
final HtmlLayout layout = HtmlLayout.newBuilder().setLocationInfo(includeLocation).build();
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);
// output starting message
root.debug("starting mdc pattern test");
root.debug("empty mdc");
root.debug("First line\nSecond line");
ThreadContext.put("key1", "value1");
ThreadContext.put("key2", "value2");
root.debug("filled mdc");
ThreadContext.remove("key1");
ThreadContext.remove("key2");
root.error("finished mdc pattern test", new NullPointerException("test"));
appender.stop();
final List<String> list = appender.getMessages();
final StringBuilder sb = new StringBuilder();
for (final String string : list) {
sb.append(string);
}
final String html = sb.toString();
assertTrue(list.size() > 85, "Incorrect number of lines. Require at least 85 " + list.size());
final String string = list.get(3);
assertEquals("<meta charset=\"UTF-8\"/>", string, "Incorrect header: " + string);
assertEquals("<title>Log4j Log Messages</title>", list.get(4), "Incorrect title");
assertEquals("</body></html>", list.get(list.size() - 1), "Incorrect footer");
if (includeLocation) {
assertEquals(list.get(50), multiLine, "Incorrect multiline");
assertTrue(html.contains("HtmlLayoutTest.java:"), "Missing location");
assertEquals(list.get(71), body, "Incorrect body");
} else {
assertFalse(html.contains("<td>HtmlLayoutTest.java:"), "Location should not be in the output table");
}
for (final Appender app : appenders.values()) {
root.addAppender(app);
}
}
Aggregations