Search in sources :

Example 16 with AbstractJacksonLayout

use of org.apache.logging.log4j.jackson.AbstractJacksonLayout in project logging-log4j2 by apache.

the class XmlLayoutTest method testIncludeNullDelimiterTrue.

@Test
public void testIncludeNullDelimiterTrue() throws Exception {
    final AbstractJacksonLayout layout = XmlLayout.newBuilder().setCompact(true).setIncludeNullDelimiter(true).build();
    final String str = layout.toSerializable(LogEventFixtures.createLogEvent());
    assertTrue(str.endsWith("\0"));
}
Also used : AbstractJacksonLayout(org.apache.logging.log4j.jackson.AbstractJacksonLayout) Test(org.junit.Test)

Example 17 with AbstractJacksonLayout

use of org.apache.logging.log4j.jackson.AbstractJacksonLayout in project logging-log4j2 by apache.

the class JsonLayoutTest method testAdditionalFields.

@Test
public void testAdditionalFields() throws Exception {
    final AbstractJacksonLayout layout = JsonLayout.newBuilder().setLocationInfo(false).setProperties(false).setComplete(false).setCompact(true).setEventEol(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());
    assertTrue(str, str.contains("\"KEY1\":\"VALUE1\""));
    assertTrue(str, str.contains("\"KEY2\":\"" + new JavaLookup().getRuntime() + "\""));
}
Also used : KeyValuePair(org.apache.logging.log4j.core.util.KeyValuePair) AbstractJacksonLayout(org.apache.logging.log4j.jackson.AbstractJacksonLayout) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) JavaLookup(org.apache.logging.log4j.core.lookup.JavaLookup) Test(org.junit.Test)

Example 18 with AbstractJacksonLayout

use of org.apache.logging.log4j.jackson.AbstractJacksonLayout in project logging-log4j2 by apache.

the class JsonLayoutTest method testEmptyValuesAreIgnored.

/**
 * @see <a href="https://issues.apache.org/jira/browse/LOG4J2-2749">LOG4J2-2749</a>
 */
@Test
public void testEmptyValuesAreIgnored() {
    final AbstractJacksonLayout layout = JsonLayout.newBuilder().setAdditionalFields(new KeyValuePair[] { new KeyValuePair("empty", "${ctx:empty:-}") }).setConfiguration(ctx.getConfiguration()).build();
    final String str = layout.toSerializable(LogEventFixtures.createLogEvent());
    assertFalse(str, str.contains("\"empty\""));
}
Also used : KeyValuePair(org.apache.logging.log4j.core.util.KeyValuePair) AbstractJacksonLayout(org.apache.logging.log4j.jackson.AbstractJacksonLayout) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 19 with AbstractJacksonLayout

use of org.apache.logging.log4j.jackson.AbstractJacksonLayout in project logging-log4j2 by apache.

the class JsonLayoutTest method testReusableLayoutMessageWithCurlyBraces.

// Test for LOG4J2-2345
@Test
public void testReusableLayoutMessageWithCurlyBraces() throws Exception {
    final boolean propertiesAsList = false;
    final AbstractJacksonLayout layout = JsonLayout.newBuilder().setLocationInfo(false).setProperties(false).setPropertiesAsList(propertiesAsList).setComplete(false).setCompact(true).setEventEol(false).setCharset(StandardCharsets.UTF_8).setIncludeStacktrace(true).build();
    Message message = ReusableMessageFactory.INSTANCE.newMessage("Testing {}", new TestObj());
    try {
        final Log4jLogEvent expected = Log4jLogEvent.newBuilder().setLoggerName("a.B").setLoggerFqcn("f.q.c.n").setLevel(Level.DEBUG).setMessage(message).setThreadName("threadName").setTimeMillis(1).build();
        MutableLogEvent mutableLogEvent = new MutableLogEvent();
        mutableLogEvent.initFrom(expected);
        final String str = layout.toSerializable(mutableLogEvent);
        final String expectedMessage = "Testing " + TestObj.TO_STRING_VALUE;
        assertTrue(str, str.contains("\"message\":\"" + expectedMessage + '"'));
        final Log4jLogEvent actual = new Log4jJsonObjectMapper(propertiesAsList, true, false, false).readValue(str, Log4jLogEvent.class);
        assertEquals(expectedMessage, actual.getMessage().getFormattedMessage());
    } finally {
        ReusableMessageFactory.release(message);
    }
}
Also used : SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) ObjectMessage(org.apache.logging.log4j.message.ObjectMessage) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) Message(org.apache.logging.log4j.message.Message) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) AbstractJacksonLayout(org.apache.logging.log4j.jackson.AbstractJacksonLayout) Log4jJsonObjectMapper(org.apache.logging.log4j.jackson.json.Log4jJsonObjectMapper) MutableLogEvent(org.apache.logging.log4j.core.impl.MutableLogEvent) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 20 with AbstractJacksonLayout

use of org.apache.logging.log4j.jackson.AbstractJacksonLayout in project logging-log4j2 by apache.

the class JsonLayoutTest method testLayoutRingBufferEventReusableMessageWithCurlyBraces.

// Test for LOG4J2-2312 LOG4J2-2341
@Test
public void testLayoutRingBufferEventReusableMessageWithCurlyBraces() throws Exception {
    final boolean propertiesAsList = false;
    final AbstractJacksonLayout layout = JsonLayout.newBuilder().setLocationInfo(false).setProperties(false).setPropertiesAsList(propertiesAsList).setComplete(false).setCompact(true).setEventEol(false).setCharset(StandardCharsets.UTF_8).setIncludeStacktrace(true).build();
    Message message = ReusableMessageFactory.INSTANCE.newMessage("Testing {}", new TestObj());
    try {
        RingBufferLogEvent ringBufferEvent = new RingBufferLogEvent();
        ringBufferEvent.setValues(null, "a.B", null, "f.q.c.n", Level.DEBUG, message, null, new SortedArrayStringMap(), ThreadContext.EMPTY_STACK, 1L, "threadName", 1, null, new SystemClock(), new DummyNanoClock());
        final String str = layout.toSerializable(ringBufferEvent);
        final String expectedMessage = "Testing " + TestObj.TO_STRING_VALUE;
        assertThat(str, containsString("\"message\":\"" + expectedMessage + '"'));
        final Log4jLogEvent actual = new Log4jJsonObjectMapper(propertiesAsList, true, false, false).readValue(str, Log4jLogEvent.class);
        assertEquals(expectedMessage, actual.getMessage().getFormattedMessage());
    } finally {
        ReusableMessageFactory.release(message);
    }
}
Also used : SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) ObjectMessage(org.apache.logging.log4j.message.ObjectMessage) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) Message(org.apache.logging.log4j.message.Message) SystemClock(org.apache.logging.log4j.core.time.internal.SystemClock) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) AbstractJacksonLayout(org.apache.logging.log4j.jackson.AbstractJacksonLayout) SortedArrayStringMap(org.apache.logging.log4j.util.SortedArrayStringMap) DummyNanoClock(org.apache.logging.log4j.core.time.internal.DummyNanoClock) Log4jJsonObjectMapper(org.apache.logging.log4j.jackson.json.Log4jJsonObjectMapper) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) RingBufferLogEvent(org.apache.logging.log4j.core.async.RingBufferLogEvent) 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