How to configure RestTemplate debug logging in log

2019-07-20 05:58发布

问题:

I am using log4j2 with xml configuration. I want to log all the JSON created by restTemplate

How can I configure it in log4j2 xml configuration file to log these?

回答1:

If your RestTemplate uses apache http client, your log4j2.xml configuration could look like this:

<Logger name="org.springframework.web.client" level="DEBUG" additivity="false">
    <AppenderRef ref="APP" level="DEBUG"/>
</Logger>
<Logger name="org.apache.http.wire" level="DEBUG" additivity="false">
    <AppenderRef ref="APP" level="DEBUG"/>
</Logger>

RestTemplate Initialisation:

org.springframework.http.client.HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
requestFactory.setReadTimeout(10000);
requestFactory.setConnectTimeout(10000);
RestTemplate restTemplate = new RestTemplate(requestFactory);