Search in sources :

Example 1 with Log4jYamlObjectMapper

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

the class YamlLayoutTest method testAllFeatures.

private void testAllFeatures(final boolean includeSource, final boolean compact, final boolean eventEol, final boolean includeContext, final boolean contextMapAslist, final boolean includeStacktrace) throws Exception {
    final Log4jLogEvent expected = LogEventFixtures.createLogEvent();
    final AbstractJacksonLayout layout = YamlLayout.createLayout(null, includeSource, includeContext, null, null, StandardCharsets.UTF_8, includeStacktrace);
    final String str = layout.toSerializable(expected);
    // System.out.println(str);
    // Just check for \n since \r might or might not be there.
    assertEquals(str, !compact || eventEol, str.contains("\n"));
    assertEquals(str, includeSource, str.contains("source"));
    assertEquals(str, includeContext, str.contains("contextMap"));
    final Log4jLogEvent actual = new Log4jYamlObjectMapper(contextMapAslist, includeStacktrace).readValue(str, Log4jLogEvent.class);
    LogEventFixtures.assertEqualLogEvents(expected, actual, includeSource, includeContext, includeStacktrace);
    if (includeContext) {
        this.checkMapEntry("MDC.A", "A_Value", compact, str);
        this.checkMapEntry("MDC.B", "B_Value", compact, str);
    }
    //
    assertNull(actual.getThrown());
    // make sure the names we want are used
    this.checkPropertyName("timeMillis", compact, str, true);
    // and not threadName
    this.checkPropertyName("thread", compact, str, true);
    this.checkPropertyName("level", compact, str, true);
    this.checkPropertyName("loggerName", compact, str, true);
    this.checkPropertyName("marker", compact, str, false);
    this.checkPropertyName("name", compact, str, true);
    this.checkPropertyName("parents", compact, str, false);
    this.checkPropertyName("message", compact, str, true);
    this.checkPropertyName("thrown", compact, str, false);
    this.checkPropertyName("cause", compact, str, false);
    this.checkPropertyName("commonElementCount", compact, str, true);
    this.checkPropertyName("localizedMessage", compact, str, true);
    if (includeStacktrace) {
        this.checkPropertyName("extendedStackTrace", compact, str, false);
        this.checkPropertyName("class", compact, str, true);
        this.checkPropertyName("method", compact, str, true);
        this.checkPropertyName("file", compact, str, true);
        this.checkPropertyName("line", compact, str, true);
        this.checkPropertyName("exact", compact, str, true);
        this.checkPropertyName("location", compact, str, true);
        this.checkPropertyName("version", compact, str, true);
    } else {
        this.checkPropertyNameAbsent("extendedStackTrace", compact, str, false);
    }
    this.checkPropertyName("suppressed", compact, str, false);
    this.checkPropertyName("loggerFqcn", compact, str, true);
    this.checkPropertyName("endOfBatch", compact, str, true);
    if (includeContext) {
        this.checkPropertyName("contextMap", compact, str, false);
    } else {
        this.checkPropertyNameAbsent("contextMap", compact, str, false);
    }
    this.checkPropertyName("contextStack", compact, str, false);
    if (includeSource) {
        this.checkPropertyName("source", compact, str, false);
    } else {
        this.checkPropertyNameAbsent("source", compact, str, false);
    }
    // check some attrs
    this.checkProperty("loggerFqcn", "f.q.c.n", compact, str, true);
    this.checkProperty("loggerName", "a.B", compact, str, true);
}
Also used : Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) Log4jYamlObjectMapper(org.apache.logging.log4j.core.jackson.Log4jYamlObjectMapper)

Example 2 with Log4jYamlObjectMapper

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

the class YamlLayoutTest method testLayoutLoggerName.

@Test
public void testLayoutLoggerName() throws Exception {
    final AbstractJacksonLayout layout = YamlLayout.createLayout(null, false, false, null, null, StandardCharsets.UTF_8, true);
    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();
    final String str = layout.toSerializable(expected);
    assertTrue(str, str.contains("loggerName: \"a.B\""));
    final Log4jLogEvent actual = new Log4jYamlObjectMapper(false, true).readValue(str, Log4jLogEvent.class);
    assertEquals(expected.getLoggerName(), actual.getLoggerName());
    assertEquals(expected, actual);
}
Also used : Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) Log4jYamlObjectMapper(org.apache.logging.log4j.core.jackson.Log4jYamlObjectMapper) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Test(org.junit.Test)

Aggregations

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