Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
704 views
in Technique[技术] by (71.8m points)

spring - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...