__CONFIG_colors_palette__{"active_palette":0,"config":{"colors":{"40f3f":{"name":"Main Accent","parent":-1}},"gradients":[]},"palettes":[{"name":"Default","value":{"colors":{"40f3f":{"val":"var(--tcb-color-0)"}},"gradients":[]},"original":{"colors":{"40f3f":{"val":"rgb(127, 215, 132)","hsl":{"h":123,"s":0.52,"l":0.67,"a":1}}},"gradients":[]}}]}__CONFIG_colors_palette__

__CONFIG_colors_palette__{"active_palette":0,"config":{"colors":{"40f3f":{"name":"Main Accent","parent":-1}},"gradients":[]},"palettes":[{"name":"Default","value":{"colors":{"40f3f":{"val":"var(--tcb-color-0)"}},"gradients":[]},"original":{"colors":{"40f3f":{"val":"rgb(127, 215, 132)","hsl":{"h":123,"s":0.52,"l":0.67,"a":1}}},"gradients":[]}}]}__CONFIG_colors_palette__
Table of Contents

How to Log Incoming Requests In Spring

In this post, we will explore as of how to Log Incoming Requests In Spring. We will explore different options to accomplish it along with the build in feature provided by Spring.


1. Introduction

Having the ability to log incoming request in a web application is a very common requirement for modern web applications. In this article, we will be covering how to do it using Spring’s logging filter.


2. Dependency Management

In order to add required logging dependencies, we can add spring-core, for this article, we will be using Spring Boot which will handle dependency management for us. Checkout Building an Application with Spring Boot to learn about Spring Boot dependency management. We will add Spring Boot dependencies to start our web application.



3. Web Controller

In order to log incoming request, we need to have a Spring Controller in place, we will be using a simple controller for our post. Read  Creating a Web Application with Spring Boot to get an understanding of creating a web application using Spring Boot.

public class LoggingDemoController {

