Skip to content

Commit

Permalink
backports changes from #2899
Browse files Browse the repository at this point in the history
  • Loading branch information
toadzky committed Oct 29, 2019
1 parent 53d1961 commit b9f652b
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public enum EventAttribute {
@JsonProperty("level") LEVEL,
@JsonProperty("threadName") THREAD_NAME,
@JsonProperty("mdc") MDC,
@JsonProperty("marker") MARKER,
@JsonProperty("loggerName") LOGGER_NAME,
@JsonProperty("message") MESSAGE,
@JsonProperty("exception") EXCEPTION,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
public class EventJsonLayoutBaseFactory extends AbstractJsonLayoutBaseFactory<ILoggingEvent> {

private EnumSet<EventAttribute> includes = EnumSet.of(EventAttribute.LEVEL,
EventAttribute.THREAD_NAME, EventAttribute.MDC, EventAttribute.LOGGER_NAME, EventAttribute.MESSAGE,
EventAttribute.EXCEPTION, EventAttribute.TIMESTAMP);
EventAttribute.THREAD_NAME, EventAttribute.MDC, EventAttribute.MARKER, EventAttribute.LOGGER_NAME,
EventAttribute.MESSAGE, EventAttribute.EXCEPTION, EventAttribute.TIMESTAMP);

private Set<String> includesMdcKeys = ImmutableSet.of();
private boolean flattenMdc = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ protected Map<String, Object> toJsonMap(ILoggingEvent event) {
.add("level", isIncluded(EventAttribute.LEVEL), String.valueOf(event.getLevel()))
.add("thread", isIncluded(EventAttribute.THREAD_NAME), event.getThreadName())
.add("logger", isIncluded(EventAttribute.LOGGER_NAME), event.getLoggerName())
.add("marker", isIncluded(EventAttribute.MARKER) && event.getMarker() != null, () -> event.getMarker().getName())
.add("message", isIncluded(EventAttribute.MESSAGE), event.getFormattedMessage())
.add("context", isIncluded(EventAttribute.CONTEXT_NAME), event.getLoggerContextVO().getName())
.add("version", jsonProtocolVersion != null, jsonProtocolVersion)
Expand All @@ -87,8 +88,8 @@ private Map<String, String> filterMdc(Map<String, String> mdcPropertyMap) {
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}

private boolean isIncluded(EventAttribute exception) {
return includes.contains(exception);
private boolean isIncluded(EventAttribute include) {
return includes.contains(include);
}

public ImmutableSet<EventAttribute> getIncludes() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.common.collect.Maps;

import java.util.function.Supplier;
import javax.annotation.Nullable;
import java.util.Map;

Expand Down Expand Up @@ -65,6 +66,20 @@ public MapBuilder add(String fieldName, boolean include, @Nullable Map<String, ?
return this;
}

/**
* Adds the string value to the provided map under the provided field name,
* if it should be included. The supplier is only invoked if the field is to be included.
*/
public MapBuilder add(String fieldName, boolean include, Supplier<String> supplier) {
if (include) {
String value = supplier.get();
if (value != null) {
map.put(getFieldName(fieldName), value);
}
}
return this;
}

/**
* Adds and optionally formats the timestamp to the provided map under the provided field name,
* if it's should be included.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import java.io.PrintStream;
import java.util.Collections;
import java.util.concurrent.TimeUnit;
import org.slf4j.Marker;
import org.slf4j.MarkerFactory;

import static java.util.Objects.requireNonNull;
import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -119,14 +121,16 @@ public void testLogJsonToConsole() throws Exception {
try {
System.setOut(new PrintStream(redirectedStream));
defaultLoggingFactory.configure(new MetricRegistry(), "json-log-test");
LoggerFactory.getLogger("com.example.app").info("Application log");
Marker marker = MarkerFactory.getMarker("marker");
LoggerFactory.getLogger("com.example.app").info(marker, "Application log");
Thread.sleep(100); // Need to wait, because the logger is async

JsonNode jsonNode = objectMapper.readTree(redirectedStream.toString());
assertThat(jsonNode).isNotNull();
assertThat(jsonNode.get("timestamp").isTextual()).isTrue();
assertThat(jsonNode.get("level").asText()).isEqualTo("INFO");
assertThat(jsonNode.get("logger").asText()).isEqualTo("com.example.app");
assertThat(jsonNode.get("marker").asText()).isEqualTo("marker");
assertThat(jsonNode.get("message").asText()).isEqualTo("Application log");
} finally {
System.setOut(old);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.EnumSet;
import java.util.Map;
import java.util.Set;
import org.slf4j.Marker;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;
Expand All @@ -36,9 +37,10 @@ public class EventJsonLayoutTest {
private ObjectMapper objectMapper = Jackson.newObjectMapper();
private JsonFormatter jsonFormatter = new JsonFormatter(objectMapper, false, true);
private ILoggingEvent event = Mockito.mock(ILoggingEvent.class);
private Marker marker = Mockito.mock(Marker.class);

private Set<EventAttribute> includes = EnumSet.of(EventAttribute.LEVEL,
EventAttribute.THREAD_NAME, EventAttribute.MDC, EventAttribute.LOGGER_NAME, EventAttribute.MESSAGE,
EventAttribute.THREAD_NAME, EventAttribute.MDC, EventAttribute.LOGGER_NAME, EventAttribute.MARKER, EventAttribute.MESSAGE,
EventAttribute.EXCEPTION, EventAttribute.TIMESTAMP);
private EventJsonLayout eventJsonLayout = new EventJsonLayout(jsonFormatter, timestampFormatter, throwableProxyConverter,
includes, ImmutableMap.of(), ImmutableMap.of(), ImmutableSet.of(), false);
Expand All @@ -49,6 +51,8 @@ public void setUp() {
when(event.getLevel()).thenReturn(Level.INFO);
when(event.getThreadName()).thenReturn("main");
when(event.getMDCPropertyMap()).thenReturn(mdc);
when(event.getMarker()).thenReturn(marker);
when(marker.getName()).thenReturn("marker");
when(event.getLoggerName()).thenReturn(logger);
when(event.getFormattedMessage()).thenReturn(message);
when(event.getLoggerContextVO()).thenReturn(new LoggerContextVO("test", ImmutableMap.of(), 0));
Expand All @@ -61,6 +65,7 @@ public void testProducesDefaultMap() {
entry("thread", "main"),
entry("level", "INFO"),
entry("logger", logger),
entry("marker", "marker"),
entry("message", message),
entry("mdc", mdc));
}
Expand All @@ -74,6 +79,7 @@ public void testLogsAnException() {
entry("thread", "main"),
entry("level", "INFO"),
entry("logger", logger),
entry("marker", "marker"),
entry("message", message),
entry("mdc", mdc),
entry("exception", "Boom!"));
Expand All @@ -88,6 +94,7 @@ public void testDisableTimestamp() {
entry("thread", "main"),
entry("level", "INFO"),
entry("logger", logger),
entry("marker", "marker"),
entry("message", message),
entry("mdc", mdc));
}
Expand All @@ -100,6 +107,7 @@ public void testLogVersion() {
entry("thread", "main"),
entry("level", "INFO"),
entry("logger", logger),
entry("marker", "marker"),
entry("message", message),
entry("mdc", mdc),
entry("version", "1.2"));
Expand All @@ -115,6 +123,7 @@ public void testReplaceFieldName() {
entry("thread", "main"),
entry("level", "INFO"),
entry("logger", logger),
entry("marker", "marker"),
entry("@message", message),
entry("mdc", mdc));
}
Expand All @@ -129,6 +138,7 @@ public void testAddNewField() {
entry("thread", "main"),
entry("level", "INFO"),
entry("logger", logger),
entry("marker", "marker"),
entry("message", message),
entry("mdc", mdc),
entry("serviceName", "userService"),
Expand All @@ -144,6 +154,7 @@ public void testFilterMdc() {
entry("thread", "main"),
entry("level", "INFO"),
entry("logger", logger),
entry("marker", "marker"),
entry("message", message),
entry("mdc", ImmutableMap.of("userId", "18", "orderId", "24")));
}
Expand All @@ -156,12 +167,40 @@ public void testFlattensMdcMap() {
entry("thread", "main"),
entry("level", "INFO"),
entry("logger", logger),
entry("marker", "marker"),
entry("message", message),
entry("userId", "18"),
entry("serviceId", "19"),
entry("orderId", "24"));
}

@Test
public void testDisabledMarker() {
final EnumSet<EventAttribute> limitedIncludes = EnumSet.of(EventAttribute.LEVEL,
EventAttribute.THREAD_NAME, EventAttribute.MDC, EventAttribute.LOGGER_NAME, EventAttribute.MESSAGE,
EventAttribute.EXCEPTION, EventAttribute.TIMESTAMP);
Map<String, Object> map = new EventJsonLayout(jsonFormatter, timestampFormatter, throwableProxyConverter, limitedIncludes,
ImmutableMap.of(), ImmutableMap.of(), ImmutableSet.of(), false).toJsonMap(event);
assertThat(map).containsOnly(entry("timestamp", timestamp),
entry("thread", "main"),
entry("level", "INFO"),
entry("logger", logger),
entry("message", message),
entry("mdc", mdc));
}

@Test
public void testNoMarker() {
when(event.getMarker()).thenReturn(null);
Map<String, Object> map = eventJsonLayout.toJsonMap(event);
assertThat(map).containsOnly(entry("timestamp", timestamp),
entry("thread", "main"),
entry("level", "INFO"),
entry("logger", logger),
entry("message", message),
entry("mdc", mdc));
}

@Test
public void testStartThrowableConverter() {
eventJsonLayout.start();
Expand Down

0 comments on commit b9f652b

Please sign in to comment.