Request/Response logging in Spring Boot Application


There are scenarios where you would want to log full request/response payloads in a Spring Boot application. This recipe shows you how to utilize logback to achieve the desired results.

Required Configuration

Below is a sample POM with required dependencies.


Create a logback-access.xml under src/main/resources with the following configuration: You can change what all fields are displayed in access log. For a full list of available fields, please refer to logback documentation.

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
            <pattern>logging uri: %requestURL | status code: %statusCode | bytes: %bytesSent | elapsed time: %elapsedTime | request-log: %magenta(%requestContent) | response-log: %cyan(%responseContent)</pattern>
    <appender-ref ref="STDOUT"/>

Create a Servlet Filter that intercepts the requests:

import ch.qos.logback.access.servlet.TeeFilter;

public class FilterConfiguration {

    public FilterRegistrationBean requestResponseFilter() {

        final FilterRegistrationBean filterRegBean = new FilterRegistrationBean();
        TeeFilter filter = new TeeFilter();
        filterRegBean.setName("Request Response Filter");
        return filterRegBean;

Once this is configured, every request/response payloads (not form POST) will be logged to your default appender.

Enabling/Disabling Logging

There are potential impacts to application performance once this filter is activated since every request/response payload is copied to an in memory buffer and hence additional overheads to garbage collection and CPU. Also, you have to be careful about not logging sensitive data. So, there are configurations available to disable this filter by default.

Add this to your to disable access logging by default:



It would be nice to have the filter skip/obfuscate contents of the request/response based on a pattern. The default TeeFilter can be extended to achieve this result.