Search in sources :

Example 1 with Log4jJsonObjectMapper

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

the class JsonLayoutTest method testAllFeatures.

private void testAllFeatures(final boolean locationInfo, final boolean compact, final boolean eventEol, final String endOfLine, final boolean includeContext, final boolean contextMapAslist, final boolean includeStacktrace) throws Exception {
    final Log4jLogEvent expected = LogEventFixtures.createLogEvent();
    // @formatter:off
    final AbstractJacksonLayout layout = JsonLayout.newBuilder().setLocationInfo(locationInfo).setProperties(includeContext).setPropertiesAsList(contextMapAslist).setComplete(false).setCompact(compact).setEventEol(eventEol).setEndOfLine(endOfLine).setCharset(StandardCharsets.UTF_8).setIncludeStacktrace(includeStacktrace).build();
    // @formatter:off
    final String str = layout.toSerializable(expected);
    this.toPropertySeparator(compact);
    if (endOfLine == null) {
        // Just check for \n since \r might or might not be there.
        assertEquals(str, !compact || eventEol, str.contains("\n"));
    } else {
        assertEquals(str, !compact || eventEol, str.contains(endOfLine));
        assertEquals(str, compact && eventEol, str.endsWith(endOfLine));
    }
    assertEquals(str, locationInfo, str.contains("source"));
    assertEquals(str, includeContext, str.contains("contextMap"));
    final Log4jLogEvent actual = new Log4jJsonObjectMapper(contextMapAslist, includeStacktrace, false, false).readValue(str, Log4jLogEvent.class);
    LogEventFixtures.assertEqualLogEvents(expected, actual, locationInfo, includeContext, includeStacktrace);
    if (includeContext) {
        this.checkMapEntry("MDC.A", "A_Value", compact, str, contextMapAslist);
        this.checkMapEntry("MDC.B", "B_Value", compact, str, contextMapAslist);
    }
    // 
    assertNull(actual.getThrown());
    // make sure the names we want are used
    this.checkPropertyName("instant", compact, str);
    // and not threadName
    this.checkPropertyName("thread", compact, str);
    this.checkPropertyName("level", compact, str);
    this.checkPropertyName("loggerName", compact, str);
    this.checkPropertyName("marker", compact, str);
    this.checkPropertyName("name", compact, str);
    this.checkPropertyName("parents", compact, str);
    this.checkPropertyName("message", compact, str);
    this.checkPropertyName("thrown", compact, str);
    this.checkPropertyName("cause", compact, str);
    this.checkPropertyName("commonElementCount", compact, str);
    this.checkPropertyName("localizedMessage", compact, str);
    if (includeStacktrace) {
        this.checkPropertyName("extendedStackTrace", compact, str);
        this.checkPropertyName("class", compact, str);
        this.checkPropertyName("method", compact, str);
        this.checkPropertyName("file", compact, str);
        this.checkPropertyName("line", compact, str);
        this.checkPropertyName("exact", compact, str);
        this.checkPropertyName("location", compact, str);
        this.checkPropertyName("version", compact, str);
    } else {
        this.checkPropertyNameAbsent("extendedStackTrace", compact, str);
    }
    this.checkPropertyName("suppressed", compact, str);
    this.checkPropertyName("loggerFqcn", compact, str);
    this.checkPropertyName("endOfBatch", compact, str);
    if (includeContext) {
        this.checkPropertyName("contextMap", compact, str);
    } else {
        this.checkPropertyNameAbsent("contextMap", compact, str);
    }
    this.checkPropertyName("contextStack", compact, str);
    if (locationInfo) {
        this.checkPropertyName("source", compact, str);
    } else {
        this.checkPropertyNameAbsent("source", compact, str);
    }
    // check some attrs
    this.checkProperty("loggerFqcn", "f.q.c.n", compact, str);
    this.checkProperty("loggerName", "a.B", compact, str);
}
Also used : Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) AbstractJacksonLayout(org.apache.logging.log4j.jackson.AbstractJacksonLayout) Log4jJsonObjectMapper(org.apache.logging.log4j.jackson.json.Log4jJsonObjectMapper) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString)

