Search in sources :

Example 1 with Log4jLogEvent

use of org.apache.logging.log4j.core.impl.Log4jLogEvent 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)

Example 2 with Log4jLogEvent

use of org.apache.logging.log4j.core.impl.Log4jLogEvent 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 3 with Log4jLogEvent

use of org.apache.logging.log4j.core.impl.Log4jLogEvent 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)

Example 4 with Log4jLogEvent

use of org.apache.logging.log4j.core.impl.Log4jLogEvent 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 5 with Log4jLogEvent

use of org.apache.logging.log4j.core.impl.Log4jLogEvent 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)36 SimpleMessage (org.apache.logging.log4j.message.SimpleMessage)15 Test (org.junit.Test)15 AbstractJacksonLayout (org.apache.logging.log4j.jackson.AbstractJacksonLayout)14 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)6 Log4jJsonObjectMapper (org.apache.logging.log4j.jackson.json.Log4jJsonObjectMapper)5 MutableLogEvent (org.apache.logging.log4j.core.impl.MutableLogEvent)4 KeyValuePair (org.apache.logging.log4j.core.util.KeyValuePair)4 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)4 StringMap (org.apache.logging.log4j.util.StringMap)4 Serializable (java.io.Serializable)3 ThrowableProxy (org.apache.logging.log4j.core.impl.ThrowableProxy)3 Message (org.apache.logging.log4j.message.Message)3 ObjectMessage (org.apache.logging.log4j.message.ObjectMessage)3 Test (org.junit.jupiter.api.Test)3 Benchmark (org.openjdk.jmh.annotations.Benchmark)3 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 Marker (org.apache.logging.log4j.Marker)2 Log4jJsonObjectMapper (org.apache.logging.log4j.core.jackson.Log4jJsonObjectMapper)2