diff --git a/google-cloud-clients/google-cloud-contrib/google-cloud-logging-logback/src/main/java/com/google/cloud/logging/logback/LoggingAppender.java b/google-cloud-clients/google-cloud-contrib/google-cloud-logging-logback/src/main/java/com/google/cloud/logging/logback/LoggingAppender.java index 547ca81da6fa..23d5e4183fdf 100644 --- a/google-cloud-clients/google-cloud-contrib/google-cloud-logging-logback/src/main/java/com/google/cloud/logging/logback/LoggingAppender.java +++ b/google-cloud-clients/google-cloud-contrib/google-cloud-logging-logback/src/main/java/com/google/cloud/logging/logback/LoggingAppender.java @@ -36,6 +36,7 @@ import java.util.Collections; import java.util.HashSet; import java.util.List; +import java.util.Map; import java.util.Set; /** @@ -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 entry : e.getMDCPropertyMap().entrySet()) { + builder.addLabel(entry.getKey(), entry.getValue()); + } + if (loggingEnhancers != null) { for (LoggingEnhancer enhancer : loggingEnhancers) { enhancer.enhanceLogEntry(builder); diff --git a/google-cloud-clients/google-cloud-contrib/google-cloud-logging-logback/src/test/java/com/google/cloud/logging/logback/LoggingAppenderTest.java b/google-cloud-clients/google-cloud-contrib/google-cloud-logging-logback/src/test/java/com/google/cloud/logging/logback/LoggingAppenderTest.java index ed43475e3897..5855c80177d7 100644 --- a/google-cloud-clients/google-cloud-contrib/google-cloud-logging-logback/src/test/java/com/google/cloud/logging/logback/LoggingAppenderTest.java +++ b/google-cloud-clients/google-cloud-contrib/google-cloud-logging-logback/src/test/java/com/google/cloud/logging/logback/LoggingAppenderTest.java @@ -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() + .put("levelName", "INFO") + .put("levelValue", String.valueOf(20000L)) + .put("mdc1", "value1") + .put("mdc2", "value2") + .build()) + .build(); + logging.setFlushSeverity(Severity.ERROR); + Capture> 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");