Skip to content

Commit 24a7195

Browse files
Remove Custom Fields from TopLevel Except when Excluded
1 parent b508db5 commit 24a7195

File tree

13 files changed

+318
-173
lines changed

13 files changed

+318
-173
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public void setExclusions(List<String> exclusionList) {
3030
}
3131
}
3232

33-
public void convert(Map<String, String> eventProperties, StringBuilder appendTo) {
33+
public void convert(StringBuilder appendTo, Map<String, String> eventProperties) {
3434
Map<String, String> properties = mergeContextMaps(eventProperties);
3535
if (properties != null && !properties.isEmpty()) {
3636
try {

cf-java-logging-support-core/src/test/java/com/sap/hcp/cf/logging/common/converter/DefaultPropertiesConverterTest.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public void cleadMdc() {
4040
public void emptyProperties() throws Exception {
4141
StringBuilder sb = new StringBuilder();
4242

43-
converter.convert(Collections.emptyMap(), sb);
43+
converter.convert(sb, Collections.emptyMap());
4444

4545
assertThat(unmarshal(sb), hasDefaultProperties());
4646
}
@@ -50,7 +50,7 @@ public void singleMdcEntry() throws Exception {
5050
StringBuilder sb = new StringBuilder();
5151
MDC.put("some key", "some value");
5252

53-
converter.convert(Collections.emptyMap(), sb);
53+
converter.convert(sb, Collections.emptyMap());
5454

5555
assertThat(unmarshal(sb), hasEntry("some key", "some value"));
5656
}
@@ -61,7 +61,7 @@ public void twoMdcEntries() throws Exception {
6161
MDC.put("some key", "some value");
6262
MDC.put("other key", "other value");
6363

64-
converter.convert(Collections.emptyMap(), sb);
64+
converter.convert(sb, Collections.emptyMap());
6565

6666
assertThat(unmarshal(sb),
6767
allOf(hasEntry("some key", "some value"), hasEntry("other key", "other value")));
@@ -77,7 +77,7 @@ public void singleExplicitEntry() throws Exception {
7777
}
7878
};
7979

80-
converter.convert(explicitFields, sb);
80+
converter.convert(sb, explicitFields);
8181

8282
assertThat(unmarshal(sb), hasEntry("explicit key", "explicit value"));
8383
}
@@ -93,7 +93,7 @@ public void mergesDifferentMdcAndExplicitEntries() throws Exception {
9393
};
9494
MDC.put("some key", "some value");
9595

96-
converter.convert(explicitFields, sb);
96+
converter.convert(sb, explicitFields);
9797

9898
assertThat(unmarshal(sb),
9999
allOf(hasEntry("some key", "some value"), hasEntry("explicit key", "explicit value")));
@@ -110,7 +110,7 @@ public void explicitValuesOverwritesMdc() throws Exception {
110110
};
111111
MDC.put("some key", "some value");
112112

113-
converter.convert(explicitFields, sb);
113+
converter.convert(sb, explicitFields);
114114

115115
assertThat(unmarshal(sb), hasEntry("some key", "explicit value"));
116116
}
@@ -129,7 +129,7 @@ public void dropsExclusions() throws Exception {
129129
MDC.put("excluded mdc key", "excluded mdc value");
130130

131131
converter.setExclusions(Arrays.asList("excluded explicit key", "excluded mdc key"));
132-
converter.convert(explicitFields, sb);
132+
converter.convert(sb, explicitFields);
133133

134134
assertThat(unmarshal(sb),
135135
allOf(hasEntry("retained mdc key", "retained mdc value"),
@@ -150,7 +150,7 @@ public void properlyEscapesKeys() throws Exception {
150150
};
151151
MDC.put("mdc" + HACK_ATTEMPT, "mdc value");
152152

153-
converter.convert(explicitFields, sb);
153+
converter.convert(sb, explicitFields);
154154

155155
assertThat(unmarshal(sb),
156156
allOf(hasEntry("mdc" + HACK_ATTEMPT, "mdc value"),
@@ -168,7 +168,7 @@ public void properlyEscapesValues() throws Exception {
168168
};
169169
MDC.put("mdc key", "mdc" + HACK_ATTEMPT);
170170

171-
converter.convert(explicitFields, sb);
171+
converter.convert(sb, explicitFields);
172172

173173
assertThat(unmarshal(sb),
174174
allOf(hasEntry("mdc key", "mdc" + HACK_ATTEMPT), hasEntry("explicit key", "explicit" + HACK_ATTEMPT)));
@@ -186,7 +186,7 @@ public void properlyEscapesExclusions() throws Exception {
186186
MDC.put("mdc" + HACK_ATTEMPT, "mdc value");
187187

188188
converter.setExclusions(Arrays.asList("explicit" + HACK_ATTEMPT, "mdc" + HACK_ATTEMPT));
189-
converter.convert(explicitFields, sb);
189+
converter.convert(sb, explicitFields);
190190

191191
assertThat(unmarshal(sb), allOf(not(hasEntry("mdc" + HACK_ATTEMPT, "mdc value")),
192192
not(hasEntry("explicit" + HACK_ATTEMPT, "explicit value"))));

cf-java-logging-support-log4j2/src/main/java/com/sap/hcp/cf/log4j2/converter/ContextPropsConverter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public static ContextPropsConverter newInstance(final String[] options) {
4343

4444
@Override
4545
public void format(LogEvent event, StringBuilder toAppendTo) {
46-
converter.convert(event.getContextMap(), toAppendTo);
46+
converter.convert(toAppendTo, event.getContextMap());
4747
}
4848

4949
}
Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.sap.hcp.cf.logback.converter;
22

3+
import java.util.ArrayList;
34
import java.util.List;
45

56
import com.sap.hcp.cf.logging.common.LogContext;
@@ -23,23 +24,38 @@
2324
*/
2425
public class ContextPropsConverter extends ClassicConverter {
2526

26-
public static final String WORD = "ctxp";
27-
private final DefaultPropertiesConverter converter = new DefaultPropertiesConverter();
28-
29-
@Override
30-
public String convert(ILoggingEvent event) {
31-
StringBuilder appendTo = new StringBuilder();
32-
LogContext.loadContextFields();
33-
converter.convert(event.getMDCPropertyMap(), appendTo);
34-
return appendTo.toString();
35-
}
36-
37-
@Override
38-
public void start() {
39-
List<String> exclusionList = getOptionList();
40-
if (exclusionList != null) {
41-
converter.setExclusions(exclusionList);
42-
}
43-
super.start();
44-
}
27+
public static final String WORD = "ctxp";
28+
private DefaultPropertiesConverter converter = new DefaultPropertiesConverter();
29+
private CustomFieldsAdapter customFieldsAdapter = new CustomFieldsAdapter();
30+
31+
void setConverter(DefaultPropertiesConverter converter) {
32+
this.converter = converter;
33+
}
34+
35+
void setCustomFieldsAdapter(CustomFieldsAdapter customFieldsAdapter) {
36+
this.customFieldsAdapter = customFieldsAdapter;
37+
}
38+
39+
@Override
40+
public String convert(ILoggingEvent event) {
41+
StringBuilder appendTo = new StringBuilder();
42+
LogContext.loadContextFields();
43+
converter.convert(appendTo, event.getMDCPropertyMap());
44+
return appendTo.toString();
45+
}
46+
47+
@Override
48+
public void start() {
49+
customFieldsAdapter.initialize(getContext());
50+
converter.setExclusions(calculateExclusions());
51+
super.start();
52+
}
53+
54+
private List<String> calculateExclusions() {
55+
List<String> exclusions = new ArrayList<>(customFieldsAdapter.getCustomFieldExclusions());
56+
if (getOptionList() != null) {
57+
exclusions.addAll(getOptionList());
58+
}
59+
return exclusions;
60+
}
4561
}
Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,59 @@
11
package com.sap.hcp.cf.logback.converter;
22

3+
import static java.util.Collections.emptyList;
4+
import static java.util.Collections.unmodifiableList;
5+
36
import java.util.ArrayList;
7+
import java.util.Collection;
48
import java.util.Collections;
59
import java.util.HashMap;
610
import java.util.List;
711
import java.util.Map;
812

13+
import com.sap.hcp.cf.logging.common.LogContext;
14+
915
import ch.qos.logback.core.Context;
1016

1117
public class CustomFieldsAdapter {
1218

1319
public static final String OPTION_MDC_CUSTOM_FIELDS = "customFieldMdcKeyNames";
20+
public static final String OPTION_MDC_RETAINED_FIELDS = "retainFieldMdcKeyNames";
1421

15-
private List<String> customFieldMdcKeyNames = Collections.emptyList();
22+
private List<String> customFieldMdcKeyNames = emptyList();
23+
private List<String> customFieldExclusions = emptyList();
1624

1725
public void initialize(Context context) {
1826
if (context == null) {
1927
return;
2028
}
21-
Object object = context.getObject(OPTION_MDC_CUSTOM_FIELDS);
22-
if (object instanceof List) {
23-
List<?> list = (List<?>) object;
24-
customFieldMdcKeyNames = new ArrayList<>(list.size());
29+
customFieldExclusions = calculateExclusions(context);
30+
customFieldMdcKeyNames = getListItemsAsStrings(context, OPTION_MDC_CUSTOM_FIELDS);
31+
}
32+
33+
private List<String> calculateExclusions(Context context) {
34+
List<String> candidates = getListItemsAsStrings(context, OPTION_MDC_CUSTOM_FIELDS);
35+
candidates.removeAll(getListItemsAsStrings(context, OPTION_MDC_RETAINED_FIELDS));
36+
candidates.removeAll(LogContext.getContextFieldsKeys());
37+
return unmodifiableList(candidates);
38+
}
39+
40+
private List<String> getListItemsAsStrings(Context context, String key) {
41+
Object object = context.getObject(key);
42+
if (object instanceof Collection) {
43+
Collection<?> list = (Collection<?>) object;
44+
ArrayList<String> listItems = new ArrayList<>(list.size());
2545
for (Object current : list) {
26-
customFieldMdcKeyNames.add(current.toString());
46+
listItems.add(current.toString());
2747
}
48+
return listItems;
2849
}
50+
return emptyList();
2951
}
3052

53+
public List<String> getCustomFieldExclusions() {
54+
return customFieldExclusions;
55+
}
56+
3157
public Map<String, String> selectCustomFields(Map<String, String> in) {
3258
if (in == null) {
3359
return Collections.emptyMap();
@@ -40,5 +66,4 @@ public Map<String, String> selectCustomFields(Map<String, String> in) {
4066
}
4167
return result;
4268
}
43-
4469
}

cf-java-logging-support-logback/src/main/java/com/sap/hcp/cf/logback/encoder/JsonEncoder.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,15 +119,21 @@ private void initPatterns() {
119119

120120
}
121121

122-
private List<String> customFieldMdcKeyNames = new ArrayList<String>();
122+
private List<String> customFieldMdcKeyNames = new ArrayList<>();
123+
private List<String> retainFieldMdcKeyNames = new ArrayList<>();
123124

124125
public void addCustomFieldMdcKeyName(String name) {
125126
customFieldMdcKeyNames.add(name);
126127
}
127128

129+
public void addRetainFieldMdcKeyName(String name) {
130+
retainFieldMdcKeyNames.add(name);
131+
}
132+
128133
@Override
129134
public void start() {
130135
context.putObject(CustomFieldsAdapter.OPTION_MDC_CUSTOM_FIELDS, customFieldMdcKeyNames);
136+
context.putObject(CustomFieldsAdapter.OPTION_MDC_RETAINED_FIELDS, retainFieldMdcKeyNames);
131137

132138
JsonLayout jsonLayout = new JsonLayout();
133139
jsonLayout.setContext(context);
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
package com.sap.hcp.cf.logback.converter;
22

33
import java.io.IOException;
4-
import java.util.ArrayList;
5-
import java.util.HashMap;
6-
import java.util.List;
7-
import java.util.Map;
8-
import java.util.Map.Entry;
94

105
import org.slf4j.Logger;
116
import org.slf4j.LoggerFactory;
12-
import org.slf4j.MDC;
137

148
import com.fasterxml.jackson.jr.ob.JSON;
159
import com.fasterxml.jackson.jr.ob.JSONObjectException;
@@ -18,13 +12,7 @@
1812
import ch.qos.logback.classic.spi.LoggingEvent;
1913

2014
public abstract class AbstractConverterTest {
21-
protected static final String PREFIX = "prefix";
22-
protected static final String EMPTY = "";
23-
protected static final String SOME_KEY = "some_key";
24-
protected static final String SOME_VALUE = "some value";
2515
protected static final String STRANGE_SEQ = "}{:\",\"";
26-
protected static final String SOME_OTHER_KEY = "some_other_key";
27-
protected static final String SOME_OTHER_VALUE = "some other value";
2816
protected static final String TEST_MSG_NO_ARGS = "This is a test ";
2917
protected static final Object[] NO_ARGS = new Object[0];
3018

@@ -45,33 +33,4 @@ protected Object arrayElem(String serialized, int i) throws JSONObjectException,
4533
protected Object[] arrayFrom(String serialized) throws JSONObjectException, IOException {
4634
return JSON.std.arrayFrom(serialized);
4735
}
48-
49-
50-
protected Map<String, Object> mapFrom(String serialized) throws JSONObjectException, IOException {
51-
return mapFrom(serialized, true);
52-
}
53-
54-
protected Map<String, Object> mapFrom(String serialized, boolean wrap) throws JSONObjectException, IOException {
55-
if (wrap) {
56-
return JSON.std.mapFrom("{" + serialized + "}");
57-
}
58-
return JSON.std.mapFrom(serialized);
59-
}
60-
61-
protected Map<String, Object> mdcMap() {
62-
return mdcMap(null);
63-
}
64-
65-
protected Map<String, Object> mdcMap(List<String> exclusions) {
66-
Map<String, Object> result = new HashMap<String, Object>();
67-
if (exclusions == null) {
68-
exclusions = new ArrayList<String>();
69-
}
70-
for (Entry<String, String> t: MDC.getCopyOfContextMap().entrySet()) {
71-
if (!exclusions.contains(t.getKey())) {
72-
result.put(t.getKey(), t.getValue());
73-
}
74-
}
75-
return result;
76-
}
7736
}

0 commit comments

Comments
 (0)