CommonsRequestLoggingFilter not working in spring boot application

Viewed 13578

I have added this @Bean to the class I have main function in

@Bean
    public CommonsRequestLoggingFilter requestLoggingFilter() {
         System.out.println("inside logging filter");
        CommonsRequestLoggingFilter loggingFilter = new CommonsRequestLoggingFilter();
        loggingFilter.setIncludeClientInfo(true);
        loggingFilter.setIncludeQueryString(true);
        loggingFilter.setIncludePayload(true);
        loggingFilter.setIncludeHeaders(false);
        return loggingFilter;
    }

On application start,

inside logging filter

gets printed in console but I do not see any logging of requests when I call method from a RestController.

Why is that? How do I fix it?

I have already added

logging.level.org.springframework.web.filter.CommonsRequestLoggingFilter=DEBUG

in application.properties file

5 Answers

I have been the same situation and I could resolve it with removing log setting.

logging.level.org.springframework.web.filter.CommonsRequestLoggingFilter=DEBUG

It runs without this setting.

Also, if you want to change log level, you can define the class extends AbstractRequestLoggingFilter and write logging like this.

@Override
protected void beforeRequest(HttpServletRequest request, String message) {
    logger.info(message);
}

@Override
protected void afterRequest(HttpServletRequest request, String message) {
    logger.info(message);
}

Try changing the level to TRACE in your application.properties file:

logging.level.org.springframework.web.filter.CommonsRequestLoggingFilter=TRACE

DEBUG didn't work for me, but TRACE did.

Fyi if you see an extra ] at the end of the json body you can remove it like this:

loggingFilter.setAfterMessageSuffix("");

In principle you just need to enable debug on the logger of choice. In effect it is hard to know what logger exatly is used by Spring. The safe way to do it is to override AbstractRequestLoggingFilter and set the logger to a logger of choice. Alternatively is is possible you just override the 'shouldLog' method to return 'true'.

Example in Kotlin

package org.ultra-marine.logging

import org.slf4j.LoggerFactory
import org.springframework.web.filter.AbstractRequestLoggingFilter
import javax.servlet.http.HttpServletRequest

class RequestLoggingFilter: AbstractRequestLoggingFilter() {

    private val log = LoggerFactory.getLogger(this::class.java)

    override fun shouldLog(request: HttpServletRequest): Boolean {
        return log.isDebugEnabled
    }

    /**
     * Writes a log message before the request is processed.
     */
    override fun beforeRequest(request: HttpServletRequest, message: String) {
        log.debug(message)
    }

    /**
     * Writes a log message after the request is processed.
     */
    override fun afterRequest(request: HttpServletRequest, message: String) {
        log.debug(message)
    }

}

The Filter need to be initialized using a Bean in the same way demonstrated in other places.

@Configuration
class SpringBootRequestLoggingConfiguration {
    @Bean
    fun requestLoggingFilter(): RequestLoggingFilter {
        val filter = RequestLoggingFilter()
        filter.setIncludeClientInfo(false)
        filter.setIncludeQueryString(true)
        filter.setIncludePayload(false)
        filter.setMaxPayloadLength(8000)
        filter.setIncludeHeaders(false)

        return filter
    }

}

you should set the MaxPayloadLength,if you use CommonsRequestLoggingFilter

 filter.setMaxPayloadLength(2048);

Or you can implements Filter,then you can customize yourself Filter.

Define a MultiReadHttpServletRequest extends HttpServletRequestWrapper

slove the getInputStream problem.

Related