-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
40 changed files
with
2,123 additions
and
1,057 deletions.
There are no files selected for viewing
69 changes: 0 additions & 69 deletions
69
...port-core/src/main/java/com/sap/hcp/cf/logging/common/converter/DefaultArgsConverter.java
This file was deleted.
Oops, something went wrong.
73 changes: 73 additions & 0 deletions
73
...e/src/main/java/com/sap/hcp/cf/logging/common/converter/DefaultCustomFieldsConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package com.sap.hcp.cf.logging.common.converter; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.slf4j.LoggerFactory; | ||
|
||
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.customfields.CustomField; | ||
|
||
public class DefaultCustomFieldsConverter { | ||
|
||
private String fieldName = null; | ||
private boolean embed = true; | ||
|
||
public void setFieldName(String fieldName) { | ||
if (fieldName != null) { | ||
this.fieldName = fieldName; | ||
embed = false; | ||
} | ||
} | ||
|
||
public void convert(StringBuilder appendTo, Map<String, String> mdcCustomFields, Object... arguments) { | ||
List<CustomField> customFields = getCustomFields(arguments); | ||
if (!customFields.isEmpty() || !mdcCustomFields.isEmpty()) { | ||
try { | ||
if (!embed) { | ||
appendTo.append(JSON.std.asString(fieldName)).append(":"); | ||
} | ||
/* | ||
* -- no matter whether we embed or not, it seems easier to | ||
* compose -- a JSON object from the key/value pairs. -- if we | ||
* embed that object, we simply chop off the outermost curly | ||
* braces. | ||
*/ | ||
ObjectComposer<JSONComposer<String>> oc = JSON.std.composeString().startObject(); | ||
for (CustomField cf : customFields) { | ||
oc.putObject(cf.getKey(), cf.getValue()); | ||
} | ||
for (Map.Entry<String, String> mdcField : mdcCustomFields.entrySet()) { | ||
oc.put(mdcField.getKey(), mdcField.getValue()); | ||
} | ||
String result = oc.end().finish().trim(); | ||
if (embed) { | ||
/* -- chop off curly braces -- */ | ||
appendTo.append(result.substring(1, result.length() - 1)); | ||
} else { | ||
appendTo.append(result); | ||
} | ||
} catch (Exception ex) { | ||
/* -- avoids substitute logger warnings on startup -- */ | ||
LoggerFactory.getLogger(DefaultCustomFieldsConverter.class).error("Conversion failed ", ex); | ||
} | ||
} | ||
} | ||
|
||
private List<CustomField> getCustomFields(Object[] arguments) { | ||
if (arguments == null || arguments.length == 0) { | ||
return Collections.emptyList(); | ||
} | ||
List<CustomField> customFields = new ArrayList<CustomField>(); | ||
for (Object argument : arguments) { | ||
if (argument instanceof CustomField) { | ||
customFields.add((CustomField) argument); | ||
} | ||
} | ||
return customFields; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 0 additions & 75 deletions
75
...ort-core/src/test/java/com/sap/hcp/cf/logging/common/converter/AbstractConverterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,107 +1,32 @@ | ||
package com.sap.hcp.cf.logging.common.converter; | ||
|
||
import java.io.IOException; | ||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Map.Entry; | ||
|
||
import org.slf4j.MDC; | ||
|
||
import com.fasterxml.jackson.jr.ob.JSON; | ||
import com.fasterxml.jackson.jr.ob.JSONObjectException; | ||
import com.sap.hcp.cf.logging.common.customfields.CustomField; | ||
|
||
public abstract class AbstractConverterTest { | ||
protected static final String PREFIX = "prefix"; | ||
protected static final String EMPTY = ""; | ||
protected static final String SOME_KEY = "some_key"; | ||
protected static final String SOME_VALUE = "some value"; | ||
protected static final String STRANGE_SEQ = "}{:\",\""; | ||
protected static final String SOME_OTHER_KEY = "some_other_key"; | ||
protected static final String SOME_OTHER_VALUE = "some other value"; | ||
protected static final String TEST_MSG_NO_ARGS = "This is a test "; | ||
protected static final Object[] NO_ARGS = new Object[0]; | ||
protected static final Object[] NON_CUSTOM_ARGS = new Object[] { new String("standard") }; | ||
|
||
protected String formatMsg(DefaultMessageConverter mc, String msg) { | ||
StringBuilder sb = new StringBuilder(); | ||
mc.convert(msg, sb); | ||
return sb.toString(); | ||
} | ||
|
||
protected String formatProps(DefaultPropertiesConverter pc) { | ||
StringBuilder sb = new StringBuilder(); | ||
pc.convert(MDC.getCopyOfContextMap(), sb); | ||
return sb.toString(); | ||
} | ||
|
||
protected String formatArgs(DefaultArgsConverter ac, Object[] args) { | ||
StringBuilder sb = new StringBuilder(); | ||
ac.convert(args, sb); | ||
return sb.toString(); | ||
} | ||
|
||
protected String formatStacktrace(DefaultStacktraceConverter dstc, Throwable t) { | ||
StringBuilder sb = new StringBuilder(); | ||
dstc.convert(t, sb); | ||
return sb.toString(); | ||
} | ||
|
||
protected Map<String, Object> makeMap(CustomField[] custFields) { | ||
Map<String, Object> map = new HashMap<String, Object>(); | ||
for (CustomField cf: custFields) { | ||
map.put(cf.getKey(), cf.getValue()); | ||
} | ||
return map; | ||
} | ||
|
||
protected Map<String, Object> makeMap(String[] keys) { | ||
Map<String, Object> map = new HashMap<String, Object>(); | ||
for (String key: keys) { | ||
map.put(key, MDC.get(key)); | ||
} | ||
return map; | ||
} | ||
|
||
protected Map<String, Object> mdcMap() { | ||
return mdcMap(null); | ||
} | ||
|
||
protected Map<String, Object> mdcMap(String[] exclusions) { | ||
Map<String, Object> result = new HashMap<String, Object>(); | ||
List<String> exclusionList; | ||
if (exclusions == null) { | ||
exclusionList = Arrays.asList(new String[0]); | ||
} else { | ||
exclusionList = Arrays.asList(exclusions); | ||
} | ||
for (Entry<String, String> t: MDC.getCopyOfContextMap().entrySet()) { | ||
if (!exclusionList.contains(t.getKey())) { | ||
result.put(t.getKey(), t.getValue()); | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
protected Object arrayElem(String serialized, int i) throws JSONObjectException, IOException { | ||
return arrayFrom(serialized)[i]; | ||
} | ||
|
||
protected Object[] arrayFrom(String serialized) throws JSONObjectException, IOException { | ||
return JSON.std.arrayFrom(serialized); | ||
} | ||
|
||
protected Map<String, Object> mapFrom(String serialized) throws JSONObjectException, IOException { | ||
return mapFrom(serialized, true); | ||
} | ||
|
||
protected Map<String, Object> mapFrom(String serialized, boolean wrap) throws JSONObjectException, IOException { | ||
if (wrap) { | ||
return JSON.std.mapFrom("{" + serialized + "}"); | ||
} else { | ||
return JSON.std.mapFrom(serialized); | ||
} | ||
} | ||
} |
Oops, something went wrong.