CustomFieldsConverter extends LogEventPatternConverter { @Override public void format(LogEvent event, StringBuilder appendTo) { converter.convert(appendTo, getCustomFieldsFromMdc(event), getMessageParameters(event)); } CustomFieldsConverter(String[] options); static CustomFieldsConverter newInstance(final String[] options); @Override void format(LogEvent event, StringBuilder appendTo); static final String WORD; }
@Test public void forwardsStringBuilder() throws Exception { StringBuilder sb = new StringBuilder(); converter.format(event, sb); verify(defaultConverter).convert(same(sb), any(), any()); }
@Test public void emptyMdcAndArguments() throws Exception { StringBuilder sb = new StringBuilder(); when(event.getContextData()).thenReturn(new SortedArrayStringMap()); when(event.getMessage().getParameters()).thenReturn(NO_PARAMETERS); converter.format(event, sb); verifyConverterCall(emptyMap()); }
@Test public void standardArgument() throws Exception { StringBuilder sb = new StringBuilder(); when(event.getContextData()).thenReturn(new SortedArrayStringMap()); when(event.getMessage().getParameters()).thenReturn(new Object[] { "standard argument" }); converter.format(event, sb); verifyConverterCall(emptyMap(), is("standard argument")); }
@Test public void singleCustomFieldArgument() throws Exception { StringBuilder sb = new StringBuilder(); when(event.getContextData()).thenReturn(new SortedArrayStringMap()); CustomField customField = customField("some key", "some value"); when(event.getMessage().getParameters()).thenReturn(new Object[] { customField }); converter.format(event, sb); verifyConverterCall(emptyMap(), is(sameInstance(customField))); }
@Test public void mdcFields() throws Exception { StringBuilder sb = new StringBuilder(); when(event.getContextData()).thenReturn(new SortedArrayStringMap(MDC_PROPERTIES)); when(event.getMessage().getParameters()).thenReturn(NO_PARAMETERS); converter.format(event, sb); verifyConverterCall(allOf(hasEntry("this key", "this value"), hasEntry("that key", "that value"), hasEntry("other key", "other value"))); }
@Test public void mergesMdcFieldsAndArguments() throws Exception { StringBuilder sb = new StringBuilder(); when(event.getContextData()).thenReturn(new SortedArrayStringMap(MDC_PROPERTIES)); CustomField customField = customField("some key", "some value"); when(event.getMessage().getParameters()).thenReturn(new Object[] { customField }); converter.format(event, sb); verifyConverterCall(allOf(hasEntry("this key", "this value"), hasEntry("that key", "that value"), hasEntry("other key", "other value")), is(sameInstance(customField))); }