13

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 5

5

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);
}
3
  • This solution works fine!! But in logs i see there is extra ] is added at the end of json object. Any idea on this? Dec 5, 2019 at 5:22
  • your solution is just lucky coincidence. this is because it is not controlled by 'org.springframework.web.filter' logger package anymore. i faced it after upgrading to boot 2.3. now it is controlled by package where bean creation is declared. and i guess this package was configured to log at DEBUG level. Jul 20, 2021 at 15:41
  • CommonsRequestLoggingFilter couldn't write the body of failed HTTP request Aug 12, 2021 at 7:19
3

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
    }

}
1

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.

1
  • 1
    I'm not sure who gave you -1, so i upvoted your comment to fix injustice. This was also the solution for me. Thanks!
    – Игор
    Oct 28, 2021 at 9:21
1

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

loggingFilter.setAfterMessageSuffix("");
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.