use of org.apache.logging.log4j.test.appender.ListAppender in project logging-log4j2 by apache.
the class CsvParameterLayoutTest method testLogJsonArgument.
@Test
public void testLogJsonArgument() throws InterruptedException {
final ListAppender appender = (ListAppender) init.getAppender("List");
appender.countDownLatch = new CountDownLatch(4);
appender.clear();
final Logger logger = (Logger) LogManager.getRootLogger();
final String json = "{\"id\":10,\"name\":\"Alice\"}";
logger.error("log:{}", json);
// wait until background thread finished processing
final int msgCount = 1;
if (appender.getMessages().size() < msgCount) {
appender.countDownLatch.await(5, 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 String eventStr = list.get(0).toString();
Assert.assertTrue(eventStr, eventStr.contains(json));
}
use of org.apache.logging.log4j.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().withLocationInfo(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("Incorrect number of lines. Require at least 85 " + list.size(), list.size() > 85);
final String string = list.get(3);
assertTrue("Incorrect header: " + string, string.equals("<meta charset=\"UTF-8\"/>"));
assertTrue("Incorrect title", list.get(4).equals("<title>Log4j Log Messages</title>"));
assertTrue("Incorrect footer", list.get(list.size() - 1).equals("</body></html>"));
if (includeLocation) {
assertTrue("Incorrect multiline", list.get(50).equals(multiLine));
assertTrue("Missing location", html.contains("HtmlLayoutTest.java:"));
assertTrue("Incorrect body", list.get(71).equals(body));
} else {
assertFalse("Location should not be in the output table", html.contains("<td>HtmlLayoutTest.java:"));
}
for (final Appender app : appenders.values()) {
root.addAppender(app);
}
}
use of org.apache.logging.log4j.test.appender.ListAppender in project logging-log4j2 by apache.
the class JsonLayoutTest 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 boolean propertiesAsList = false;
// @formatter:off
final AbstractJacksonLayout layout = JsonLayout.newBuilder().setConfiguration(configuration).setLocationInfo(true).setProperties(true).setPropertiesAsList(propertiesAsList).setComplete(true).setCompact(false).setEventEol(false).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("Here is a quote ' and then a double quote \"");
appender.stop();
final List<String> list = appender.getMessages();
this.checkAt("[", 0, list);
this.checkAt("{", 1, 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.test.appender.ListAppender in project logging-log4j2 by apache.
the class Rfc5424LayoutTest method testParameterizedMessage.
@Test
public void testParameterizedMessage() {
for (final Appender appender : root.getAppenders().values()) {
root.removeAppender(appender);
}
// set up appender
final AbstractStringLayout layout = Rfc5424Layout.createLayout(Facility.LOCAL0, "Event", 3692, true, "RequestContext", null, null, true, null, "ATM", null, "key1, key2, locale", null, null, 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);
root.info("Hello {}", "World");
try {
final List<String> list = appender.getMessages();
assertTrue("Not enough list entries", list.size() > 0);
final String message = list.get(0);
assertTrue("Incorrect message. Expected - Hello World, Actual - " + message, message.contains("Hello World"));
} finally {
root.removeAppender(appender);
appender.stop();
}
}
use of org.apache.logging.log4j.test.appender.ListAppender in project logging-log4j2 by apache.
the class SerializedLayoutTest method testLayout.
/**
* Test case for MDC conversion pattern.
*/
@Test
public void testLayout() throws Exception {
final Map<String, Appender> appenders = root.getAppenders();
for (final Appender appender : appenders.values()) {
root.removeAppender(appender);
}
// set up appender
final SerializedLayout layout = SerializedLayout.createLayout();
final ListAppender appender = new ListAppender("List", null, layout, false, true);
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");
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"));
final Exception parent = new IllegalStateException("Test");
final Throwable child = new LoggingException("This is a test", parent);
root.error("Throwing an exception", child);
appender.stop();
final List<byte[]> data = appender.getData();
assertTrue(data.size() > 0);
int i = 0;
for (final byte[] item : data) {
final ByteArrayInputStream bais = new ByteArrayInputStream(item);
final ObjectInputStream ois = new ObjectInputStream(bais);
LogEvent event;
try {
event = (LogEvent) ois.readObject();
} catch (final IOException ioe) {
System.err.println("Exception processing item " + i);
throw ioe;
}
assertTrue("Incorrect event", event.toString().equals(expected[i]));
++i;
}
for (final Appender app : appenders.values()) {
root.addAppender(app);
}
}
Aggregations