use of org.apache.logging.log4j.jackson.AbstractJacksonLayout in project logging-log4j2 by apache.
the class YamlLayoutTest 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 AbstractJacksonLayout layout = YamlLayout.newBuilder().setConfiguration(configuration).setLocationInfo(true).setProperties(true).setHeader("[[".getBytes(StandardCharsets.UTF_8)).setFooter("]]".getBytes(StandardCharsets.UTF_8)).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"));
appender.stop();
final List<String> list = appender.getMessages();
this.checkAt("---", 0, 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.jackson.AbstractJacksonLayout in project logging-log4j2 by apache.
the class YamlLayoutTest method testIncludeNullDelimiterTrue.
@Test
public void testIncludeNullDelimiterTrue() throws Exception {
final AbstractJacksonLayout layout = YamlLayout.newBuilder().setIncludeNullDelimiter(true).build();
final String str = layout.toSerializable(LogEventFixtures.createLogEvent());
assertThat(str, endsWith("\0"));
}
use of org.apache.logging.log4j.jackson.AbstractJacksonLayout in project logging-log4j2 by apache.
the class YamlLayoutTest method testContentType.
@Test
public void testContentType() {
final AbstractJacksonLayout layout = YamlLayout.createDefaultLayout();
assertEquals("application/yaml; charset=UTF-8", layout.getContentType());
}
use of org.apache.logging.log4j.jackson.AbstractJacksonLayout in project logging-log4j2 by apache.
the class YamlLayoutTest method testAdditionalFields.
@Test
public void testAdditionalFields() throws Exception {
final AbstractJacksonLayout layout = YamlLayout.newBuilder().setLocationInfo(false).setProperties(false).setIncludeStacktrace(false).setAdditionalFields(new KeyValuePair[] { new KeyValuePair("KEY1", "VALUE1"), new KeyValuePair("KEY2", "${java:runtime}") }).setCharset(StandardCharsets.UTF_8).setConfiguration(ctx.getConfiguration()).build();
final String str = layout.toSerializable(LogEventFixtures.createLogEvent());
assertThat(str, containsString("KEY1: \"VALUE1\""));
assertThat(str, containsString("KEY2: \"" + new JavaLookup().getRuntime() + "\""));
}
use of org.apache.logging.log4j.jackson.AbstractJacksonLayout in project logging-log4j2 by apache.
the class XmlLayoutTest method testMutableLogEvent.
@Test
public void testMutableLogEvent() throws Exception {
final AbstractJacksonLayout layout = XmlLayout.newBuilder().setLocationInfo(false).setProperties(false).setIncludeStacktrace(false).setAdditionalFields(new KeyValuePair[] { new KeyValuePair("KEY1", "VALUE1"), new KeyValuePair("KEY2", "${java:runtime}") }).setCharset(StandardCharsets.UTF_8).setConfiguration(ctx.getConfiguration()).build();
Log4jLogEvent logEvent = LogEventFixtures.createLogEvent();
final MutableLogEvent mutableEvent = new MutableLogEvent();
mutableEvent.initFrom(logEvent);
final String strLogEvent = layout.toSerializable(logEvent);
final String strMutableEvent = layout.toSerializable(mutableEvent);
assertEquals(strMutableEvent, strLogEvent, strMutableEvent);
}
Aggregations