Skip to content

Commit 1016e38

Browse files
committed
springboot2-request-logger - implemented spring filter
1 parent 2a0c51e commit 1016e38

19 files changed

Lines changed: 584 additions & 0 deletions

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.bvn13.example.recursion.print;
2+
3+
/**
4+
* @author bvn13
5+
* @since 21.09.2019
6+
*/
7+
public class RecursionPrint {
8+
9+
public static void main(String... args) {
10+
11+
long n = 20;
12+
13+
boolean result = printIt(1, n);
14+
15+
}
16+
17+
private static boolean printIt(long x, long n) {
18+
System.out.println(x);
19+
20+
return String.valueOf(x).equals(String.valueOf(n)) || printIt(x+1, n);
21+
}
22+
23+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
HELP.md
2+
.gradle
3+
build/
4+
!gradle/wrapper/gradle-wrapper.jar
5+
!**/src/main/**
6+
!**/src/test/**
7+
8+
### STS ###
9+
.apt_generated
10+
.classpath
11+
.factorypath
12+
.project
13+
.settings
14+
.springBeans
15+
.sts4-cache
16+
17+
### IntelliJ IDEA ###
18+
.idea
19+
*.iws
20+
*.iml
21+
*.ipr
22+
out/
23+
24+
### NetBeans ###
25+
/nbproject/private/
26+
/nbbuild/
27+
/dist/
28+
/nbdist/
29+
/.nb-gradle/
30+
31+
### VS Code ###
32+
.vscode/
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# How to log every Request in SpringBoot
2+
3+
## Prerequesites
4+
5+
Imagine we have an web SpringBoot application containing a [Controller](./src/main/java/com/bvn13/example/springboot/springrequestlogger/controllers/FirstController.java).
6+
It returns a template [index](/src/main/resources/templates/index.html) containing two links: on [css file](./src/main/resources/static/test.css) and on [js file](./src/main/resources/static/test.js).
7+
8+
So we want to log every request from client while opening `index.html`:
9+
1. `index.html`
10+
2. `test.css`
11+
3. `test.js`
12+
13+
## Enabling logging
14+
15+
We must set up the logging of our classes. Take a look at example of [logback-spring.xml](./src/main/resources/logback-spring.xml).
16+
17+
## Build a Filter
18+
19+
Every Spring Web application have a chain on different filters. Every request flows through this filter chain before getting a controller.
20+
21+
So we can implement a [logging filter](./src/main/java/com/bvn13/example/springboot/springrequestlogger/filters/RequestLoggingFilter.java) and put it into Spring Filter Chain to achieve our goal.
22+
23+
```java
24+
@Slf4j
25+
@Component
26+
public class RequestLoggingFilter extends OncePerRequestFilter {
27+
@Override
28+
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
29+
log.debug(
30+
String.format("FILTERED URL: %s", request.getRequestURI())
31+
);
32+
33+
//continue filtering
34+
filterChain.doFilter(request, response);
35+
}
36+
}
37+
```
38+
39+
Using `@Slf4j` annotation get us to decrease boilerplate code to have a logger in a class.
40+
41+
We must annotate our class with `@Component` annotation to make SpringBoot to instantiate this class as spring bean in Singleton scope.
42+
43+
Our class extends `OncePerRequestFilter` abstract class so every request goes through our filter only once.
44+
45+
And we are able to log anything regarding the request inside `doFilterInternal` method.
46+
47+
### Result
48+
49+
You may run [testControllerLoggingWithFilter](./src/test/java/com/bvn13/example/springboot/springrequestlogger/SpringrequestloggerApplicationTests.java) to see the result
50+
51+
```java
52+
@Test(expected = HttpClientErrorException.NotFound.class)
53+
public void testControllerLoggingWithFilter() {
54+
restTemplate.getForObject("http://localhost:"+port+"/", String.class);
55+
restTemplate.getForObject("http://localhost:"+port+"/test.js", String.class);
56+
restTemplate.getForObject("http://localhost:"+port+"/test.css", String.class);
57+
restTemplate.getForObject("http://localhost:"+port+"/does-not-exist.file", String.class);
58+
}
59+
```
60+
61+
![](./img/2019-09-29_21-09.png)
62+
63+
So our goal is achieved. We can see all files we request and not existing file too.
64+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
plugins {
2+
id 'org.springframework.boot' version '2.1.8.RELEASE'
3+
id 'io.spring.dependency-management' version '1.0.8.RELEASE'
4+
id 'java'
5+
}
6+
7+
group = 'com.bvn13.example.springboot'
8+
version = '0.0.1-SNAPSHOT'
9+
sourceCompatibility = '1.8'
10+
11+
12+
configurations {
13+
compileOnly {
14+
extendsFrom annotationProcessor
15+
}
16+
}
17+
18+
repositories {
19+
mavenCentral()
20+
}
21+
22+
dependencies {
23+
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
24+
implementation 'org.springframework.boot:spring-boot-starter-web'
25+
compileOnly 'org.projectlombok:lombok'
26+
annotationProcessor 'org.projectlombok:lombok'
27+
testImplementation 'org.springframework.boot:spring-boot-starter-test'
28+
}
54.3 KB
Binary file not shown.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
distributionBase=GRADLE_USER_HOME
2+
distributionPath=wrapper/dists
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.2-bin.zip
4+
zipStoreBase=GRADLE_USER_HOME
5+
zipStorePath=wrapper/dists

springboot2-request-logger/gradlew

Lines changed: 188 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)