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

LOGMGR-182 support for logging complete MDC map #152

Merged
merged 1 commit into from
Jul 3, 2018
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
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 @@ -1008,9 +1008,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