Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exclude certain HTTP requests from being logged in Java Spring (e.g. /health Endpoint)

In my Spring boot application the following setting:

logging.org.springframework.web: DEBUG

causes all HTTP requests to be logged, which is cool!

However, as the /health endpoint is continuously polled, this spams the logfile. How can I disable request logging just for this endpoint?

like image 349
MonitorLizard Avatar asked Oct 19 '25 08:10

MonitorLizard


1 Answers

I recommend to use Spring Built-In request logging filter CommonsRequestLoggingFilter.

To enable the logging filter, you need to set the property:

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

But when you need specify custom rules for enabling logging, you can create custom implementation in following way:

@Component
public class RequestLoggingFilter extends AbstractRequestLoggingFilter {

    private Set<String> excludedUrls = Set.of("/health");

    @Override
    protected boolean shouldLog(HttpServletRequest request) {
        if(excludedUrls.contains(request.getRequestURI())) {
            return false;
        }
        return logger.isDebugEnabled();
    }

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

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

and to enable the filter add the following config property to application.properties:

logging.level.<package_name>.RequestLoggingFilter=DEBUG
like image 170
saver Avatar answered Oct 20 '25 21:10

saver



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!