get ServletResponse content string for logging

10,163

For a custom solution here is the idea.

In the filter you need to wrap the response and intercept the output of the servlet, so you can write it.

public void doFilter(ServletRequest request, 
                     ServletResponse response, 
                     FilterChain chain) throws ServletException, IOException {

    // Wrap the response
    MyResponseWrapper responseWrapper = 
                         new MyResponseWrapper((HttpServletResponse) response);

    chain.doFilter(request, responseWrapper);
}

Where MyResponseWrapper is a class where you can intercept all calls to output stream

public class MyResponseWrapper implements HttpServletResponse {
     private HttpServletResponse response;
     private ServletOutputStream outputStream;
     private MyOutputStreamCopier myOutputStreamCopier;

     public MyResponseWrapper(HttpServletResponse response) {
         this.response = response;
     }

     // Implements all needed methods

     // Write methods like the following to redirect output to your logs
     public ServletOutputStream getOutputStream() throws IOException {
         if (outputStream == null) {
             outputStream = getResponse().getOutputStream();
             copier = new MyOutputStreamCopier(outputStream);
         }

         return copier;
    }
}

Where MyOutputStreamCopier is a custom class that extends ServletOutputStream and copy all writes to a local buffer (or directly log it to a log file).


If you like an already build solution take a look at this link

Share:
10,163
Armen
Author by

Armen

Updated on June 05, 2022

Comments

  • Armen
    Armen almost 2 years

    I have a RESTful service developed by Java Spring. I created some filters for authentications, get some custom headers, etc. etc. in one of the filters I need to log incoming Request and outgoing Response. Right now I stuck in logging the response. here is the filter

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException 
    

    servletResponse is the object that I need to get the content.

    I know there are some duplicate questions that already answered but for some reason none of those works for me.

    I have a filter and in doFilter method after chain.doFilter I want to log the response content.

    I can see the JSON object when I'm inspecting the response in outputStream or in writer. But I cannot get it programmatically.

    Can someone help me to resolve this? Thank you in advance!

    Here are the links that I used and didn't help:

    How to read and copy the HTTP servlet response output stream content for logging

    Capture and log the response body

    Logging response body (HTML) from HttpServletResponse using Spring MVC HandlerInterceptorAdapter

  • Armen
    Armen about 8 years
    Lorenzo how can I get the content after doFilter?
  • Davide Lorenzo MARINO
    Davide Lorenzo MARINO about 8 years
    @Armen basically MyOutputStreamCopier needs to buffer the data sent to the output, so you can use them in a second moment. Probably you can add a method String getOutputContent() to retrieve the content as a String. Otherwise you can write directly in the log file in the class MyOutputStreamCopier.