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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With