Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Log HttpRequest and HttpResponse in a file?

Can anyone explain any techniques to log HttpRequest and HttpResponse in a file.

We are using Spring MVC/Spring Rest.

What we want is to intercept the request before it is processed and log it. Same way intercept the response before it is sent and log it.

Thanks a lot in advance.

like image 253
Harsh M Avatar asked Sep 18 '14 06:09

Harsh M


People also ask

What is Httprequest and HTTPResponse?

HTTP Response sent by a server to the client. The response is used to provide the client with the resource it requested. It is also used to inform the client that the action requested has been carried out. It can also inform the client that an error occurred in processing its request.

What is log HTTP request?

HTTP Logging is a middleware that logs information about HTTP requests and HTTP responses. HTTP logging provides logs of: HTTP request information. Common properties. Headers.

What is a HTTPResponse object?

The HTTPResponse object contains all outgoing response data. It consists of the HTTP status code and message, the HTTP headers, and any response body data. HTTPResponse also contains the ability to stream or push chunks of response data to the client, and to complete or terminate the request.


2 Answers

For logging the request Spring has the AbstractRequestLoggingFilter class (well actually one of the subclasses). This can be used to log the incoming request (before and after processing).

Depending on the configuration this can include the payload, client information and full URL (including erquest parameters). All these three are disabled by default but can be enabled through configuration (see the javadoc for more information).

<filter>
    <filter-name>requestLoggingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CommonsRequestLoggingFilter</filter-class>
    <init-param>
        <param-name>includeClientInfo</param-name>
        <param-value>true</param-value>
    </init-param>
    <init-param>
        <param-name>includePayload</param-name>
        <param-value>true</param-value>
    </init-param>
    <init-param>
        <param-name>includeQueryString</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>

<filter-mapping>
    <filter-name>requestLoggingFilter</filter-name>
    <servlet-name>dispatcherServlet</servlet-name>
</filter-mapping>

The filter will now log everything using a Commons Logging logger to a logfile.

like image 94
M. Deinum Avatar answered Oct 24 '22 04:10

M. Deinum


Accepted answer is already correct, adding annotation based configuration. Add following bean to your config.

@Bean
public CommonsRequestLoggingFilter requestLoggingFilter() {
    CommonsRequestLoggingFilter loggingFilter = new CommonsRequestLoggingFilter();
    loggingFilter.setIncludeClientInfo(true);
    loggingFilter.setIncludeQueryString(true);
    loggingFilter.setIncludePayload(true);
    return loggingFilter;
}
like image 29
Yogesh Badke Avatar answered Oct 24 '22 03:10

Yogesh Badke