Skip to content

markhobson/spring-rest-template-logger

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

46 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Spring RestTemplate Logger

Spring RestTemplate customizer to log HTTP traffic.

Getting started

Add a dependency to your Maven project:

<dependency>
	<groupId>org.hobsoft.spring</groupId>
	<artifactId>spring-rest-template-logger</artifactId>
	<version>2.0.0</version>
</dependency>

Customize your RestTemplate as follows:

RestTemplate restTemplate = new RestTemplateBuilder()
	.customizers(new LoggingCustomizer())
	.build();

Ensure that debug logging is enabled in application.properties:

logging.level.org.hobsoft.spring.resttemplatelogger.LoggingCustomizer = DEBUG

Now all RestTemplate HTTP traffic will be logged to org.hobsoft.spring.resttemplatelogger.LoggingCustomizer at debug level:

2020-02-09 10:59:48.954 DEBUG 17602 --- [nio-8080-exec-1] o.h.s.r.LoggingCustomizer                : Request: GET http://example.com/ 
2020-02-09 10:59:49.291 DEBUG 17602 --- [nio-8080-exec-1] o.h.s.r.LoggingCustomizer                : Response: 200 <!doctype html>
<html>
<head>
    <title>Example Domain</title>
...

See the demo Spring Boot application to see this in action.

Configuration

Using a different logger

To log HTTP traffic to an alternative logger, simply pass it to the customizer:

RestTemplate restTemplate = new RestTemplateBuilder()
	.customizers(new LoggingCustomizer(LogFactory.getLog("my.http.log")))
	.build();

Customising the log format

To use a different format when logging HTTP traffic, implement LogFormatter and pass it to the customizer:

RestTemplate restTemplate = new RestTemplateBuilder()
	.customizers(new LoggingCustomizer(LogFactory.getLog(LoggingCustomizer.class), new MyLogFormatter()))
	.build();

Consider subclassing DefaultLogFormatter and overriding formatBody if you only need to customise the logged HTTP body.

Acknowledgements

Thanks to @nwholloway and @hdpe for the original implementation.

License

CI

About

Spring RestTemplate customizer to log HTTP traffic.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages