DefaultPropertiesConverter { public void convert(StringBuilder appendTo, Map<String, String> eventProperties) { Map<String, String> properties = mergeContextMaps(eventProperties); if (properties != null && !properties.isEmpty()) { try { 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()); } } String result = oc.end().finish().trim(); appendTo.append(result.substring(1, result.length() - 1)); } catch (Exception ex) { LoggerFactory.getLogger(DefaultPropertiesConverter.class).error("Conversion failed ", ex); } } } DefaultPropertiesConverter(); void setExclusions(List<String> exclusionList); void convert(StringBuilder appendTo, Map<String, String> eventProperties);  }
@Test public void explicitValuesOverwritesMdc() throws Exception { StringBuilder sb = new StringBuilder(); @SuppressWarnings("serial") Map<String, String> explicitFields = new HashMap<String, String>() { { put("some key", "explicit value"); } }; MDC.put("some key", "some value"); converter.convert(sb, explicitFields); assertThat(unmarshal(sb), hasEntry("some key", "explicit value")); }
@Test public void properlyEscapesKeys() throws Exception { StringBuilder sb = new StringBuilder(); @SuppressWarnings("serial") Map<String, String> explicitFields = new HashMap<String, String>() { { put("explicit" + HACK_ATTEMPT, "explicit value"); } }; MDC.put("mdc" + HACK_ATTEMPT, "mdc value"); converter.convert(sb, explicitFields); assertThat(unmarshal(sb), allOf(hasEntry("mdc" + HACK_ATTEMPT, "mdc value"), hasEntry("explicit" + HACK_ATTEMPT, "explicit value"))); }
@Test public void properlyEscapesValues() throws Exception { StringBuilder sb = new StringBuilder(); @SuppressWarnings("serial") Map<String, String> explicitFields = new HashMap<String, String>() { { put("explicit key", "explicit" + HACK_ATTEMPT); } }; MDC.put("mdc key", "mdc" + HACK_ATTEMPT); converter.convert(sb, explicitFields); assertThat(unmarshal(sb), allOf(hasEntry("mdc key", "mdc" + HACK_ATTEMPT), hasEntry("explicit key", "explicit" + HACK_ATTEMPT))); }
@Test public void emptyProperties() throws Exception { StringBuilder sb = new StringBuilder(); converter.convert(sb, Collections.emptyMap()); assertThat(unmarshal(sb), hasDefaultProperties()); }
@Test public void singleMdcEntry() throws Exception { StringBuilder sb = new StringBuilder(); MDC.put("some key", "some value"); converter.convert(sb, Collections.emptyMap()); assertThat(unmarshal(sb), hasEntry("some key", "some value")); }
@Test public void twoMdcEntries() throws Exception { StringBuilder sb = new StringBuilder(); MDC.put("some key", "some value"); MDC.put("other key", "other value"); converter.convert(sb, Collections.emptyMap()); assertThat(unmarshal(sb), allOf(hasEntry("some key", "some value"), hasEntry("other key", "other value"))); }
@Test public void singleExplicitEntry() throws Exception { StringBuilder sb = new StringBuilder(); @SuppressWarnings("serial") Map<String, String> explicitFields = new HashMap<String, String>() { { put("explicit key", "explicit value"); } }; converter.convert(sb, explicitFields); assertThat(unmarshal(sb), hasEntry("explicit key", "explicit value")); }
@Test public void mergesDifferentMdcAndExplicitEntries() throws Exception { StringBuilder sb = new StringBuilder(); @SuppressWarnings("serial") Map<String, String> explicitFields = new HashMap<String, String>() { { put("explicit key", "explicit value"); } }; MDC.put("some key", "some value"); converter.convert(sb, explicitFields); assertThat(unmarshal(sb), allOf(hasEntry("some key", "some value"), hasEntry("explicit key", "explicit value"))); }