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

Replace dots with underscores in MDC #65

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
7 changes: 6 additions & 1 deletion src/main/java/net/logstash/log4j/JSONEventLayoutV1.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,12 @@ public String format(LoggingEvent loggingEvent) {
threadName = loggingEvent.getThreadName();
timestamp = loggingEvent.getTimeStamp();
exceptionInformation = new HashMap<String, Object>();
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();
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/net/logstash/log4j/JSONEventLayoutV1Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down