Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.baeldung.httplogging;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

@RestController
@RequestMapping("/api/books")
public class BookController {

@PostMapping
public ResponseEntity<BookCreatedResponse> create(@RequestBody CreateBookRequest request) {
BookCreatedResponse response = new BookCreatedResponse(
UUID.randomUUID(),
request.title(),
request.author()
);

return ResponseEntity.status(HttpStatus.CREATED).body(response);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.baeldung.httplogging;

import java.util.UUID;

public record BookCreatedResponse(UUID id, String title, String author) { }
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.baeldung.httplogging;

import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;
import org.springframework.web.util.ContentCachingRequestWrapper;
import org.springframework.web.util.ContentCachingResponseWrapper;

import java.io.IOException;

@Component
public class CachingHttpFilter extends OncePerRequestFilter {

@Override
protected void doFilterInternal(
HttpServletRequest request,
HttpServletResponse response,
FilterChain filterChain) throws ServletException, IOException {

ContentCachingRequestWrapper cachingRequest =
new ContentCachingRequestWrapper(request);

ContentCachingResponseWrapper cachingResponse =
new ContentCachingResponseWrapper(response);

try {
filterChain.doFilter(cachingRequest, cachingResponse);
} finally {
cachingResponse.copyBodyToResponse();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.baeldung.httplogging;

public record CreateBookRequest(String title, String author) { }
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.baeldung.httplogging;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class HttpLoggingApplication {

public static void main(String[] args) {
SpringApplication.run(HttpLoggingApplication.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.baeldung.httplogging;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.util.ContentCachingRequestWrapper;
import org.springframework.web.util.ContentCachingResponseWrapper;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;

@Component
public class HttpLoggingInterceptor implements HandlerInterceptor {

private static final Logger log =
LoggerFactory.getLogger(HttpLoggingInterceptor.class);

@Override
public boolean preHandle(
HttpServletRequest request,
HttpServletResponse response,
Object handler) {

log.info("Incoming {} {}", request.getMethod(), request.getRequestURI());
return true;
}

@Override
public void afterCompletion(
HttpServletRequest request,
HttpServletResponse response,
Object handler,
Exception ex) {

String requestBody = extractRequestBody(request);
String responseBody = extractResponseBody(response);

log.info(
"HTTP {} {} status={} requestBody={} responseBody={}",
request.getMethod(),
request.getRequestURI(),
response.getStatus(),
requestBody,
responseBody
);
}

private String extractRequestBody(HttpServletRequest request) {
if (request instanceof ContentCachingRequestWrapper wrapper) {
byte[] buf = wrapper.getContentAsByteArray();
var encoding = request.getCharacterEncoding();
return getStringValueFromBuffer(buf, encoding);
}
return "";
}

private String extractResponseBody(HttpServletResponse response) {
if (response instanceof ContentCachingResponseWrapper wrapper) {
byte[] buf = wrapper.getContentAsByteArray();
var encoding = response.getCharacterEncoding();
return getStringValueFromBuffer(buf, encoding);
}
return "";
}

private String getStringValueFromBuffer(byte[] buffer, String encoding) {
if (buffer.length > 0) {
try {
return new String(buffer, encoding != null ? encoding : StandardCharsets.UTF_8.name());
} catch (UnsupportedEncodingException ex) {
return "[unknown-encoding]";
}
}
return "";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.baeldung.httplogging;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {

private final HttpLoggingInterceptor loggingInterceptor;

public WebConfig(HttpLoggingInterceptor loggingInterceptor) {
this.loggingInterceptor = loggingInterceptor;
}

@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(loggingInterceptor)
.addPathPatterns("/api/**");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.baeldung.httplogging;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.system.CapturedOutput;
import org.springframework.boot.test.system.OutputCaptureExtension;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;

import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@SpringBootTest
@AutoConfigureMockMvc
@ExtendWith(OutputCaptureExtension.class)
class BookControllerLoggingTest {

@Autowired
private MockMvc mockMvc;

@Test
void whenCreateBook_thenRequestAndResponseAreLogged(CapturedOutput output) throws Exception {
String requestBody = """
{ "title": "Spring in Action", "author": "Craig Walls" }
""";

mockMvc.perform(post("/api/books")
.contentType(MediaType.APPLICATION_JSON)
.content(requestBody))
.andExpect(status().isCreated());

assertThat(output).contains("Incoming POST /api/books");
assertThat(output).contains("HTTP POST /api/books status=201");
assertThat(output).contains("\"title\":\"Spring in Action\"");
assertThat(output).contains("\"author\":\"Craig Walls\"");
}
}