Skip to content

Commit 9b331e1

Browse files
committed
chore: update logback.xml to include exception and async
1 parent e78f27e commit 9b331e1

1 file changed

Lines changed: 26 additions & 16 deletions

File tree

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,61 @@
11
package uk.gov.hmcts.cp.integration;
22

3+
import ch.qos.logback.classic.AsyncAppender;
34
import com.fasterxml.jackson.core.type.TypeReference;
45
import com.fasterxml.jackson.databind.ObjectMapper;
56
import lombok.extern.slf4j.Slf4j;
7+
import org.assertj.core.api.AssertionsForClassTypes;
68
import org.junit.jupiter.api.AfterEach;
79
import org.junit.jupiter.api.Test;
10+
import org.slf4j.LoggerFactory;
811
import org.slf4j.MDC;
912

1013
import java.io.ByteArrayOutputStream;
1114
import java.io.IOException;
1215
import java.io.PrintStream;
13-
import java.nio.charset.StandardCharsets;
1416
import java.util.Map;
1517

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

1820
@Slf4j
1921
class SpringLoggingIntegrationTest extends IntegrationTestBase {
20-
21-
private final PrintStream originalStdOut = System.out;
22+
private PrintStream originalStdOut = System.out;
2223

2324
@AfterEach
2425
void afterEach() {
2526
System.setOut(originalStdOut);
2627
}
2728

2829
@Test
29-
void springboot_test_should_log_correct_fields() throws IOException {
30+
void springboot_test_should_log_correct_fields_including_exception() throws IOException {
3031
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();
3335

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<>() {
3539
});
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");
4447
}
4548

4649
private ByteArrayOutputStream captureStdOut() {
4750
final ByteArrayOutputStream capturedStdOut = new ByteArrayOutputStream();
48-
System.setOut(new PrintStream(capturedStdOut, true, StandardCharsets.UTF_8));
51+
System.setOut(new PrintStream(capturedStdOut));
4952
return capturedStdOut;
5053
}
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+
}
5161
}

0 commit comments

Comments
 (0)