Search in sources :

Example 1 with Log4jXmlObjectMapper

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

the class XmlLayoutTest method testAllFeatures.

/**
     * @param includeSource TODO
     * @param compact
     * @param includeContext TODO
     * @throws IOException
     * @throws JsonParseException
     * @throws JsonMappingException
     */
private void testAllFeatures(final boolean includeSource, final boolean compact, final boolean includeContext, final boolean includeStacktrace) throws IOException, JsonParseException, JsonMappingException {
    final Log4jLogEvent expected = LogEventFixtures.createLogEvent();
    final XmlLayout layout = XmlLayout.createLayout(includeSource, includeContext, false, compact, StandardCharsets.UTF_8, includeStacktrace);
    final String str = layout.toSerializable(expected);
    // System.out.println(str);
    assertEquals(str, !compact, str.contains("\n"));
    assertEquals(str, includeSource, str.contains("Source"));
    assertEquals(str, includeContext, str.contains("ContextMap"));
    final Log4jLogEvent actual = new Log4jXmlObjectMapper().readValue(str, Log4jLogEvent.class);
    LogEventFixtures.assertEqualLogEvents(expected, actual, includeSource, includeContext, includeStacktrace);
    if (includeContext) {
        this.checkElement("MDC.A", "A_Value", compact, str);
        this.checkElement("MDC.B", "B_Value", compact, str);
    }
    //
    assertNull(actual.getThrown());
    // check some attrs
    assertTrue(str, str.contains("loggerFqcn=\"f.q.c.n\""));
    assertTrue(str, str.contains("loggerName=\"a.B\""));
    // make sure short names are used
    assertTrue(str, str.contains("<Event "));
    if (includeStacktrace) {
        assertTrue(str, str.contains("class="));
        assertTrue(str, str.contains("method="));
        assertTrue(str, str.contains("file="));
        assertTrue(str, str.contains("line="));
    }
    //
    // make sure the names we want are used
    this.checkAttributeName("timeMillis", compact, str);
    // and not threadName
    this.checkAttributeName("thread", compact, str);
    this.checkAttributeName("level", compact, str);
    this.checkAttributeName("loggerName", compact, str);
    this.checkElementName("Marker", compact, str, true, true);
    this.checkAttributeName("name", compact, str);
    this.checkElementName("Parents", compact, str, false, true);
    this.checkElementName("Message", compact, str, false, true);
    this.checkElementName("Thrown", compact, str, true, true);
    this.checkElementName("Cause", compact, str, true, includeStacktrace);
    this.checkAttributeName("commonElementCount", compact, str);
    this.checkAttributeName("message", compact, str);
    this.checkAttributeName("localizedMessage", compact, str);
    if (includeStacktrace) {
        this.checkElementName("ExtendedStackTrace", compact, str, false, true);
        this.checkAttributeName("class", compact, str);
        this.checkAttributeName("method", compact, str);
        this.checkAttributeName("file", compact, str);
        this.checkAttributeName("line", compact, str);
        this.checkAttributeName("exact", compact, str);
        this.checkAttributeName("location", compact, str);
        this.checkAttributeName("version", compact, str);
    } else {
        this.checkElementNameAbsent("ExtendedStackTrace", compact, str);
    }
    this.checkElementName("Suppressed", compact, str, false, true);
    this.checkAttributeName("loggerFqcn", compact, str);
    this.checkAttributeName("endOfBatch", compact, str);
    if (includeContext) {
        this.checkElementName("ContextMap", compact, str, false, true);
    } else {
        this.checkElementNameAbsent("ContextMap", compact, str);
    }
    this.checkElementName("ContextStack", compact, str, false, true);
    if (includeSource) {
        this.checkElementName("Source", compact, str, true, false);
    } else {
        this.checkElementNameAbsent("Source", compact, str);
    }
    // check some attrs
    this.checkAttribute("loggerFqcn", "f.q.c.n", compact, str);
    this.checkAttribute("loggerName", "a.B", compact, str);
}
Also used : Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) Log4jXmlObjectMapper(org.apache.logging.log4j.core.jackson.Log4jXmlObjectMapper)

Aggregations

Log4jLogEvent (org.apache.logging.log4j.core.impl.Log4jLogEvent)1 Log4jXmlObjectMapper (org.apache.logging.log4j.core.jackson.Log4jXmlObjectMapper)1