@Override public void format(LogEvent event, StringBuilder toAppendTo) { converter.convert(event.getContextMap(), toAppendTo); }
public ContextPropsConverter(String[] options) { super(WORD, WORD); if (options != null) { converter.setExclusions(Arrays.asList(options)); } }
public void convert(Map<String, String> eventProperties, StringBuilder appendTo) { Map<String, String> properties = mergeContextMaps(eventProperties); if (properties != null && !properties.isEmpty()) { try { /* * -- let's compose an JSON object, then chop off the outermost * curly braces */ 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) { /* -- avoids substitute logger warnings on startup -- */ LoggerFactory.getLogger(DefaultPropertiesConverter.class).error("Conversion failed ", ex); } } }
@Test public void testExclusionStrangeSeq() throws JSONObjectException, IOException { DefaultPropertiesConverter cpc = new DefaultPropertiesConverter(); String[] exclusions = new String[] { STRANGE_SEQ }; cpc.setExclusions(Arrays.asList(exclusions)); MDC.clear(); MDC.put(SOME_KEY, SOME_VALUE); MDC.put(STRANGE_SEQ, STRANGE_SEQ); assertThat(mapFrom(formatProps(cpc)), is(mdcMap(exclusions))); } }
@Test public void testStrangeArgs() throws JSONObjectException, IOException { DefaultPropertiesConverter cpc = new DefaultPropertiesConverter(); MDC.clear(); MDC.put(SOME_KEY, SOME_VALUE); MDC.put(STRANGE_SEQ, STRANGE_SEQ); assertThat(mapFrom(formatProps(cpc)), is(mdcMap())); }
@Test public void testExclusion() throws JSONObjectException, IOException { DefaultPropertiesConverter cpc = new DefaultPropertiesConverter(); String[] exclusions = new String[] { SOME_KEY }; cpc.setExclusions(Arrays.asList(exclusions)); MDC.clear(); MDC.put(SOME_KEY, SOME_VALUE); MDC.put(SOME_OTHER_KEY, SOME_OTHER_VALUE); assertThat(mapFrom(formatProps(cpc)), is(mdcMap(exclusions))); }
@Test public void testTwoArgs() throws JSONObjectException, IOException { DefaultPropertiesConverter cpc = new DefaultPropertiesConverter(); MDC.clear(); MDC.put(SOME_KEY, SOME_VALUE); MDC.put(SOME_OTHER_KEY, SOME_OTHER_VALUE); assertThat(mapFrom(formatProps(cpc)), is(mdcMap())); }
@Override public String convert(ILoggingEvent event) { StringBuilder appendTo = new StringBuilder(); LogContext.loadContextFields(); converter.convert(event.getMDCPropertyMap(), appendTo); return appendTo.toString(); }
@Override public void start() { List<String> exclusionList = getOptionList(); if (exclusionList != null) { converter.setExclusions(exclusionList); } super.start(); } }
@Test public void testSingleArg() throws JSONObjectException, IOException { DefaultPropertiesConverter cpc = new DefaultPropertiesConverter(); MDC.clear(); MDC.put(SOME_KEY, SOME_VALUE); assertThat(mapFrom(formatProps(cpc)), is(mdcMap())); }
public void convert(Map<String, String> eventProperties, StringBuilder appendTo) { Map<String, String> properties = mergeContextMaps(eventProperties); if (properties != null && !properties.isEmpty()) { try { /* * -- let's compose an JSON object, then chop off the outermost * curly braces */ 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) { /* -- avoids substitute logger warnings on startup -- */ LoggerFactory.getLogger(DefaultPropertiesConverter.class).error("Conversion failed ", ex); } } }
protected String formatProps(DefaultPropertiesConverter pc) { StringBuilder sb = new StringBuilder(); pc.convert(MDC.getCopyOfContextMap(), sb); return sb.toString(); }
@Test public void testEmpty() throws JSONObjectException, IOException { DefaultPropertiesConverter cpc = new DefaultPropertiesConverter(); MDC.clear(); assertThat(mapFrom(formatProps(cpc)), is(mdcMap())); }