    public String sayHello(){
        return "Hello Stranger !!!";

There is nothing special with this controller and it is simply returning "Hello Stranger !!! " to the client.


4. Custom Solutions

Spring provides interceptors to perform actions before and after web request. You can use HandlerInterceptor to create your custom implementation to log incoming requests in Spring.

You have to be careful while using such approach as input stream will be marked as consumed the moment it is read for the first time. In order to use this approach, we need to extend HandlerInterceptorAdapter and override the following two methods.

  • preHandle() – This is executed before the actual method call.
  • afterCompletion() – Method executed after the method call and our service is ready to send the response.

Alternatively, we can implement the HandlerInterceptor and provide implementation for the above two methods. Let’s have a look at the custom handler interceptor.

package com.example;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.Instant;
import java.time.LocalDateTime;

public class CustomRequestInterceptor extends HandlerInterceptorAdapter {

 private static final Logger logger = LoggerFactory.getLogger(CustomRequestInterceptor.class);

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

  long startTime = Instant.now().toEpochMilli();
  logger.info("Request URL::" + request.getRequestURL().toString() +
   ":: Start Time=" + Instant.now());
  request.setAttribute("startTime", startTime);
  return true;

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

  long startTime = (Long) request.getAttribute("startTime");

  logger.info("Request URL::" + request.getRequestURL().toString() +
   ":: Time Taken=" + (Instant.now().toEpochMilli() - startTime));

As the last step, we need to register our custom interceptor usingaddInterceptors method.

public class RequestAppConfig implements WebMvcConfigurer {

 private CustomRequestInterceptor customRequestInterceptor;

 public void addInterceptors(InterceptorRegistry registry) {

WebMvcConfigurer adds theCustomRequestInterceptor to the spring MVC lifecycle by invoking addInterceptors() method. When we run our application, we can see the following output in the console

2018-09-30 12:02:09.704  INFO 51707 --- [nio-8080-exec-2] com.example.CustomRequestInterceptor     : Request URL::http://localhost:8080/log-incoming-request:: Start Time=2018-09-30T06:32:08.861Z
2018-09-30 12:02:16.820  INFO 51707 --- [nio-8080-exec-2] com.example.CustomRequestInterceptor     : Request URL::http://localhost:8080/log-incoming-request:: Time Taken=9942


We can also use Spring’s ContentCachingRequestWrapper andContentCachingResponseWrapper to work for caching the request data for logging purpose.


5. Spring Built-In Request Logging

The Spring framework comes with ready to use a feature which can log your request, all we are required to configure this ready to use solution. Spring comes with AbstractRequestLoggingFilter, that perform logging operations before and after a request is processed.

Before we get into implementation details, this filter requires a subclass to override the beforeRequest(HttpServletRequest, String) and afterRequest(HttpServletRequest, String) methods to perform the actual logging around the request.

Spring provides the following 2 implementations for AbstractRequestLoggingFilter

  1. CommonsRequestLoggingFilter
  2. ServletContextRequestLoggingFilter


ServletContextRequestLoggingFilter Simple request logging filter that writes the request URI (and optionally the query string) to the ServletContext log. We are going to discuss CommonsRequestLoggingFilter in this post.


5.1 CommonsRequestLoggingFilter using Spring Boot

Spring Boot is the new way to create and run your Spring-powered applications, we can enable CommonsRequestLoggingFilter by simply registering it as a bean with our application.

public CommonsRequestLoggingFilter requestLoggingFilter() {
    CommonsRequestLoggingFilter loggingFilter = new CommonsRequestLoggingFilter();
    return loggingFilter;

In addition to the above configuration, we need to make sure to set log level as DEBUG for CommonsRequestLoggingFilter either through application.properties or YAML


Once these configurations are in place, you should be able to see a similar output in the console

2017-10-25 19:52:02.708 DEBUG 70034 --- [io-10070-exec-4] o.s.w.f.CommonsRequestLoggingFilter      : Before request [uri=/demo/greeting;client=0:0:0:0:0:0:0:1]
2017-10-25 19:52:02.791 DEBUG 70034 --- [io-10070-exec-4] o.s.w.f.CommonsRequestLoggingFilter      : After request [uri=/demo/greeting;client=0:0:0:0:0:0:0:1]

And voila, your requests are visible in the console as well on the log files.

5.2 CommonsRequestLoggingFilter without Spring Boot

If you are not using Spring Boot, You can configure this by using traditional Filter. We have the following options to configure this in our traditional web application

  1. Configure this Filter either through xml configuration or Java configuration with default values.
  2. Create a custom filter by extending CommonsRequestLoggingFilter to modify default behaviour.


5.2.1 CommonsRequestLoggingFilter using XML

If you want to use CommonsRequestLoggingFilter with no changes, you can simply configure it in your application configuration file as a filer

5.2.2 CommonsRequestLoggingFilter using Java Web Initializer

If you are not a fan of using XML configuration for your web application, Spring provides a way to configure it using WebApplicationInitializer. Please note that WebApplicationInitializer Interface to be implemented in Servlet 3.0+ environments in order to configure the ServletContext programmatically.

public class MyWebAppInitializer implements WebApplicationInitializer {

    public void onStartup(ServletContext container) {
        XmlWebApplicationContext appContext = new XmlWebApplicationContext();

        ServletRegistration.Dynamic dispatcher =
                container.addServlet("dispatcher", new DispatcherServlet(appContext));

        container.addFilter("requestLoggingFilter", CommonsRequestLoggingFilter.class)
                .addMappingForServletNames(null, false, "dispatcher");

5.2.3 Custom CommonsRequestLoggingFilter

If you want to customize the behavior of CommonsRequestLoggingFilter, you can always create your custom Filter by extending CommonsRequestLoggingFilter

public class CustomeRequestLoggingFilter extends CommonsRequestLoggingFilter {

    public CustomeRequestLoggingFilter(){

You can use any of the above options to configure your custom Filter. For more details, read CommonsRequestLoggingFilter



In this post, we explore as of how to Log Incoming Request in Spring. Spring comes with many hidden features which can always help us to avoid writing custom/duplicate code and CommonsRequestLoggingFilter is one of such hidden gem in Spring.

0 0 vote
Article Rating

Related Posts

Java Development Journal

Hello!! Welcome to the Java Development Journal. We love to share our knowledge with our readers and love to build a thriving community.

Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Newest Most Voted
Inline Feedbacks
View all comments
{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}
Would love your thoughts, please comment.x