Search in sources :

Example 11 with AbstractJacksonLayout

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);
    }
}
Also used : Appender(org.apache.logging.log4j.core.Appender) ListAppender(org.apache.logging.log4j.core.test.appender.ListAppender) Configuration(org.apache.logging.log4j.core.config.Configuration) ListAppender(org.apache.logging.log4j.core.test.appender.ListAppender) AbstractJacksonLayout(org.apache.logging.log4j.jackson.AbstractJacksonLayout) AbstractLogger(org.apache.logging.log4j.spi.AbstractLogger) Test(org.junit.Test)

Example 12 with AbstractJacksonLayout

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"));
}
Also used : AbstractJacksonLayout(org.apache.logging.log4j.jackson.AbstractJacksonLayout) Test(org.junit.Test)

Example 13 with AbstractJacksonLayout

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());
}
Also used : AbstractJacksonLayout(org.apache.logging.log4j.jackson.AbstractJacksonLayout) Test(org.junit.Test)

Example 14 with AbstractJacksonLayout

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() + "\""));
}
Also used : KeyValuePair(org.apache.logging.log4j.core.util.KeyValuePair) AbstractJacksonLayout(org.apache.logging.log4j.jackson.AbstractJacksonLayout) JavaLookup(org.apache.logging.log4j.core.lookup.JavaLookup) Test(org.junit.Test)

Example 15 with AbstractJacksonLayout

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);
}
Also used : KeyValuePair(org.apache.logging.log4j.core.util.KeyValuePair) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) AbstractJacksonLayout(org.apache.logging.log4j.jackson.AbstractJacksonLayout) MutableLogEvent(org.apache.logging.log4j.core.impl.MutableLogEvent) Test(org.junit.Test)

Aggregations

AbstractJacksonLayout (org.apache.logging.log4j.jackson.AbstractJacksonLayout)32 Test (org.junit.Test)26 Log4jLogEvent (org.apache.logging.log4j.core.impl.Log4jLogEvent)14 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)12 KeyValuePair (org.apache.logging.log4j.core.util.KeyValuePair)7 Log4jJsonObjectMapper (org.apache.logging.log4j.jackson.json.Log4jJsonObjectMapper)5 Appender (org.apache.logging.log4j.core.Appender)4 Configuration (org.apache.logging.log4j.core.config.Configuration)4 MutableLogEvent (org.apache.logging.log4j.core.impl.MutableLogEvent)4 ListAppender (org.apache.logging.log4j.core.test.appender.ListAppender)4 SimpleMessage (org.apache.logging.log4j.message.SimpleMessage)4 AbstractLogger (org.apache.logging.log4j.spi.AbstractLogger)4 JavaLookup (org.apache.logging.log4j.core.lookup.JavaLookup)3 ObjectMessage (org.apache.logging.log4j.message.ObjectMessage)3 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)3 DefaultConfiguration (org.apache.logging.log4j.core.config.DefaultConfiguration)2 Log4jYamlObjectMapper (org.apache.logging.log4j.jackson.yaml.Log4jYamlObjectMapper)2 Message (org.apache.logging.log4j.message.Message)2 RingBufferLogEvent (org.apache.logging.log4j.core.async.RingBufferLogEvent)1 DummyNanoClock (org.apache.logging.log4j.core.time.internal.DummyNanoClock)1