You can add your own logging filter to the processing chain. With spring boot simply by providing the following for example:
import javax.servlet.Filter;
...
@Configuration / @SpringBootApplication
class Something {
@Bean
public Filter loggingFilter(){
return new AbstractRequestLoggingFilter() {
private final Logger log = LoggerFactory
.getLogger(Something.class);
{
setIncludeClientInfo(true);
setIncludeQueryString(true);
setIncludePayload(true);
}
@Override
protected void beforeRequest(HttpServletRequest request, String message) {
// not needed
}
@Override
protected void afterRequest(HttpServletRequest request, String message) {
log.info(message);
}
};
}
results in something similar to
{timestamp and such} : After request [uri=/v1/thing/1007?null;client=127.0.0.1;payload= {
"name": "test7"
]
with
{
"name": "test7"
being a malformed body. This isn't the nicest of filters since it omits useful information such as the http method (POST, GET, ..) the time it took for the request, the response, ... But if you look at it's source you can easily write a nicer one that fits your needs. Especially since ContentCachingRequestWrapper
and ContentCachingResponseWrapper
allow you to get a copy of request and response very easily.
Besides servlet filters you can also add a HandlerInterceptor
like https://stackoverflow.com/a/28022330 - There you have access to SpringMVC specific details but you can't access request & response content without consuming them (compare https://stackoverflow.com/a/2171633 )