Search in sources :

Example 11 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 String endOfLine, 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).setEndOfLine(endOfLine).setCharset(StandardCharsets.UTF_8).setIncludeStacktrace(includeStacktrace).build();
    // @formatter:off
    final String str = layout.toSerializable(expected);
    this.toPropertySeparator(compact);
    if (endOfLine == null) {
        // Just check for \n since \r might or might not be there.
        assertEquals(str, !compact || eventEol, str.contains("\n"));
    } else {
        assertEquals(str, !compact || eventEol, str.contains(endOfLine));
        assertEquals(str, compact && eventEol, str.endsWith(endOfLine));
    }
    assertEquals(str, locationInfo, str.contains("source"));
    assertEquals(str, includeContext, str.contains("contextMap"));
    final Log4jLogEvent actual = new Log4jJsonObjectMapper(contextMapAslist, includeStacktrace, false, false).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("instant", 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) AbstractJacksonLayout(org.apache.logging.log4j.jackson.AbstractJacksonLayout) Log4jJsonObjectMapper(org.apache.logging.log4j.jackson.json.Log4jJsonObjectMapper) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString)

Example 12 with Log4jLogEvent

use of org.apache.logging.log4j.core.impl.Log4jLogEvent in project logging-log4j2 by apache.

the class JsonLayoutTest method testLayoutMessageWithCurlyBraces.

@Test
public void testLayoutMessageWithCurlyBraces() throws Exception {
    final boolean propertiesAsList = false;
    final AbstractJacksonLayout layout = JsonLayout.newBuilder().setLocationInfo(false).setProperties(false).setPropertiesAsList(propertiesAsList).setComplete(false).setCompact(true).setEventEol(false).setCharset(StandardCharsets.UTF_8).setIncludeStacktrace(true).build();
    final Log4jLogEvent expected = Log4jLogEvent.newBuilder().setLoggerName("a.B").setLoggerFqcn("f.q.c.n").setLevel(Level.DEBUG).setMessage(new ParameterizedMessage("Testing {}", new TestObj())).setThreadName("threadName").setTimeMillis(1).build();
    final String str = layout.toSerializable(expected);
    final String expectedMessage = "Testing " + TestObj.TO_STRING_VALUE;
    assertTrue(str, str.contains("\"message\":\"" + expectedMessage + '"'));
    final Log4jLogEvent actual = new Log4jJsonObjectMapper(propertiesAsList, true, false, false).readValue(str, Log4jLogEvent.class);
    assertEquals(expectedMessage, actual.getMessage().getFormattedMessage());
}
Also used : Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) AbstractJacksonLayout(org.apache.logging.log4j.jackson.AbstractJacksonLayout) Log4jJsonObjectMapper(org.apache.logging.log4j.jackson.json.Log4jJsonObjectMapper) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 13 with Log4jLogEvent

use of org.apache.logging.log4j.core.impl.Log4jLogEvent in project logging-log4j2 by apache.

the class JsonLayoutTest method testMutableLogEvent.

@Test
public void testMutableLogEvent() throws Exception {
    final AbstractJacksonLayout layout = JsonLayout.newBuilder().setLocationInfo(false).setProperties(false).setComplete(false).setCompact(true).setEventEol(false).setIncludeStacktrace(false).setAdditionalFields(new KeyValuePair[] { new KeyValuePair("KEY1", "VALUE1"), new KeyValuePair("KEY2", "${java:runtime}") }).setCharset(StandardCharsets.UTF_8).setConfiguration(ctx.getConfiguration()).build();
    Log4jLogEvent logEvent = LogEventFixtures.createLogEvent();
    final MutableLogEvent mutableEvent = new MutableLogEvent();
    mutableEvent.initFrom(logEvent);
    final String strLogEvent = layout.toSerializable(logEvent);
    final String strMutableEvent = layout.toSerializable(mutableEvent);
    assertEquals(strMutableEvent, strLogEvent, strMutableEvent);
}
Also used : KeyValuePair(org.apache.logging.log4j.core.util.KeyValuePair) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) AbstractJacksonLayout(org.apache.logging.log4j.jackson.AbstractJacksonLayout) MutableLogEvent(org.apache.logging.log4j.core.impl.MutableLogEvent) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 14 with Log4jLogEvent

use of org.apache.logging.log4j.core.impl.Log4jLogEvent in project logging-log4j2 by apache.

the class JsonLayoutTest method prepareJsonForObjectMessageAsJsonObjectTests.

private String prepareJsonForObjectMessageAsJsonObjectTests(final int value, final boolean objectMessageAsJsonObject) {
    final TestClass testClass = new TestClass();
    testClass.setValue(value);
    // @formatter:off
    final Log4jLogEvent expected = Log4jLogEvent.newBuilder().setLoggerName("a.B").setLoggerFqcn("f.q.c.n").setLevel(Level.DEBUG).setMessage(new ObjectMessage(testClass)).setThreadName("threadName").setTimeMillis(1).build();
    // @formatter:off
    final AbstractJacksonLayout layout = JsonLayout.newBuilder().setCompact(true).setObjectMessageAsJsonObject(objectMessageAsJsonObject).build();
    // @formatter:off
    return layout.toSerializable(expected);
}
Also used : Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) ObjectMessage(org.apache.logging.log4j.message.ObjectMessage) AbstractJacksonLayout(org.apache.logging.log4j.jackson.AbstractJacksonLayout)

Example 15 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.newBuilder().setLocationInfo(includeSource).setProperties(includeContext).setIncludeStacktrace(includeStacktrace).setCharset(StandardCharsets.UTF_8).build();
    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, false).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("instant", compact, str, false);
    // 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.jackson.yaml.Log4jYamlObjectMapper) AbstractJacksonLayout(org.apache.logging.log4j.jackson.AbstractJacksonLayout)

Aggregations

Log4jLogEvent (org.apache.logging.log4j.core.impl.Log4jLogEvent)37 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 MutableLogEvent (org.apache.logging.log4j.core.impl.MutableLogEvent)5 Log4jJsonObjectMapper (org.apache.logging.log4j.jackson.json.Log4jJsonObjectMapper)5 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