Search in sources :

Example 1 with Log4jXmlObjectMapper

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

the class XmlLayoutTest method testAllFeatures.

/**
 * @param includeLocationInfo
 *            TODO
 * @param compact
 * @param includeContextMap
 *            TODO
 * @param includeContextStack
 *            TODO
 * @throws IOException
 * @throws JsonParseException
 * @throws JsonMappingException
 */
private void testAllFeatures(final boolean includeLocationInfo, final boolean compact, final boolean includeContextMap, final boolean includeContextStack, final boolean includeStacktrace) throws IOException, JsonParseException, JsonMappingException {
    final Log4jLogEvent expected = LogEventFixtures.createLogEvent();
    // @formatter:off
    final XmlLayout layout = XmlLayout.newBuilder().setLocationInfo(includeLocationInfo).setProperties(includeContextMap).setComplete(false).setCompact(compact).setIncludeStacktrace(includeStacktrace).setCharset(StandardCharsets.UTF_8).build();
    final String str = layout.toSerializable(expected);
    // @formatter:on
    // System.out.println(str);
    assertEquals(str, !compact, str.contains("\n"));
    assertEquals(str, includeLocationInfo, str.contains("Source"));
    assertEquals(str, includeContextMap, str.contains("ContextMap"));
    final Log4jLogEvent actual = new Log4jXmlObjectMapper().readValue(str, Log4jLogEvent.class);
    LogEventFixtures.assertEqualLogEvents(expected, actual, includeLocationInfo, includeContextMap, includeStacktrace);
    if (includeContextMap) {
        this.checkContextMapElement("MDC.A", "A_Value", compact, str);
        this.checkContextMapElement("MDC.B", "B_Value", compact, str);
    }
    if (includeContextStack) {
        this.checkContextStackElement("stack_msg1", compact, str);
        this.checkContextStackElement("stack_msg2", 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("Missing \"class=\" in: " + str, str.contains("class="));
        assertTrue("Missing \"method=\" in: " + str, str.contains("method="));
        assertTrue("Missing \"file=\" in: " + str, str.contains("file="));
        assertTrue("Missing \"line=\" in: " + str, str.contains("line="));
    }
    // 
    // make sure the names we want are used
    // this.checkAttributeName("timeMillis", compact, str);
    this.checkElementName("Instant", compact, str, true, false);
    this.checkAttributeName("epochSecond", compact, str);
    this.checkAttributeName("nanoOfSecond", 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 (includeContextMap) {
        this.checkElementName("ContextMap", compact, str, false, true);
    } else {
        this.checkElementNameAbsent("ContextMap", compact, str);
    }
    this.checkElementName("ContextStack", compact, str, false, true);
    if (includeLocationInfo) {
        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);
    this.checkJsonPropertyOrder(includeContextStack, includeContextMap, includeStacktrace, str);
}
Also used : Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) Log4jXmlObjectMapper(org.apache.logging.log4j.jackson.xml.Log4jXmlObjectMapper)

Aggregations

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