Home / Class/ LogstashStructuredLogFormatterTests Class — spring-boot Architecture

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/logback/LogstashStructuredLogFormatterTests.java lines 43–109

class LogstashStructuredLogFormatterTests extends AbstractStructuredLoggingTests {

	private LogstashStructuredLogFormatter formatter;

	@Override
	@BeforeEach
	void setUp() {
		super.setUp();
		this.formatter = new LogstashStructuredLogFormatter(null, TestContextPairs.include(),
				getThrowableProxyConverter(), this.customizer);
	}

	@Test
	void callsCustomizer() {
		then(this.customizer).should().customize(any());
	}

	@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);
		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", 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%n\tat org.springframework.boot.logging.logback.LogstashStructuredLogFormatterTests.shouldFormatException"
					.formatted());
		assertThat(json).contains(
				"java.lang.RuntimeException: Boom%n\\tat org.springframework.boot.logging.logback.LogstashStructuredLogFormatterTests.shouldFormatException"
					.formatted()
					.replace("\n", "\\n")
					.replace("\r", "\\r"));
	}

	@Test
	void shouldFormatExceptionWithStackTracePrinter() {
		this.formatter = new LogstashStructuredLogFormatter(new SimpleStackTracePrinter(), TestContextPairs.include(),
				getThrowableProxyConverter(), this.customizer);
		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).isEqualTo("stacktrace:RuntimeException");
	}

}

Domain

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free