Example 2 with Log4jJsonObjectMapper

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

the class JsonLayoutTest method testLayoutMessageWithCurlyBraces.

@Test
public void testLayoutMessageWithCurlyBraces() 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();
    final Log4jLogEvent expected = Log4jLogEvent.newBuilder().setLoggerName("a.B").setLoggerFqcn("f.q.c.n").setLevel(Level.DEBUG).setMessage(new ParameterizedMessage("Testing {}", new TestObj())).setThreadName("threadName").setTimeMillis(1).build();
    final String str = layout.toSerializable(expected);
    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());
}
Also used : Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) AbstractJacksonLayout(org.apache.logging.log4j.jackson.AbstractJacksonLayout) Log4jJsonObjectMapper(org.apache.logging.log4j.jackson.json.Log4jJsonObjectMapper) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 3 with Log4jJsonObjectMapper

use of org.apache.logging.log4j.jackson.json.Log4jJsonObjectMapper 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 4 with Log4jJsonObjectMapper

use of org.apache.logging.log4j.jackson.json.Log4jJsonObjectMapper 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)

Example 5 with Log4jJsonObjectMapper

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

the class JsonLayoutTest method testLayoutLoggerName.

@Test
public void testLayoutLoggerName() throws Exception {
    final boolean propertiesAsList = false;
    // @formatter:off
    final AbstractJacksonLayout layout = JsonLayout.newBuilder().setLocationInfo(false).setProperties(false).setPropertiesAsList(propertiesAsList).setComplete(false).setCompact(true).setEventEol(false).setCharset(StandardCharsets.UTF_8).setIncludeStacktrace(true).build();
    // @formatter:on
    // @formatter:off
    final Log4jLogEvent expected = Log4jLogEvent.newBuilder().setLoggerName("a.B").setLoggerFqcn("f.q.c.n").setLevel(Level.DEBUG).setMessage(new SimpleMessage("M")).setThreadName("threadName").setTimeMillis(1).build();
    // @formatter:on
    final String str = layout.toSerializable(expected);
    assertTrue(str, str.contains("\"loggerName\":\"a.B\""));
    final Log4jLogEvent actual = new Log4jJsonObjectMapper(propertiesAsList, true, false, false).readValue(str, Log4jLogEvent.class);
    assertEquals(expected.getLoggerName(), actual.getLoggerName());
    assertEquals(expected, actual);
}
Also used : Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) AbstractJacksonLayout(org.apache.logging.log4j.jackson.AbstractJacksonLayout) Log4jJsonObjectMapper(org.apache.logging.log4j.jackson.json.Log4jJsonObjectMapper) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Aggregations

Log4jLogEvent (org.apache.logging.log4j.core.impl.Log4jLogEvent)5 AbstractJacksonLayout (org.apache.logging.log4j.jackson.AbstractJacksonLayout)5 Log4jJsonObjectMapper (org.apache.logging.log4j.jackson.json.Log4jJsonObjectMapper)5 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)5 Test (org.junit.Test)4 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)3 SimpleMessage (org.apache.logging.log4j.message.SimpleMessage)3 Message (org.apache.logging.log4j.message.Message)2 ObjectMessage (org.apache.logging.log4j.message.ObjectMessage)2 RingBufferLogEvent (org.apache.logging.log4j.core.async.RingBufferLogEvent)1 MutableLogEvent (org.apache.logging.log4j.core.impl.MutableLogEvent)1 DummyNanoClock (org.apache.logging.log4j.core.time.internal.DummyNanoClock)1 SystemClock (org.apache.logging.log4j.core.time.internal.SystemClock)1 SortedArrayStringMap (org.apache.logging.log4j.util.SortedArrayStringMap)1