如何在文件中记录 HttpRequest 和 HttpResponse? [英] How to Log HttpRequest and HttpResponse in a file?

查看:33
本文介绍了如何在文件中记录 HttpRequest 和 HttpResponse?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能解释一下在文件中记录 HttpRequest 和 HttpResponse 的任何技术.

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

我们使用的是 Spring MVC/Spring Rest.

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.

非常感谢.

推荐答案

对于记录请求 Spring 有 AbstractRequestLoggingFilter 类(实际上是子类之一).这可用于记录传入请求(处理之前和之后).

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

根据配置,这可以包括有效负载、客户端信息和完整 URL(包括请求参数).所有这三个默认情况下都是禁用的,但可以通过配置启用(有关详细信息,请参阅 javadoc).

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>

过滤器现在将使用 Commons Logging 记录器将所有内容记录到日志文件中.

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

这篇关于如何在文件中记录 HttpRequest 和 HttpResponse?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