Search in sources :

Example 1 with Log4jJsonObjectMapper

use of org.apache.logging.log4j.core.jackson.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).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) Log4jJsonObjectMapper(org.apache.logging.log4j.core.jackson.Log4jJsonObjectMapper) Test(org.junit.Test)

Example 2 with Log4jJsonObjectMapper

use of org.apache.logging.log4j.core.jackson.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 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).setCharset(StandardCharsets.UTF_8).setIncludeStacktrace(includeStacktrace).build();
    // @formatter:off
    final String str = layout.toSerializable(expected);
    this.toPropertySeparator(compact);
    // Just check for \n since \r might or might not be there.
    assertEquals(str, !compact || eventEol, str.contains("\n"));
    assertEquals(str, locationInfo, str.contains("source"));
    assertEquals(str, includeContext, str.contains("contextMap"));
    final Log4jLogEvent actual = new Log4jJsonObjectMapper(contextMapAslist, includeStacktrace).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("timeMillis", 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) Log4jJsonObjectMapper(org.apache.logging.log4j.core.jackson.Log4jJsonObjectMapper)

Aggregations

Log4jLogEvent (org.apache.logging.log4j.core.impl.Log4jLogEvent)2 Log4jJsonObjectMapper (org.apache.logging.log4j.core.jackson.Log4jJsonObjectMapper)2 SimpleMessage (org.apache.logging.log4j.message.SimpleMessage)1 Test (org.junit.Test)1