diff --git a/src/main/java/net/logstash/log4j/JSONEventLayoutV1.java b/src/main/java/net/logstash/log4j/JSONEventLayoutV1.java index aaf3228..e7b9748 100644 --- a/src/main/java/net/logstash/log4j/JSONEventLayoutV1.java +++ b/src/main/java/net/logstash/log4j/JSONEventLayoutV1.java @@ -63,7 +63,12 @@ public String format(LoggingEvent loggingEvent) { threadName = loggingEvent.getThreadName(); timestamp = loggingEvent.getTimeStamp(); exceptionInformation = new HashMap(); - mdc = loggingEvent.getProperties(); + mdc = new HashMap(); + for (Object mdcObj : loggingEvent.getProperties().entrySet()) { + Map.Entry mapEntry = (Map.Entry) mdcObj; + String key = (String) mapEntry.getKey(); + mdc.put(key.replaceAll("\\.", "_"), mapEntry.getValue()); + } ndc = loggingEvent.getNDC(); logstashEvent = new JSONObject(); diff --git a/src/test/java/net/logstash/log4j/JSONEventLayoutV1Test.java b/src/test/java/net/logstash/log4j/JSONEventLayoutV1Test.java index 96ad821..9601da5 100644 --- a/src/test/java/net/logstash/log4j/JSONEventLayoutV1Test.java +++ b/src/test/java/net/logstash/log4j/JSONEventLayoutV1Test.java @@ -184,6 +184,19 @@ public void testJSONEventLayoutHasNestedMDC() { Assert.assertEquals("Nested MDC data is wrong", "baz", nested.get("bar")); } + @Test + public void testJSONEventLayoutEscapesMDCKeys() { + MDC.put("foo.bar","baz"); + logger.warn("I should escape MDC field names in my log"); + String message = appender.getMessages()[0]; + Object obj = JSONValue.parse(message); + JSONObject jsonObject = (JSONObject) obj; + JSONObject mdc = (JSONObject) jsonObject.get("mdc"); + + Assert.assertFalse("Event contains not escaped key", mdc.containsKey("foo.bar")); + Assert.assertTrue("Event does not contain escaped key", mdc.containsKey("foo_bar")); + } + @Test public void testJSONEventLayoutExceptions() { String exceptionMessage = new String("shits on fire, yo");