1

I'm working on rest api with spring boot. I need to log all requests with input params (with methods, eg. GET, POST, etc), request Url, query string, , also response of this action, both success and errors and Status code.

I need to log not only incoming https request/response but also outgoing https requests/response. I have to log request/response with json format. Could you tell me the best practice in spring to achieve this, and with concrete example please ?

4
  • Are you using embeded jetty or external tomcat? Commented Sep 11, 2017 at 18:22
  • I'm using external tomcat. Commented Sep 11, 2017 at 20:13
  • Which HTTP client are you using? Commented Sep 12, 2017 at 0:45
  • I'm using import org.springframework.web.client.RestTemplate for Rest API and org.springframework.ws.client.core.WebServiceTemplate for SOAP. Commented Sep 12, 2017 at 11:08

2 Answers 2

2

There is org.springframework.web.filter.CommonsRequestLoggingFilter that you can use to log query strings, headers and body content before and after the request.

// filter will be applied to all paths
@Bean
public CommonsRequestLoggingFilter loggingFilter() {
    CommonsRequestLoggingFilter filter = new CommonsRequestLoggingFilter();
    filter.setIncludeQueryString(true);
    filter.setIncludeHeaders(true);
    filter.setIncludePayload(true);
    filter.setMaxPayloadLength(1000); // default is 50 bytes
    return filter;
}

There are a few additional settings. Also, if this implementation is not suitable you can inherit from org.springframework.web.filter.AbstractRequestLoggingFilter and override methods to make it suite your requirements.

It makes use of the org.springframework.web.util.ContentCachingRequestWrapper which allows you to read the request content many times.

Sign up to request clarification or add additional context in comments.

Comments

0

Usually this can be accomplished by using a servlet filter. A servlet filter "intercepts" the requests on it's way in and the response on it's way out. You can access all the information you need from the HttpServletRequest and HttpServletResponse and log it however you want.

Simple example:

public class LoggingFilter implements javax.servlet.Filter {

private static final Logger LOGGER = LoggerFactory.getLogger(LoggingFilter.class);

@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
    HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
    HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;

    // log information from the httpServletRequest like url, params, etc
    filterChain.doFilter(servletRequest, servletResponse);
    // log information regarding the httpServletResponse like status code, etc
}
}

Registering the filter in your @SpringBootApplication-annotated class:

@Bean
public FilterRegistrationBean loadBalancerHealthCheckHandler() {
    FilterRegistrationBean registration = new FilterRegistrationBean();
    registration.setFilter(new LoggingFilter());
    registration.addUrlPatterns("/*");
    return registration;
}

2 Comments

Thanks jorgen.ringen. I can use this for incoming requests but how to intercept requests in it's way out ?
This is a good start. This is also how I currently accomplish the same. But there is an issue with accessing the request body ahead of the Spring MVC. The HttpServletRequest only coughs up the request body once via an InputStream. You have to do some trickery to cache the request body so that it can be accessed multiple times.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.