Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
KarstenSchnitter authored Jul 12, 2019
2 parents 8a40ef3 + 1ecc179 commit f81216b
Show file tree
Hide file tree
Showing 40 changed files with 2,123 additions and 1,057 deletions.

This file was deleted.

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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void setExclusions(List<String> exclusionList) {
}
}

public void convert(Map<String, String> eventProperties, StringBuilder appendTo) {
public void convert(StringBuilder appendTo, Map<String, String> eventProperties) {
Map<String, String> properties = mergeContextMaps(eventProperties);
if (properties != null && !properties.isEmpty()) {
try {
Expand Down
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);
}
}
}
Loading

0 comments on commit f81216b

Please sign in to comment.