Search code examples
springspring-bootlogging

CommonsRequestLoggingFilter not logging when override


The following code does not log when I add in the override methods. If I replace the code without the override the log works fine. Has seen in multiple conversations, I added the following properties: logging.level.com.mypackage.myproject.RequestLoggingConfiguration=TRACE(or DEBUG) logging.level.org.springframework.web.filter.CommonsRequestLoggingFilter=TRACE

Using Spring Boot 3.2.3 And spring-boot-log4j2 dependency.

I'm just tryna remove the after log from the Logger, but the logger doesn't seems to work anymore...

Here is the code :

    @Bean 
    public CommonsRequestLoggingFilter logFilter() {
    CommonsRequestLoggingFilter filter = new CommonsRequestLoggingFilter() { 
            @Override 
            protected void afterRequest(HttpServletRequest request, String message) { 
                 
            }
    };

    filter.setIncludeClientInfo(true);
    filter.setIncludeHeaders(true);
    filter.setIncludePayload(true);
    filter.setIncludeQueryString(true);
    filter.setBeforeMessagePrefix("Request started => ");
    filter.setAfterMessagePrefix("Request ended => ");
    
    
    return filter;

}

In contrast, the following code successfully logs the output:

@Bean 
public CommonsRequestLoggingFilter logFilter() {
    CommonsRequestLoggingFilter filter = new CommonsRequestLoggingFilter();
    filter.setIncludeQueryString(true);
    filter.setIncludePayload(true); filter.setMaxPayloadLength(10000);
    filter.setIncludeHeaders(false);
    filter.setAfterMessagePrefix("REQUEST DATA: ");
    return filter;
}

log4j2.xml

<logger name="org.springframework.web.filter.CommonsRequestLoggingFilter">
    <level value="DEBUG" />
</logger>

Solution

  • I think that the problem lies in the fact that Spring expects to use the logger that has exactly the same class name as stated in the configuration. When you create an anonymous class inline, you actually create a class that has slightly different class name. Whatever the class name of the anonymous class, it's certainly different from the class name in the configuration. But if you create a real class that extends the CommonsRequestLoggingFilter and use that real class name in the configuration, everything works as expected :)

    So, this will work (note that my root package is rs.in.luka.*, I have checked the solution on my own code):

    MovieRequestLoggingFilter.java

    package rs.in.luka.filters;
    
    import org.springframework.web.filter.CommonsRequestLoggingFilter;
    
    import jakarta.servlet.http.HttpServletRequest;
    
    public class MovieRequestLoggingFilter extends CommonsRequestLoggingFilter {
      @SuppressWarnings("null")
      @Override
      protected void afterRequest(HttpServletRequest request, String message) {
      }
    }
    

    WebConfig.java

    package rs.in.luka;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    import rs.in.luka.filters.MovieRequestLoggingFilter;
    
    @Configuration
    public class WebConfig {
      @Bean
      public MovieRequestLoggingFilter requestLoggingFilter() {
          MovieRequestLoggingFilter loggingFilter = new MovieRequestLoggingFilter();
          loggingFilter.setBeforeMessagePrefix("REQUEST [");
          loggingFilter.setIncludeClientInfo(true);
          loggingFilter.setIncludeQueryString(true);
          loggingFilter.setIncludeHeaders(false);
          loggingFilter.setIncludePayload(true);
          loggingFilter.setMaxPayloadLength(10000);
          return loggingFilter;
      }
    }
    

    In your application.properties file just add the following line:

    logging.level.rs.in.luka.filters.MovieRequestLoggingFilter=DEBUG
    

    After that you will see only BEFORE log lines.