Skip to content

Commit ac8e76d

Browse files
Suppress Default Values from MDC (#120)
The library can add fields from the MDC to each generated log messages. Certain fields are always created, even if the value is unknown. This changes avoids those fields in the output. This reduces message size and avoids unnecessary fields in the target service. The old behaviour is still available by an extra configuration in the logback.xml or log4j2.xml. Signed-off-by: Karsten Schnitter <[email protected]>
1 parent fa5b88d commit ac8e76d

File tree

16 files changed

+470
-324
lines changed

16 files changed

+470
-324
lines changed

cf-java-logging-support-core/src/main/java/com/sap/hcp/cf/logging/common/converter/DefaultPropertiesConverter.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@
1414
import com.fasterxml.jackson.jr.ob.JSON;
1515
import com.fasterxml.jackson.jr.ob.JSONComposer;
1616
import com.fasterxml.jackson.jr.ob.comp.ObjectComposer;
17+
import com.sap.hcp.cf.logging.common.Defaults;
1718
import com.sap.hcp.cf.logging.common.LogContext;
1819

1920
public class DefaultPropertiesConverter {
2021

2122
private final Set<String> exclusions = new HashSet<String>();
23+
private boolean sendDefaultValues = false;
2224

2325
public DefaultPropertiesConverter() {
2426
}
@@ -31,6 +33,10 @@ public void setExclusions(List<String> exclusionList) {
3133
}
3234
}
3335

36+
public void setSendDefaultValues(boolean sendDefaultValues) {
37+
this.sendDefaultValues = sendDefaultValues;
38+
}
39+
3440
private static class LoggerHolder {
3541
static final Logger LOG = LoggerFactory.getLogger(LoggerHolder.class.getEnclosingClass());
3642
}
@@ -46,7 +52,14 @@ public void convert(StringBuilder appendTo, Map<String, String> eventProperties)
4652
ObjectComposer<JSONComposer<String>> oc = JSON.std.composeString().startObject();
4753
for (Entry<String, String> p: properties.entrySet()) {
4854
if (!exclusions.contains(p.getKey())) {
49-
oc.put(p.getKey(), p.getValue());
55+
if (sendDefaultValues) {
56+
oc.put(p.getKey(), p.getValue());
57+
} else {
58+
String defaultValue = getDefaultValue(p.getKey());
59+
if (!defaultValue.equals(p.getValue())) {
60+
oc.put(p.getKey(), p.getValue());
61+
}
62+
}
5063
}
5164
}
5265
String result = oc.end().finish().trim();
@@ -73,4 +86,9 @@ private Map<String, String> mergeContextMaps(Map<String, String> eventMap) {
7386
}
7487
return result;
7588
}
89+
90+
private String getDefaultValue(String key) {
91+
String defaultValue = LogContext.getDefault(key);
92+
return defaultValue == null ? Defaults.UNKNOWN : defaultValue;
93+
}
7694
}

0 commit comments

Comments
 (0)