Skip to content

Commit

Permalink
Add tests for LogbackLogstashStructuredLoggingFormatter
Browse files Browse the repository at this point in the history
  • Loading branch information
mhalbritter committed Jul 2, 2024
1 parent 23e8e2b commit d625aa7
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import ch.qos.logback.classic.pattern.ThrowableProxyConverter;
import ch.qos.logback.classic.spi.ILoggingEvent;
Expand Down Expand Up @@ -104,7 +104,7 @@ private Set<String> getMarkers(ILoggingEvent event) {
if (CollectionUtils.isEmpty(event.getMarkerList())) {
return Collections.emptySet();
}
Set<String> result = new HashSet<>();
Set<String> result = new TreeSet<>();
for (Marker marker : event.getMarkerList()) {
addMarker(result, marker);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.slf4j.Marker;
import org.slf4j.event.KeyValuePair;
import org.slf4j.helpers.BasicMarkerFactory;

import static org.assertj.core.api.Assertions.assertThat;

Expand All @@ -46,8 +48,11 @@ abstract class AbstractStructuredLoggingTests {

private ThrowableProxyConverter throwableProxyConverter;

private BasicMarkerFactory markerFactory;

@BeforeEach
void setUp() {
this.markerFactory = new BasicMarkerFactory();
this.throwableProxyConverter = new ThrowableProxyConverter();
this.throwableProxyConverter.start();
}
Expand All @@ -57,6 +62,10 @@ void tearDown() {
this.throwableProxyConverter.stop();
}

protected Marker getMarker(String name) {
return this.markerFactory.getDetachedMarker(name);
}

protected ThrowableProxyConverter getThrowableProxyConverter() {
return this.throwableProxyConverter;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class LogbackEcsStructuredLoggingFormatterTests extends AbstractStructuredLoggin

private LogbackEcsStructuredLoggingFormatter formatter;

@Override
@BeforeEach
void setUp() {
super.setUp();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.boot.logging.logback;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import ch.qos.logback.classic.spi.LoggingEvent;
import ch.qos.logback.classic.spi.ThrowableProxy;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.slf4j.Marker;

import static org.assertj.core.api.Assertions.assertThat;

/**
* Tests for {@link LogbackLogstashStructuredLoggingFormatter}.
*
* @author Moritz Halbritter
*/
class LogbackLogstashStructuredLoggingFormatterTests extends AbstractStructuredLoggingTests {

private LogbackLogstashStructuredLoggingFormatter formatter;

@Override
@BeforeEach
void setUp() {
super.setUp();
this.formatter = new LogbackLogstashStructuredLoggingFormatter(getThrowableProxyConverter());
}

@Test
void shouldFormat() {
LoggingEvent event = createEvent();
event.setMDCPropertyMap(Map.of("mdc-1", "mdc-v-1"));
event.setKeyValuePairs(keyValuePairs("kv-1", "kv-v-1"));
Marker marker1 = getMarker("marker-1");
marker1.add(getMarker("marker-2"));
event.addMarker(marker1);
String json = this.formatter.format(event);
assertThat(json).endsWith("\n");
Map<String, Object> deserialized = deserialize(json);
assertThat(deserialized).containsExactlyInAnyOrderEntriesOf(
map("@timestamp", "2024-07-02T10:49:53+02:00", "@version", "1", "message", "message", "logger_name",
"org.example.Test", "thread_name", "main", "level", "INFO", "level_value", 20000, "mdc-1",
"mdc-v-1", "kv-1", "kv-v-1", "tags", List.of("marker-1", "marker-2")));
}

@Test
void shouldFormatException() {
LoggingEvent event = createEvent();
event.setThrowableProxy(new ThrowableProxy(new RuntimeException("Boom")));
event.setMDCPropertyMap(Collections.emptyMap());
String json = this.formatter.format(event);
Map<String, Object> deserialized = deserialize(json);
String stackTrace = (String) deserialized.get("stack_trace");
assertThat(stackTrace).startsWith(
"""
java.lang.RuntimeException: Boom
\tat org.springframework.boot.logging.logback.LogbackLogstashStructuredLoggingFormatterTests.shouldFormatException
"""
.trim());
assertThat(json).contains(
"""
java.lang.RuntimeException: Boom\\n\\tat org.springframework.boot.logging.logback.LogbackLogstashStructuredLoggingFormatterTests.shouldFormatException
"""
.trim());
}

}

0 comments on commit d625aa7

Please sign in to comment.