Skip to content

Commit 65f4501

Browse files
Make DynamicLogLevelProcessor pluggable
Users can provide own subclasses of RequestLoggingFilter, which can bring own implementations of DynLogEnvironment and DynLogLevelProcessor. There are several constructors, that can be used to customize the filter.
1 parent 235f13c commit 65f4501

File tree

5 files changed

+253
-148
lines changed

5 files changed

+253
-148
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.sap.hcp.cf.logging.servlet.dynlog;
2+
3+
import java.security.interfaces.RSAPublicKey;
4+
5+
import javax.servlet.http.HttpServletRequest;
6+
7+
public interface DynLogConfiguration {
8+
9+
String getDynLogHeaderKey();
10+
11+
RSAPublicKey getRsaPublicKey();
12+
13+
default String getDynLogHeaderValue(HttpServletRequest httpRequest) {
14+
return httpRequest.getHeader(getDynLogHeaderKey());
15+
}
16+
17+
}

cf-java-logging-support-servlet/src/main/java/com/sap/hcp/cf/logging/servlet/dynlog/DynLogEnvironment.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
import com.sap.hcp.cf.logging.common.helper.Environment;
1212

13-
public class DynLogEnvironment {
13+
public class DynLogEnvironment implements DynLogConfiguration {
1414

1515
private static final Logger LOGGER = LoggerFactory.getLogger(DynLogEnvironment.class);
1616
private final RSAPublicKey rsaPublicKey;
@@ -46,10 +46,12 @@ public DynLogEnvironment() {
4646
}
4747
}
4848

49+
@Override
4950
public RSAPublicKey getRsaPublicKey() {
5051
return rsaPublicKey;
5152
}
5253

54+
@Override
5355
public String getDynLogHeaderKey() {
5456
return dynLogHeaderKey;
5557
}

cf-java-logging-support-servlet/src/main/java/com/sap/hcp/cf/logging/servlet/dynlog/DynamicLogLevelProcessor.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
import java.util.Arrays;
44
import java.util.List;
55

6-
import javax.servlet.http.HttpServletRequest;
7-
86
import org.slf4j.Logger;
97
import org.slf4j.LoggerFactory;
108
import org.slf4j.MDC;
@@ -24,25 +22,22 @@ public class DynamicLogLevelProcessor {
2422
private static final List<String> ALLOWED_DYNAMIC_LOGLEVELS = Arrays.asList("TRACE", "DEBUG", "INFO", "WARN",
2523
"ERROR");
2624
private final TokenDecoder tokenDecoder;
27-
private final DynLogEnvironment dynLogEnvironment;
2825

29-
public DynamicLogLevelProcessor(DynLogEnvironment dynLogEnvironment) {
30-
this.dynLogEnvironment = dynLogEnvironment;
31-
this.tokenDecoder = new TokenDecoder(dynLogEnvironment.getRsaPublicKey());
26+
public DynamicLogLevelProcessor(DynLogConfiguration dynLogConfig) {
27+
this.tokenDecoder = new TokenDecoder(dynLogConfig.getRsaPublicKey());
3228
}
3329

34-
public void copyDynamicLogLevelToMDC(HttpServletRequest httpRequest) {
35-
String logLevelToken = httpRequest.getHeader(dynLogEnvironment.getDynLogHeaderKey());
30+
public void copyDynamicLogLevelToMDC(String logLevelToken) {
3631
if (logLevelToken == null) {
3732
return;
3833
} else {
3934
try {
4035
DecodedJWT jwt = tokenDecoder.validateAndDecodeToken(logLevelToken);
4136
String dynamicLogLevel = jwt.getClaim("level").asString();
42-
String packages = jwt.getClaim("packages").asString();
37+
String packages = jwt.getClaim("packages").asString();
4338
if (ALLOWED_DYNAMIC_LOGLEVELS.contains(dynamicLogLevel)) {
4439
MDC.put(DynamicLogLevelHelper.MDC_DYNAMIC_LOG_LEVEL_KEY, dynamicLogLevel);
45-
MDC.put(DynamicLogLevelHelper.MDC_DYNAMIC_LOG_LEVEL_PREFIXES, packages);
40+
MDC.put(DynamicLogLevelHelper.MDC_DYNAMIC_LOG_LEVEL_PREFIXES, packages);
4641
} else {
4742
throw new DynamicLogLevelException("Dynamic Log-Level [" + dynamicLogLevel +
4843
"] provided in header is not valid. Allowed Values are " +

0 commit comments

Comments
 (0)