📜 ⬆️ ⬇️

Logging HTTP requests in Spring Boot using Reels

Greetings, dear friends!


Today I want to show an amazing example of how Bobin can help in a very common situation - logging HTTP requests and responses in Spring Boot.


Even more! We will only log HTTP messages into separate files.


So let's go!


To activate HTTP logging in Spring Boot, simply add a line to application.properties :


 logging.level.org.springframework.web=TRACE 

This configuration still does not allow the HTTP request body to be logged.


To activate the HTTP request body logging, create a configuration class:


 import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.filter.CommonsRequestLoggingFilter; import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration public class WebMvcConfiguration implements WebMvcConfigurer { @Bean public CommonsRequestLoggingFilter logFilter() { CommonsRequestLoggingFilter filter = new CommonsRequestLoggingFilter(); filter.setIncludeQueryString(true); filter.setIncludePayload(true); filter.setMaxPayloadLength(100000); filter.setIncludeHeaders(false); filter.setAfterMessagePrefix("REQUEST DATA : "); return filter; } } 

Now, let's set up Bobbin .


Add a dependency to build.gradle :


 compile "io.infinite:bobbin:2.0.4" 

Or in pom.xml :


 <dependency> <groupId>org.codehaus.groovy</groupId> <artifactId>groovy-all</artifactId> <version>2.5.6</version> <type>pom</type> </dependency> <dependency> <groupId>io.infinite</groupId> <artifactId>bobbin</artifactId> <version>2.0.4</version> </dependency> 

Create a file Bobbin.json in the resource directory or in the working directory of the application:


 { "levels": "['debug', 'info', 'warn', 'error'].contains(level)", "destinations": [ { "name": "io.infinite.bobbin.destinations.FileDestination", "properties": { "fileName": "\"./LOGS/THREADS/${threadGroupName}/${threadName}/${level}/${threadName}_${level}_${date}.log\"" }, "classes": "className.contains('io.infinite.')" }, { "name": "io.infinite.bobbin.destinations.FileDestination", "properties": { "fileName": "\"./LOGS/ALL/WARNINGS_AND_ERRORS_${date}.log\"" }, "levels": "['warn', 'error'].contains(level)" }, { "name": "io.infinite.bobbin.destinations.ConsoleDestination", "levels": "['warn', 'error'].contains(level)" } ] } 

One of the main features of the Reel is that it easily shares the output of logging into separate files based on criteria such as:



So why should we bother looking for our HTTP logs in files containing other logs?


Let's send HTTP Spring Boot logs to separate files for our convenience!


Add a new destination in Bobbin.json :


  { "name": "io.infinite.bobbin.destinations.FileDestination", "properties": { "fileName": "\"./LOGS/SPRING_WEB/${threadGroupName}/${threadName}/${level}/${threadName}_${level}_${date}.log\"" }, "classes": "className.contains('org.springframework.web')" } 

As you can see, we write logs from classes containing org.springframework.web in the name of their package in the "SPRING_WEB" directory!


Easy, isn't it?


Here is the complete configuration of Bobbin.json :


 { "levels": "['debug', 'info', 'warn', 'error'].contains(level)", "destinations": [ { "name": "io.infinite.bobbin.destinations.FileDestination", "properties": { "fileName": "\"./LOGS/THREADS/${threadGroupName}/${threadName}/${level}/${threadName}_${level}_${date}.log\"" }, "classes": "className.contains('io.infinite.')" }, { "name": "io.infinite.bobbin.destinations.FileDestination", "properties": { "fileName": "\"./LOGS/ALL/WARNINGS_AND_ERRORS_${date}.log\"" }, "levels": "['warn', 'error'].contains(level)" }, { "name": "io.infinite.bobbin.destinations.FileDestination", "properties": { "fileName": "\"./LOGS/SPRING_WEB/${threadGroupName}/${threadName}/${level}/${threadName}_${level}_${date}.log\"" }, "classes": "className.contains('org.springframework.web')" }, { "name": "io.infinite.bobbin.destinations.ConsoleDestination", "levels": "['warn', 'error'].contains(level)" } ] } 

And here is the contents of the log file itself .\LOGS\SPRING_WEB\main\http-nio-8089-exec-1\debug\http-nio-8089-exec-1_debug_2019-03-22.log : Link to github.com, t .to. the file has long lines


Never before has logging setup been so easy!


Thanks for attention!


')

Source: https://habr.com/ru/post/449008/


All Articles