Skip to content

Commit f81216b

Browse files
Merge branch 'master' into master
2 parents 8a40ef3 + 1ecc179 commit f81216b

File tree

40 files changed

+2123
-1057
lines changed

40 files changed

+2123
-1057
lines changed

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

Lines changed: 0 additions & 69 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.sap.hcp.cf.logging.common.converter;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
import org.slf4j.LoggerFactory;
9+
10+
import com.fasterxml.jackson.jr.ob.JSON;
11+
import com.fasterxml.jackson.jr.ob.JSONComposer;
12+
import com.fasterxml.jackson.jr.ob.comp.ObjectComposer;
13+
import com.sap.hcp.cf.logging.common.customfields.CustomField;
14+
15+
public class DefaultCustomFieldsConverter {
16+
17+
private String fieldName = null;
18+
private boolean embed = true;
19+
20+
public void setFieldName(String fieldName) {
21+
if (fieldName != null) {
22+
this.fieldName = fieldName;
23+
embed = false;
24+
}
25+
}
26+
27+
public void convert(StringBuilder appendTo, Map<String, String> mdcCustomFields, Object... arguments) {
28+
List<CustomField> customFields = getCustomFields(arguments);
29+
if (!customFields.isEmpty() || !mdcCustomFields.isEmpty()) {
30+
try {
31+
if (!embed) {
32+
appendTo.append(JSON.std.asString(fieldName)).append(":");
33+
}
34+
/*
35+
* -- no matter whether we embed or not, it seems easier to
36+
* compose -- a JSON object from the key/value pairs. -- if we
37+
* embed that object, we simply chop off the outermost curly
38+
* braces.
39+
*/
40+
ObjectComposer<JSONComposer<String>> oc = JSON.std.composeString().startObject();
41+
for (CustomField cf : customFields) {
42+
oc.putObject(cf.getKey(), cf.getValue());
43+
}
44+
for (Map.Entry<String, String> mdcField : mdcCustomFields.entrySet()) {
45+
oc.put(mdcField.getKey(), mdcField.getValue());
46+
}
47+
String result = oc.end().finish().trim();
48+
if (embed) {
49+
/* -- chop off curly braces -- */
50+
appendTo.append(result.substring(1, result.length() - 1));
51+
} else {
52+
appendTo.append(result);
53+
}
54+
} catch (Exception ex) {
55+
/* -- avoids substitute logger warnings on startup -- */
56+
LoggerFactory.getLogger(DefaultCustomFieldsConverter.class).error("Conversion failed ", ex);
57+
}
58+
}
59+
}
60+
61+
private List<CustomField> getCustomFields(Object[] arguments) {
62+
if (arguments == null || arguments.length == 0) {
63+
return Collections.emptyList();
64+
}
65+
List<CustomField> customFields = new ArrayList<CustomField>();
66+
for (Object argument : arguments) {
67+
if (argument instanceof CustomField) {
68+
customFields.add((CustomField) argument);
69+
}
70+
}
71+
return customFields;
72+
}
73+
}

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 {
Original file line numberDiff line numberDiff line change
@@ -1,107 +1,32 @@
11
package com.sap.hcp.cf.logging.common.converter;
22

33
import java.io.IOException;
4-
import java.util.Arrays;
5-
import java.util.HashMap;
6-
import java.util.List;
7-
import java.util.Map;
8-
import java.util.Map.Entry;
9-
10-
import org.slf4j.MDC;
114

125
import com.fasterxml.jackson.jr.ob.JSON;
136
import com.fasterxml.jackson.jr.ob.JSONObjectException;
14-
import com.sap.hcp.cf.logging.common.customfields.CustomField;
157

168
public abstract class AbstractConverterTest {
17-
protected static final String PREFIX = "prefix";
189
protected static final String EMPTY = "";
19-
protected static final String SOME_KEY = "some_key";
20-
protected static final String SOME_VALUE = "some value";
2110
protected static final String STRANGE_SEQ = "}{:\",\"";
22-
protected static final String SOME_OTHER_KEY = "some_other_key";
23-
protected static final String SOME_OTHER_VALUE = "some other value";
2411
protected static final String TEST_MSG_NO_ARGS = "This is a test ";
25-
protected static final Object[] NO_ARGS = new Object[0];
26-
protected static final Object[] NON_CUSTOM_ARGS = new Object[] { new String("standard") };
2712

2813
protected String formatMsg(DefaultMessageConverter mc, String msg) {
2914
StringBuilder sb = new StringBuilder();
3015
mc.convert(msg, sb);
3116
return sb.toString();
3217
}
3318

34-
protected String formatProps(DefaultPropertiesConverter pc) {
35-
StringBuilder sb = new StringBuilder();
36-
pc.convert(MDC.getCopyOfContextMap(), sb);
37-
return sb.toString();
38-
}
39-
40-
protected String formatArgs(DefaultArgsConverter ac, Object[] args) {
41-
StringBuilder sb = new StringBuilder();
42-
ac.convert(args, sb);
43-
return sb.toString();
44-
}
45-
4619
protected String formatStacktrace(DefaultStacktraceConverter dstc, Throwable t) {
4720
StringBuilder sb = new StringBuilder();
4821
dstc.convert(t, sb);
4922
return sb.toString();
5023
}
5124

52-
protected Map<String, Object> makeMap(CustomField[] custFields) {
53-
Map<String, Object> map = new HashMap<String, Object>();
54-
for (CustomField cf: custFields) {
55-
map.put(cf.getKey(), cf.getValue());
56-
}
57-
return map;
58-
}
59-
60-
protected Map<String, Object> makeMap(String[] keys) {
61-
Map<String, Object> map = new HashMap<String, Object>();
62-
for (String key: keys) {
63-
map.put(key, MDC.get(key));
64-
}
65-
return map;
66-
}
67-
68-
protected Map<String, Object> mdcMap() {
69-
return mdcMap(null);
70-
}
71-
72-
protected Map<String, Object> mdcMap(String[] exclusions) {
73-
Map<String, Object> result = new HashMap<String, Object>();
74-
List<String> exclusionList;
75-
if (exclusions == null) {
76-
exclusionList = Arrays.asList(new String[0]);
77-
} else {
78-
exclusionList = Arrays.asList(exclusions);
79-
}
80-
for (Entry<String, String> t: MDC.getCopyOfContextMap().entrySet()) {
81-
if (!exclusionList.contains(t.getKey())) {
82-
result.put(t.getKey(), t.getValue());
83-
}
84-
}
85-
return result;
86-
}
87-
8825
protected Object arrayElem(String serialized, int i) throws JSONObjectException, IOException {
8926
return arrayFrom(serialized)[i];
9027
}
9128

9229
protected Object[] arrayFrom(String serialized) throws JSONObjectException, IOException {
9330
return JSON.std.arrayFrom(serialized);
9431
}
95-
96-
protected Map<String, Object> mapFrom(String serialized) throws JSONObjectException, IOException {
97-
return mapFrom(serialized, true);
98-
}
99-
100-
protected Map<String, Object> mapFrom(String serialized, boolean wrap) throws JSONObjectException, IOException {
101-
if (wrap) {
102-
return JSON.std.mapFrom("{" + serialized + "}");
103-
} else {
104-
return JSON.std.mapFrom(serialized);
105-
}
106-
}
10732
}

0 commit comments

Comments
 (0)