Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/main/resources/logback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@
<encoder class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
<providers>
<mdc/>
<logLevel/>
<timestamp>
<fieldName>timestamp</fieldName>
<pattern>yyyy-MM-dd' 'HH:mm:ss.SSS</pattern>
</timestamp>
<loggerName/>
<threadName/>
<message/>
<logLevel/>
<pattern>
<pattern>{"message": "%message%n%xException{full}"}</pattern>
</pattern>
</providers>
</encoder>
</appender>
<root level="INFO"><appender-ref ref="JSON"/></root>
<root level="INFO">
<appender-ref ref="JSON"/>
</root>
</configuration>
Original file line number Diff line number Diff line change
@@ -1,51 +1,53 @@
package uk.gov.hmcts.cp.integration;

import ch.qos.logback.classic.AsyncAppender;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.assertj.core.api.AssertionsForClassTypes;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.charset.StandardCharsets;
import java.util.Map;

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

@Slf4j
class SpringLoggingIntegrationTest extends IntegrationTestBase {

private final PrintStream originalStdOut = System.out;
private PrintStream originalStdOut = System.out;

@AfterEach
void afterEach() {
System.setOut(originalStdOut);
}

@Test
void springboot_test_should_log_correct_fields() throws IOException {
void springboot_test_should_log_correct_fields_including_exception() throws IOException {
MDC.put("any-mdc-field", "1234-1234");
final ByteArrayOutputStream capturedStdOut = captureStdOut();
log.info("spring boot test message");
ByteArrayOutputStream capturedStdOut = captureStdOut();
log.info("spring boot test message", new RuntimeException("MyException"));

final Map<String, Object> capturedFields = new ObjectMapper().readValue(capturedStdOut.toString(StandardCharsets.UTF_8), new TypeReference<>() {
String logMessage = capturedStdOut.toString();
AssertionsForClassTypes.assertThat(logMessage).isNotEmpty();
Map<String, Object> capturedFields = new ObjectMapper().readValue(logMessage, new TypeReference<>() {
});

assertThat(capturedFields)
.containsEntry("any-mdc-field", "1234-1234")
.containsKey("timestamp")
.containsEntry("logger_name", "uk.gov.hmcts.cp.integration.SpringLoggingIntegrationTest")
.containsEntry("thread_name", "Test worker")
.containsEntry("level", "INFO")
.containsEntry("message", "spring boot test message");
assertThat(capturedFields.get("any-mdc-field")).isEqualTo("1234-1234");
assertThat(capturedFields.get("timestamp")).isNotNull();
assertThat(capturedFields.get("logger_name")).isEqualTo("uk.gov.hmcts.cp.integration.SpringLoggingIntegrationTest");
assertThat(capturedFields.get("thread_name")).isEqualTo("Test worker");
assertThat(capturedFields.get("level")).isEqualTo("INFO");
assertThat(capturedFields.get("message").toString()).contains("spring boot test message\njava.lang.RuntimeException: MyException");
assertThat(capturedFields.get("message").toString()).contains("at uk.gov.hmcts.cp.integration.SpringLoggingIntegrationTest");
}

private ByteArrayOutputStream captureStdOut() {
final ByteArrayOutputStream capturedStdOut = new ByteArrayOutputStream();
System.setOut(new PrintStream(capturedStdOut, true, StandardCharsets.UTF_8));
System.setOut(new PrintStream(capturedStdOut));
return capturedStdOut;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ private void assertTracingFields(final Map<String, Object> log, final String exp

private void assertCommonLogFields(final Map<String, Object> log) {
assertEquals("uk.gov.hmcts.cp.controllers.ExampleController", log.get("logger_name"));
assertEquals(log.get("message"), "getExampleByExampleId example for " + entity.getId());
assertEquals(log.get("message"), "getExampleByExampleId example for " + entity.getId() + "\n");
}

private void assertResponseHeaders(final MvcResult result, final String expectedTraceId, final String expectedSpanId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ void junit_should_log_correct_fields() throws JsonProcessingException {
.containsEntry("logger_name", "uk.gov.hmcts.cp.logging.JunitLoggingTest")
.containsEntry("thread_name", "Test worker")
.containsEntry("level", "INFO")
.containsEntry("message", "junit test message");
.containsEntry("message", "junit test message\n");
}

private ByteArrayOutputStream captureStdOut() {
Expand Down
Loading