Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MDC support in Logback appender #4477

Merged
merged 1 commit into from
Mar 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
Expand Down Expand Up @@ -232,6 +233,10 @@ private LogEntry logEntryFor(ILoggingEvent e) {
.addLabel(LEVEL_NAME_KEY, level.toString())
.addLabel(LEVEL_VALUE_KEY, String.valueOf(level.toInt()));

for (Map.Entry<String, String> entry : e.getMDCPropertyMap().entrySet()) {
builder.addLabel(entry.getKey(), entry.getValue());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should check that its not overriding LEVEL_NAME_KEY and LEVEL_VALUE_KEY.

}

if (loggingEnhancers != null) {
for (LoggingEnhancer enhancer : loggingEnhancers) {
enhancer.enhanceLogEntry(builder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,36 @@ public void testDefaultWriteOptionsHasExpectedDefaults() {
// assertThat(resourceArg.getValue()).isEqualTo(defaultWriteOptions[1]);
}

@Test
public void testMdcValuesAreConvertedToLabels() {
LogEntry logEntry =
LogEntry.newBuilder(StringPayload.of("this is a test"))
.setTimestamp(100000L)
.setSeverity(Severity.INFO)
.setLabels(
new ImmutableMap.Builder<String, String>()
.put("levelName", "INFO")
.put("levelValue", String.valueOf(20000L))
.put("mdc1", "value1")
.put("mdc2", "value2")
.build())
.build();
logging.setFlushSeverity(Severity.ERROR);
Capture<Iterable<LogEntry>> capturedArgument = Capture.newInstance();
logging.write(capture(capturedArgument), (WriteOption) anyObject(), (WriteOption) anyObject());
expectLastCall().once();
replay(logging);
Timestamp timestamp = Timestamp.ofTimeSecondsAndNanos(100000, 0);
LoggingEvent loggingEvent = createLoggingEvent(Level.INFO, timestamp.getSeconds());
loggingEvent.setMDCPropertyMap(ImmutableMap.of("mdc1", "value1", "mdc2", "value2"));
loggingAppender.start();
// info event does not get logged
loggingAppender.doAppend(loggingEvent);
verify(logging);
assertThat(capturedArgument.getValue().iterator().hasNext()).isTrue();
assertThat(capturedArgument.getValue().iterator().next()).isEqualTo(logEntry);
}

private LoggingEvent createLoggingEvent(Level level, long timestamp) {
LoggingEvent loggingEvent = new LoggingEvent();
loggingEvent.setMessage("this is a test");
Expand Down