Skip to content

Commit

Permalink
Merge pull request #152 from kszbcss/master
Browse files Browse the repository at this point in the history
LOGMGR-182 support for logging complete MDC map
  • Loading branch information
jamezp authored Jul 3, 2018
2 parents 6743142 + cef6f39 commit 2cc3e43
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/main/java/org/jboss/logmanager/formatters/Formatters.java
Original file line number Diff line number Diff line change
Expand Up @@ -1073,9 +1073,13 @@ public static FormatStep mdcFormatStep(final String key, final boolean leftJusti
public static FormatStep mdcFormatStep(final String key, final boolean leftJustify, final int minimumWidth, final boolean truncateBeginning, final int maximumWidth) {
return new JustifyingFormatStep(leftJustify, minimumWidth, truncateBeginning, maximumWidth) {
public void renderRaw(final StringBuilder builder, final ExtLogRecord record) {
final String value = record.getMdc(key);
if (value != null) {
builder.append(value);
if (key == null) {
builder.append(record.getMdcCopy());
} else {
final String value = record.getMdc(key);
if (value != null) {
builder.append(value);
}
}
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.jboss.logmanager.formatters;

import org.jboss.logmanager.ExtLogRecord;
import org.jboss.logmanager.MDC;
import org.jboss.logmanager.NDC;
import org.junit.Assert;
import org.junit.Test;
Expand Down Expand Up @@ -113,6 +114,28 @@ public void ndc() throws Exception {
Assert.assertEquals("value2.value3", formatter.format(record));
}

@Test
public void mdc() throws Exception {
try {
MDC.put("key1", "value1");
MDC.put("key2", "value2");
final ExtLogRecord record = createLogRecord("test");

PatternFormatter formatter = new PatternFormatter("%X{key1}");
Assert.assertEquals("value1", formatter.format(record));

formatter = new PatternFormatter("%X{not.found}");
Assert.assertEquals("", formatter.format(record));

formatter = new PatternFormatter("%X");
String formatted = formatter.format(record);
Assert.assertTrue(formatted.equals("{key1=value1, key2=value2}")
|| formatted.equals("{key2=value2, key1=value1}"));
} finally {
MDC.clear();
}
}

@Test
public void threads() throws Exception {
final ExtLogRecord record = createLogRecord("test");
Expand Down

0 comments on commit 2cc3e43

Please sign in to comment.