Spring RestTemplate - 如何启用请求/响应的完整调试/日志记录? [英] Spring RestTemplate - how to enable full debugging/logging of requests/responses?

查看:60
本文介绍了Spring RestTemplate - 如何启用请求/响应的完整调试/日志记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用 Spring RestTemplate 一段时间了,当我试图调试它的请求和响应时,我总是碰壁.我基本上希望看到与在打开详细"选项的情况下使用 curl 时看到的相同的内容.例如:

I have been using the Spring RestTemplate for a while and I consistently hit a wall when I'am trying to debug it's requests and responses. I'm basically looking to see the same things as I see when I use curl with the "verbose" option turned on. For example :

curl -v http://twitter.com/statuses/public_timeline.rss

将显示发送的数据和接收的数据(包括标题、cookie 等).

Would display both the sent data and the received data (including the headers, cookies, etc.).

我查看了一些相关的帖子,例如:如何在 Spring RestTemplate 中记录响应?但我还没有设法解决这个问题.

I have checked some related posts like : How do I log response in Spring RestTemplate? but I haven't managed to solve this issue.

这样做的一种方法是实际更改 RestTemplate 源代码并在那里添加一些额外的日志记录语句,但我会发现这种方法确实是最后的手段.应该有一些方法可以告诉 Spring Web Client/RestTemplate 以更友好的方式记录所有内容.

One way to do this would be to actually change the RestTemplate source code and add some extra logging statements there, but I would find this approach really a last resort thing. There should be some way to tell Spring Web Client/RestTemplate to log everything in a much friendlier way.

我的目标是能够使用以下代码来做到这一点:

My goal would be to be able to do this with code like :

restTemplate.put("http://someurl", objectToPut, urlPathValues);

然后在日志文件或控制台中获取相同类型的调试信息(就像我使用 curl 得到的那样).我相信这对于使用 Spring RestTemplate 并且有问题的任何人都非常有用.使用 curl 调试您的 RestTemplate 问题是行不通的(在某些情况下).

and then to get the same type of debug information (as I get with curl) in the log file or in the console. I believe this would be extremely useful for anyone that uses the Spring RestTemplate and has problems. Using curl to debug your RestTemplate problems just doesn't work (in some cases).

推荐答案

我终于找到了正确的方法.大多数解决方案来自如何配置 Spring 和SLF4J 以便我可以进行日志记录?

I finally found a way to do this in the right way. Most of the solution comes from How do I configure Spring and SLF4J so that I can get logging?

看来有两件事需要做:

  1. 在 log4j.properties 中添加以下行:log4j.logger.httpclient.wire=DEBUG
  2. 确保 spring 不会忽略您的日志记录配置

第二个问题主要发生在使用 slf4j 的 spring 环境中(就像我的情况一样).因此,当使用 slf4j 时,请确保发生以下两件事:

The second issue happens mostly to spring environments where slf4j is used (as it was my case). As such, when slf4j is used make sure that the following two things happen :

  1. 您的类路径中没有 commons-logging 库:这可以通过在 pom 中添加排除描述符来完成:

  1. There is no commons-logging library in your classpath : this can be done by adding the exclusion descriptors in your pom :

        <exclusions><exclusion>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
        </exclusion>
    </exclusions>

  • log4j.properties 文件存储在 spring 可以找到/查看它的类路径中的某个位置.如果您对此有问题,最后的解决方案是将 log4j.properties 文件放在默认包中(不是一个好的做法,只是为了看看事情是否如您所愿)

  • The log4j.properties file is stored somewhere in the classpath where spring can find/see it. If you have problems with this, a last resort solution would be to put the log4j.properties file in the default package (not a good practice but just to see that things work as you expect)

    这篇关于Spring RestTemplate - 如何启用请求/响应的完整调试/日志记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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