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

Add markers and mdc formatting to LoggingEvent.print() #27

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
20 changes: 19 additions & 1 deletion src/main/java/uk/org/lidalia/slf4jtest/LoggingEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,25 @@ public String apply(final Throwable throwableToPrint) {
}

private String formatLogStatement() {
return getTimestamp() + " [" + getThreadName() + "] " + getLevel() + safeLoggerName() + " - " + getFormattedMessage();
return getTimestamp() + " [" + getThreadName() + "] " + getLevel() + safeLoggerName() + getFormattedMdc() + getFormattedMarkers() + " - " + getFormattedMessage();
}

private String getFormattedMdc() {
StringBuilder sb = new StringBuilder();
if (getMdc() != null) {
for (Map.Entry entry : getMdc().entrySet()) {
sb.append(" " + entry.getKey() + "=" + entry.getValue());
}
}
return sb.toString();
}

private String getFormattedMarkers() {
if (getMarker().isPresent()) {
return " " + marker.get().toString();
} else {
return "";
}
}

private String safeLoggerName() {
Expand Down
14 changes: 13 additions & 1 deletion src/test/java/uk/org/lidalia/slf4jtest/LoggingEventTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import junitparams.JUnitParamsRunner;
import junitparams.Parameters;

import org.slf4j.MarkerFactory;
import uk.org.lidalia.slf4jext.Level;
import uk.org.lidalia.test.StaticTimeRule;
import uk.org.lidalia.test.SystemOutputRule;
Expand Down Expand Up @@ -56,7 +57,7 @@ public class LoggingEventTests {
mdc.put("key", "value");
}

Marker marker = mock(Marker.class);
Marker marker = MarkerFactory.getMarker("marker1");
Throwable throwable = new Throwable();
String message = "message";
Object arg1 = "arg1";
Expand Down Expand Up @@ -522,6 +523,17 @@ public void printWarnAndAbove(Level level) {
assertThat(systemOutputRule.getSystemOut(), is(""));
}

@Test
@Parameters({"INFO"})
public void printInfoWithMDCAndMarkers(Level level) {
LoggingEvent event = new LoggingEvent(level, mdc, marker, "message");
event.print();
assertThat(systemOutputRule.getSystemOut(),
is("1970-01-01T00:00:00.000Z ["+Thread.currentThread().getName()+"] INFO key=value marker1 - message" + lineSeparator())
);

}

@Test
public void nullArgument() {
LoggingEvent event = new LoggingEvent(level, "message with null arg", null, null);
Expand Down