Search in sources :

Example 1 with Severity

use of org.apache.logging.log4j.core.net.Severity in project logging-log4j2 by apache.

the class JsonTemplateLayoutTest method test_level_severity.

@Test
void test_level_severity() {
    // Create the event template.
    final String eventTemplate = writeJson(asMap("severityKeyword", asMap("$resolver", "level", "field", "severity", "severity", asMap("field", "keyword")), "severityCode", asMap("$resolver", "level", "field", "severity", "severity", asMap("field", "code"))));
    // Create the layout.
    final JsonTemplateLayout layout = JsonTemplateLayout.newBuilder().setConfiguration(CONFIGURATION).setEventTemplate(eventTemplate).build();
    for (final Level level : Level.values()) {
        // Create the log event.
        final SimpleMessage message = new SimpleMessage("Hello, World!");
        final LogEvent logEvent = Log4jLogEvent.newBuilder().setLoggerName(LOGGER_NAME).setLevel(level).setMessage(message).build();
        // Check the serialized event.
        usingSerializedLogEventAccessor(layout, logEvent, accessor -> {
            final Severity expectedSeverity = Severity.getSeverity(level);
            final String expectedSeverityKeyword = expectedSeverity.name();
            final int expectedSeverityCode = expectedSeverity.getCode();
            assertThat(accessor.getString("severityKeyword")).isEqualTo(expectedSeverityKeyword);
            assertThat(accessor.getInteger("severityCode")).isEqualTo(expectedSeverityCode);
        });
    }
}
Also used : LogEvent(org.apache.logging.log4j.core.LogEvent) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Level(org.apache.logging.log4j.Level) Severity(org.apache.logging.log4j.core.net.Severity) Test(org.junit.jupiter.api.Test)

Aggregations

Level (org.apache.logging.log4j.Level)1 LogEvent (org.apache.logging.log4j.core.LogEvent)1 Log4jLogEvent (org.apache.logging.log4j.core.impl.Log4jLogEvent)1 Severity (org.apache.logging.log4j.core.net.Severity)1 SimpleMessage (org.apache.logging.log4j.message.SimpleMessage)1 Test (org.junit.jupiter.api.Test)1