LogstashStructuredLogFormatterTests Class — spring-boot Architecture
Architecture documentation for the LogstashStructuredLogFormatterTests class in LogstashStructuredLogFormatterTests.java from the spring-boot codebase.
Entity Profile
Relationship Graph
Source Code
core/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/LogstashStructuredLogFormatterTests.java lines 44–119
class LogstashStructuredLogFormatterTests extends AbstractStructuredLoggingTests {
private LogstashStructuredLogFormatter formatter;
@BeforeEach
void setUp() {
this.formatter = new LogstashStructuredLogFormatter(null, TestContextPairs.include(), this.customizer);
}
@Test
void callsCustomizer() {
then(this.customizer).should().customize(any());
}
@Test
void shouldFormat() {
MutableLogEvent event = createEvent();
event.setContextData(new JdkMapAdapterStringMap(Map.of("mdc-1", "mdc-v-1"), true));
Log4jMarker marker1 = new Log4jMarker("marker-1");
marker1.addParents(new Log4jMarker("marker-2"));
event.setMarker(marker1);
String json = this.formatter.format(event);
assertThat(json).endsWith("\n");
Map<String, Object> deserialized = deserialize(json);
String timestamp = DateTimeFormatter.ISO_OFFSET_DATE_TIME
.format(OffsetDateTime.ofInstant(EVENT_TIME, ZoneId.systemDefault()));
assertThat(deserialized).containsExactlyInAnyOrderEntriesOf(map("@timestamp", timestamp, "@version", "1",
"message", "message", "logger_name", "org.example.Test", "thread_name", "main", "level", "INFO",
"level_value", 400, "mdc-1", "mdc-v-1", "tags", List.of("marker-1", "marker-2")));
}
@Test
void shouldFormatException() {
MutableLogEvent event = createEvent();
event.setThrown(new RuntimeException("Boom"));
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%n\tat org.springframework.boot.logging.log4j2.LogstashStructuredLogFormatterTests.shouldFormatException"
.formatted());
assertThat(json).contains(
"java.lang.RuntimeException: Boom%n\\tat org.springframework.boot.logging.log4j2.LogstashStructuredLogFormatterTests.shouldFormatException"
.formatted()
.replace("\n", "\\n")
.replace("\r", "\\r"));
}
@Test
void shouldFormatExceptionWithStackTracePrinter() {
this.formatter = new LogstashStructuredLogFormatter(new SimpleStackTracePrinter(), TestContextPairs.include(),
this.customizer);
MutableLogEvent event = createEvent();
event.setThrown(new RuntimeException("Boom"));
String json = this.formatter.format(event);
Map<String, Object> deserialized = deserialize(json);
String stackTrace = (String) deserialized.get("stack_trace");
assertThat(stackTrace).isEqualTo("stacktrace:RuntimeException");
}
@Test
void shouldFormatStructuredMessage() {
MutableLogEvent event = createEvent();
event.setMessage(new MapMessage<>().with("foo", true).with("bar", 1.0));
String json = this.formatter.format(event);
assertThat(json).endsWith("\n");
Map<String, Object> deserialized = deserialize(json);
Map<String, Object> expectedMessage = Map.of("foo", true, "bar", 1.0);
String timestamp = DateTimeFormatter.ISO_OFFSET_DATE_TIME
.format(OffsetDateTime.ofInstant(EVENT_TIME, ZoneId.systemDefault()));
assertThat(deserialized).containsExactlyInAnyOrderEntriesOf(
map("@timestamp", timestamp, "@version", "1", "message", expectedMessage, "logger_name",
"org.example.Test", "thread_name", "main", "level", "INFO", "level_value", 400));
}
}
Domain
Source
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free