Logging HTTP requests in Spring Boot using Bobbins
Greetings, dear friends!
Today I want to demonstrate a terrific example of how Bobina can help in a very common situation - logging HTTP requests and responses in Spring Boot.
Even more! We will only log HTTP messages in separate files.
So let's go!
To activate HTTP logging in Spring Boot just add the line to application.properties
:
logging.level.org.springframework.web=TRACE
Such a configuration will still not allow the body of the HTTP request to be logged.
To activate the logging of the body of the HTTP request, 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 the dependency to build.gradle
:
compile "io.infinite:bobbin:2.0.4"
Or in pom.xml
:
org.codehaus.groovy groovy-all 2.5.6 pom io.infinite bobbin 2.0.4
Create a file Bobbin.json
in the directory with resources 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 "Bobbin" is that it easily splits logging output into separate files based on criteria such as:
- Message level (
debug
,warn
, etc.) - Stream name
- Class and package name
- etc.
So why should we bother looking for our HTTP logs in files containing other logs?
Let's direct the Spring Boot HTTP logs to separate files for our convenience!
Add new destination
to 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
the name of their package in a directory "SPRING_WEB"
!
Easy, right?
This is what the full configuration looks like 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, because file has long lines
Logging has never been so easy!
Thanks for attention!