|
1 | 1 | package uk.gov.hmcts.cp.integration; |
2 | 2 |
|
| 3 | +import ch.qos.logback.classic.AsyncAppender; |
3 | 4 | import com.fasterxml.jackson.core.type.TypeReference; |
4 | 5 | import com.fasterxml.jackson.databind.ObjectMapper; |
5 | 6 | import lombok.extern.slf4j.Slf4j; |
| 7 | +import org.assertj.core.api.AssertionsForClassTypes; |
6 | 8 | import org.junit.jupiter.api.AfterEach; |
7 | 9 | import org.junit.jupiter.api.Test; |
| 10 | +import org.slf4j.LoggerFactory; |
8 | 11 | import org.slf4j.MDC; |
9 | 12 |
|
10 | 13 | import java.io.ByteArrayOutputStream; |
11 | 14 | import java.io.IOException; |
12 | 15 | import java.io.PrintStream; |
13 | | -import java.nio.charset.StandardCharsets; |
14 | 16 | import java.util.Map; |
15 | 17 |
|
16 | 18 | import static org.assertj.core.api.Assertions.assertThat; |
17 | 19 |
|
18 | 20 | @Slf4j |
19 | 21 | class SpringLoggingIntegrationTest extends IntegrationTestBase { |
20 | | - |
21 | | - private final PrintStream originalStdOut = System.out; |
| 22 | + private PrintStream originalStdOut = System.out; |
22 | 23 |
|
23 | 24 | @AfterEach |
24 | 25 | void afterEach() { |
25 | 26 | System.setOut(originalStdOut); |
26 | 27 | } |
27 | 28 |
|
28 | 29 | @Test |
29 | | - void springboot_test_should_log_correct_fields() throws IOException { |
| 30 | + void springboot_test_should_log_correct_fields_including_exception() throws IOException { |
30 | 31 | MDC.put("any-mdc-field", "1234-1234"); |
31 | | - final ByteArrayOutputStream capturedStdOut = captureStdOut(); |
32 | | - log.info("spring boot test message"); |
| 32 | + ByteArrayOutputStream capturedStdOut = captureStdOut(); |
| 33 | + log.info("spring boot test message", new RuntimeException("MyException")); |
| 34 | + captureAsyncLogs(); |
33 | 35 |
|
34 | | - final Map<String, Object> capturedFields = new ObjectMapper().readValue(capturedStdOut.toString(StandardCharsets.UTF_8), new TypeReference<>() { |
| 36 | + String logMessage = capturedStdOut.toString(); |
| 37 | + AssertionsForClassTypes.assertThat(logMessage).isNotEmpty(); |
| 38 | + Map<String, Object> capturedFields = new ObjectMapper().readValue(logMessage, new TypeReference<>() { |
35 | 39 | }); |
36 | | - |
37 | | - assertThat(capturedFields) |
38 | | - .containsEntry("any-mdc-field", "1234-1234") |
39 | | - .containsKey("timestamp") |
40 | | - .containsEntry("logger_name", "uk.gov.hmcts.cp.integration.SpringLoggingIntegrationTest") |
41 | | - .containsEntry("thread_name", "Test worker") |
42 | | - .containsEntry("level", "INFO") |
43 | | - .containsEntry("message", "spring boot test message"); |
| 40 | + assertThat(capturedFields.get("any-mdc-field")).isEqualTo("1234-1234"); |
| 41 | + assertThat(capturedFields.get("timestamp")).isNotNull(); |
| 42 | + assertThat(capturedFields.get("logger_name")).isEqualTo("uk.gov.hmcts.cp.integration.SpringLoggingIntegrationTest"); |
| 43 | + assertThat(capturedFields.get("thread_name")).isEqualTo("Test worker"); |
| 44 | + assertThat(capturedFields.get("level")).isEqualTo("INFO"); |
| 45 | + assertThat(capturedFields.get("message").toString()).contains("spring boot test message\njava.lang.RuntimeException: MyException"); |
| 46 | + assertThat(capturedFields.get("message").toString()).contains("at uk.gov.hmcts.cp.integration.SpringLoggingIntegrationTest"); |
44 | 47 | } |
45 | 48 |
|
46 | 49 | private ByteArrayOutputStream captureStdOut() { |
47 | 50 | final ByteArrayOutputStream capturedStdOut = new ByteArrayOutputStream(); |
48 | | - System.setOut(new PrintStream(capturedStdOut, true, StandardCharsets.UTF_8)); |
| 51 | + System.setOut(new PrintStream(capturedStdOut)); |
49 | 52 | return capturedStdOut; |
50 | 53 | } |
| 54 | + |
| 55 | + private void captureAsyncLogs() { |
| 56 | + AsyncAppender asyncAppender = (AsyncAppender) ((ch.qos.logback.classic.Logger) LoggerFactory |
| 57 | + .getLogger("ROOT")) |
| 58 | + .getAppender("ASYNC_JSON"); |
| 59 | + asyncAppender.stop(); |
| 60 | + } |
51 | 61 | } |
0 commit comments