springboot2-request-logger - logging using CommonsRequestLoggingFilter

master
bvn13 2019-09-30 23:41:37 +03:00
parent 44ea2370a2
commit 6952be2572
4 changed files with 85 additions and 23 deletions

View File

@ -69,7 +69,7 @@ Handler interceptors have more power to process your requests since they handle
You can check out Spring class `org.springframework.web.servlet.handler.HandlerInterceptorAdapter` to see its methods we are able to implement in our handler.
Lets [our handler](/src/main/java/com/bvn13/example/springboot/springrequestlogger/handlers/RequestLoggingHandler.java) implement `preHandle` and `postHandle` events:
Lets [our handler](./src/main/java/com/bvn13/example/springboot/springrequestlogger/handlers/RequestLoggingHandler.java) implement `preHandle` and `postHandle` events:
```java
@Override
@ -91,7 +91,7 @@ Lets [our handler](/src/main/java/com/bvn13/example/springboot/springrequestlogg
}
```
But the handler will not work until we actually tell Spring to use it. We must build a [configuration](/src/main/java/com/bvn13/example/springboot/springrequestlogger/handlers/WebApplicationConfiguration.java) to enable our handler.
But the handler will not work until we actually tell Spring to use it. We must build a [configuration](./src/main/java/com/bvn13/example/springboot/springrequestlogger/handlers/WebApplicationConfiguration.java) to enable our handler.
```java
@Configuration
@ -113,3 +113,38 @@ Lets start our test to check out the result!
![](./img/2019-09-30_23-25.png)
## Spring way to log requests
Spring provides a mechanism to log every request out of the box - `CommonsRequestLoggingFilter` bean.
You can implement an [instance of this class](./src/main/java/com/bvn13/example/springboot/springrequestlogger/config/RequestLoggingFilterConfig.java) ...
```java
@Bean
public CommonsRequestLoggingFilter logFilter() {
CommonsRequestLoggingFilter filter = new CommonsRequestLoggingFilter();
filter.setIncludeQueryString(true);
filter.setIncludePayload(true);
filter.setMaxPayloadLength(10000);
filter.setIncludeHeaders(false);
filter.setAfterMessagePrefix("REQUEST DATA : ");
return filter;
}
```
... and then set uo logging level into [`logback-spring.xml`](./src/main/resources/logback-spring.xml) located into `resources` or providing it externally ...
```xml
<logger name="org.springframework.web.filter.CommonsRequestLoggingFilter">
<level value="DEBUG" />
</logger>
```
... to enable requests logging.
### Result
Starting our test we have the result:
![](./img/2019-09-30_23-40.png)

Binary file not shown.

After

Width:  |  Height:  |  Size: 243 KiB

View File

@ -0,0 +1,23 @@
package com.bvn13.example.springboot.springrequestlogger.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.CommonsRequestLoggingFilter;
/**
* @author bvn13
* @since 30.09.2019
*/
@Configuration
public class RequestLoggingFilterConfig {
@Bean
public CommonsRequestLoggingFilter logFilter() {
CommonsRequestLoggingFilter filter = new CommonsRequestLoggingFilter();
filter.setIncludeQueryString(true);
filter.setIncludePayload(true);
filter.setMaxPayloadLength(10000);
filter.setIncludeHeaders(false);
filter.setAfterMessagePrefix("REQUEST DATA : ");
return filter;
}
}

View File

@ -20,4 +20,8 @@
<appender-ref ref="Console" />
</logger>
<logger name="org.springframework.web.filter.CommonsRequestLoggingFilter">
<level value="DEBUG" />
</logger>
</configuration>