Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suppress Default Values from MDC #120

Merged
merged 1 commit into from
Dec 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
import com.fasterxml.jackson.jr.ob.JSON;
import com.fasterxml.jackson.jr.ob.JSONComposer;
import com.fasterxml.jackson.jr.ob.comp.ObjectComposer;
import com.sap.hcp.cf.logging.common.Defaults;
import com.sap.hcp.cf.logging.common.LogContext;

public class DefaultPropertiesConverter {

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

public DefaultPropertiesConverter() {
}
Expand All @@ -31,6 +33,10 @@ public void setExclusions(List<String> exclusionList) {
}
}

public void setSendDefaultValues(boolean sendDefaultValues) {
this.sendDefaultValues = sendDefaultValues;
}

private static class LoggerHolder {
static final Logger LOG = LoggerFactory.getLogger(LoggerHolder.class.getEnclosingClass());
}
Expand All @@ -46,7 +52,14 @@ public void convert(StringBuilder appendTo, Map<String, String> eventProperties)
ObjectComposer<JSONComposer<String>> oc = JSON.std.composeString().startObject();
for (Entry<String, String> p: properties.entrySet()) {
if (!exclusions.contains(p.getKey())) {
oc.put(p.getKey(), p.getValue());
if (sendDefaultValues) {
oc.put(p.getKey(), p.getValue());
} else {
String defaultValue = getDefaultValue(p.getKey());
if (!defaultValue.equals(p.getValue())) {
KarstenSchnitter marked this conversation as resolved.
Show resolved Hide resolved
oc.put(p.getKey(), p.getValue());
}
}
}
}
String result = oc.end().finish().trim();
Expand All @@ -73,4 +86,9 @@ private Map<String, String> mergeContextMaps(Map<String, String> eventMap) {
}
return result;
}

private String getDefaultValue(String key) {
String defaultValue = LogContext.getDefault(key);
return defaultValue == null ? Defaults.UNKNOWN : defaultValue;
}
}
Loading