Skip to content

Commit 6022a27

Browse files
author
Brede Fritjof Klausen
authored
[FOR-4925] New annotation, only log response and not request (#9)
To cut down on number of logs, only the response should be logged and not the request. Normally for every request there will be two logs, one for request that will inform about the method, context-path and potential request-body, and one identical but with response, millis and entity. We only need the latter log if it's a GET request. # Task https://obos-bbl.atlassian.net/browse/FOR-4925 # Changes * Add new annotation `@LogOnlyResponse` * Default is set to false * Will only log the response and not the request when set to true ## Example To use it you have to set `LogOnlyResponse` to `true` like this `@LogOnlyResponse(true)` or like this `@LogOnlyResponse(value = true)` ### Without the annotation `2024-08-14 09:41:43,628 INFO [0fe1aef1-81b8-1337-hello-0021a8dff2fd] n.o.u.s.client.ClientLogFilter GET http://app.obos.no/bolig/v4/api/selskap/1445/organisasjonsnumre` `2024-08-14 09:41:43,646 INFO [0fe1aef1-81b8-1337-hello-0021a8dff2fd] n.o.u.s.client.ClientLogFilter GET http://app.obos.no/bolig/v4/api/selskap/1445/organisasjonsnumre response: 200, millis: 18` ### With the annotation `2024-08-14 09:41:43,646 INFO [0fe1aef1-81b8-1337-hello-0021a8dff2fd] n.o.u.s.client.ClientLogFilter GET http://app.obos.no/bolig/v4/api/selskap/1445/organisasjonsnumre response: 200, millis: 18`
1 parent 9249e70 commit 6022a27

File tree

4 files changed

+33
-6
lines changed

4 files changed

+33
-6
lines changed

servicebuilder-core-addons/src/main/java/no/obos/util/servicebuilder/log/ServerLogFilter.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public void filter(ContainerRequestContext request) throws IOException {
7979

8080
LogParams logParams = serverLogger.LogParamsForCall(handlingClass, handlingMethod);
8181

82-
if (! logParams.enableLogging) {
82+
if (! logParams.enableLogging || logParams.logOnlyResponse) {
8383
return;
8484
}
8585

@@ -113,8 +113,6 @@ public void filter(ContainerRequestContext request) throws IOException {
113113
logRequest.headers((ImmutableMap.copyOf(headers)));
114114
}
115115

116-
117-
118116
if (logParams.logRequestPayload) {
119117
logRequest.entity(extractRequestEntity(request));
120118
}

servicebuilder-core-addons/src/main/java/no/obos/util/servicebuilder/log/ServerLogger.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import lombok.AllArgsConstructor;
99
import lombok.extern.slf4j.Slf4j;
1010
import no.obos.util.servicebuilder.annotations.Log;
11+
import no.obos.util.servicebuilder.annotations.LogOnlyResponse;
1112
import no.obos.util.servicebuilder.annotations.LogRequestEntity;
1213
import no.obos.util.servicebuilder.annotations.LogResponseEntity;
1314
import no.obos.util.servicebuilder.log.model.LogParams;
@@ -44,6 +45,11 @@ public LogParams LogParamsForCall(Class<?> clazz, Method method) {
4445
ret = ret.enableLogging(enableLogging.value());
4546
}
4647

48+
LogOnlyResponse logOnlyResponse = AnnotationUtil.getAnnotation(LogOnlyResponse.class, method);
49+
if (logOnlyResponse != null) {
50+
ret = ret.logOnlyResponse(logOnlyResponse.value());
51+
}
52+
4753
LogRequestEntity logRequestEntity = AnnotationUtil.getAnnotation(LogRequestEntity.class, method);
4854
if (logRequestEntity != null) {
4955
ret = ret.logRequestPayload(logRequestEntity.value());
@@ -54,7 +60,6 @@ public LogParams LogParamsForCall(Class<?> clazz, Method method) {
5460
ret = ret.logResponseEntity(logResponseEntity.value());
5561
}
5662
return ret;
57-
5863
}
5964

6065
public void handleRequest(LogRequest logRequest, LogParams logParams) {

servicebuilder-core-addons/src/main/java/no/obos/util/servicebuilder/log/model/LogParams.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,14 @@ public class LogParams {
5050
@Wither(AccessLevel.PRIVATE)
5151
public final boolean logRequestPayload;
5252

53-
public final static LogParams defaults = new LogParams(true, LogLevel.INFO, false, ImmutableSet.of(), true, true);
53+
/**
54+
* default er false, og om det er false logges request og response.
55+
* Om det er true, logges kun response
56+
*/
57+
@Wither(AccessLevel.PRIVATE)
58+
public final boolean logOnlyResponse;
59+
60+
public static final LogParams defaults = new LogParams(true, LogLevel.INFO, false, ImmutableSet.of(), true, true, false);
5461

5562
public LogParams enableLogging(boolean enableLogging) {
5663
return withEnableLogging(enableLogging);
@@ -65,7 +72,7 @@ public LogParams logHeaders(boolean logHeaders) {
6572
}
6673

6774
public LogParams skipHeaders(ImmutableSet<String> skipHeaders) {
68-
return this.skipHeaders == skipHeaders ? this : new LogParams(this.enableLogging, this.logLevel, this.logHeaders, skipHeaders, this.logResponseEntity, this.logRequestPayload);
75+
return this.skipHeaders == skipHeaders ? this : new LogParams(this.enableLogging, this.logLevel, this.logHeaders, skipHeaders, this.logResponseEntity, this.logRequestPayload, this.logOnlyResponse);
6976
}
7077

7178
public LogParams clearSkipHeaders() {
@@ -83,4 +90,9 @@ public LogParams logResponseEntity(boolean logResponseEntity) {
8390
public LogParams logRequestPayload(boolean logRequestPayload) {
8491
return withLogRequestPayload(logRequestPayload);
8592
}
93+
94+
public LogParams logOnlyResponse(boolean logOnlyResponse) {
95+
return withLogOnlyResponse(logOnlyResponse);
96+
}
97+
8698
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package no.obos.util.servicebuilder.annotations;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
@Retention(RetentionPolicy.RUNTIME)
9+
@Target(ElementType.METHOD)
10+
public @interface LogOnlyResponse {
11+
boolean value() default false;
12+
}

0 commit comments

Comments
 (0)