How to Log Request & Response in Spring Boot Applications

Logging is essential for monitoring and troubleshooting running applications. This guide explains how to utilize logback to collect full request/response payloads in a Spring Boot application.

Getting Started

To begin, you create a Maven Project Object Model to enable logback. A Project Object Model or POM is an XML file that contains information about the project and configuration details. Below is a sample specifying the required dependencies.


Next create a logback-access.xml under src/main/resources.You can change the fields displayed in an access log. For a full list of available fields refer to the 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"/>

Capturing Request/Response

It is often useful to capture both the client’s request and the server’s response when diagnosing bugs. The TeeFilter servlet filter accomplishes this.

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 payload is logged to your default appender.

Enabling or Disabling Logging

There are potential impacts to application performance when this filter is activated. Every request/response payload is copied to an in-memory buffer, creating additional garbage collection and CPU overhead. To reduce overhead or to avoid logging sensitive data, add the following to your to deactivate access logging by default:


Keep Learning

You can find out more by reading the Spring Boot documentation on logging or the full logback manual.

Logging has three elements: collection, indexing, and visualization. This guide explains the first element, collection. For indexing and visualization, there’s a wide ecosystem of open-source technologies that can be used. For example, the “EFK stack” (Elasticsearch, Fluentd, and Kibana) is popular for solving this problem.

Two open-source tools that help with logging and visualization are Prometheus and Grafana. Prometheus excels at gathering metrics from a wide array of sources, while Grafana is the go-to tool for visualizing complex time-series data. The following guides explain how to use these tools in Kubernetes environments:

Spring Boot also provides health checks for application monitoring in addition to logging. Learn how to enable health checks using Spring Boot Actuator.

Frequently Asked Questions

How do log requests and responses in Spring Boot work?

Log requests and responses in a Spring Boot application work by utilizing logback to collect full payloads, an essential part of monitoring and troubleshooting running applications.

How do you get Spring Boot logs?

Spring Boot logs can be obtained by creating a Maven Project Object Model and enabling logback.

What logging does Spring Boot use?

Spring Boot utilizes Apache Commons for internal logging and is also configured to support Logback, Log4j2, and Java Util Logging for console and file logging.

How do you manage logs in Spring Boot microservices

Logs in Spring Boot can be managed by enabling logback in a POM, containing configuration details and other vital information about the project. Additionally, Prometheus and Grafana can also be utilized when trying to visualize data and metrics.

How do you capture both requests and responses when diagnosing bugs in a Spring Boot application?

Capturing client’s requests and server’s response when diagnosing bugs can be accomplished with the TeeFilter servlet.

How do you avoid logging sensitive data in Spring Boot?

Enabling or disabling access logging can help users avoid logging sensitive data in Spring Boot.