Skip to content
This repository was archived by the owner on Apr 23, 2024. It is now read-only.

Commit 1e59016

Browse files
committed
Added Marker to JSON layout.
Signed-off-by: Daniel Roig <[email protected]>
1 parent c68c2e5 commit 1e59016

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

json/classic/src/main/java/ch/qos/logback/contrib/json/classic/JsonLayout.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import java.util.LinkedHashMap;
2222
import java.util.Map;
2323

24+
import org.slf4j.Marker;
25+
2426
/**
2527
* A JsonLayout builds its {@link #toJsonMap(ch.qos.logback.classic.spi.ILoggingEvent) jsonMap} from a
2628
* source {@link ch.qos.logback.classic.spi.ILoggingEvent ILoggingEvent} with the following keys/value pairs:
@@ -111,6 +113,7 @@ public class JsonLayout extends JsonLayoutBase<ILoggingEvent> {
111113
public static final String MESSAGE_ATTR_NAME = "raw-message";
112114
public static final String EXCEPTION_ATTR_NAME = "exception";
113115
public static final String CONTEXT_ATTR_NAME = "context";
116+
public static final String MARKER_ATTR_NAME = "marker";
114117

115118
protected boolean includeLevel;
116119
protected boolean includeThreadName;
@@ -120,6 +123,7 @@ public class JsonLayout extends JsonLayoutBase<ILoggingEvent> {
120123
protected boolean includeMessage;
121124
protected boolean includeException;
122125
protected boolean includeContextName;
126+
protected boolean includeMarker;
123127

124128
private ThrowableHandlingConverter throwableProxyConverter;
125129

@@ -133,6 +137,7 @@ public JsonLayout() {
133137
this.includeException = true;
134138
this.includeContextName = true;
135139
this.throwableProxyConverter = new ThrowableProxyConverter();
140+
this.includeMarker = true;
136141
}
137142

138143
@Override
@@ -147,13 +152,18 @@ public void stop() {
147152
this.throwableProxyConverter.stop();
148153
}
149154

155+
protected String getMarkerNameOrNull (Marker marker) {
156+
return marker != null ? marker.getName() : null;
157+
}
158+
150159
@Override
151160
protected Map toJsonMap(ILoggingEvent event) {
152161

153162
Map<String, Object> map = new LinkedHashMap<String, Object>();
154163

155164
addTimestamp(TIMESTAMP_ATTR_NAME, this.includeTimestamp, event.getTimeStamp(), map);
156165
add(LEVEL_ATTR_NAME, this.includeLevel, String.valueOf(event.getLevel()), map);
166+
add(MARKER_ATTR_NAME, this.includeMarker, getMarkerNameOrNull(event.getMarker()), map);
157167
add(THREAD_ATTR_NAME, this.includeThreadName, event.getThreadName(), map);
158168
addMap(MDC_ATTR_NAME, this.includeMDC, event.getMDCPropertyMap(), map);
159169
add(LOGGER_ATTR_NAME, this.includeLoggerName, event.getLoggerName(), map);
@@ -261,4 +271,13 @@ public ThrowableHandlingConverter getThrowableProxyConverter() {
261271
public void setThrowableProxyConverter(ThrowableHandlingConverter throwableProxyConverter) {
262272
this.throwableProxyConverter = throwableProxyConverter;
263273
}
274+
275+
public boolean isIncludeMarker() {
276+
return includeMarker;
277+
}
278+
279+
public void setIncludeMarker(boolean includeMarker) {
280+
this.includeMarker = includeMarker;
281+
}
282+
264283
}

json/classic/src/test/java/ch/qos/logback/contrib/json/classic/JsonLayoutTest.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import ch.qos.logback.core.joran.spi.JoranException;
2222
import org.junit.Test;
2323
import org.slf4j.LoggerFactory;
24+
import org.slf4j.Marker;
25+
import org.slf4j.MarkerFactory;
2426

2527
import java.util.*;
2628

@@ -137,6 +139,51 @@ public void jsonLayout() throws Exception {
137139
assertThat(logWithException, containsString(String.format("%s=%s", JsonLayout.MESSAGE_ATTR_NAME, debugMessage)));
138140
assertThat(logWithException, containsString(String.format("%s=%s", JsonLayout.EXCEPTION_ATTR_NAME, exception.toString())));
139141
}
142+
143+
@Test
144+
public void jsonLayoutWithMarker() throws Exception {
145+
configure("src/test/input/json/jsonLayout.xml");
146+
String loggerName = "ROOT";
147+
String message = "Info message with Marker";
148+
String debugMessage = "Debug message with Marker";
149+
Logger logger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
150+
Marker marker = MarkerFactory.getMarker("MYMARKER");
151+
logger.info(marker, "Test");
152+
ILoggingEvent event = new LoggingEvent("my.class.name", logger, Level.INFO, message, null, null);
153+
((LoggingEvent)event).setMarker(marker);
154+
155+
JsonLayout jsonLayout = new JsonLayout();
156+
jsonLayout.setContext(context);
157+
String log = jsonLayout.doLayout(event);
158+
159+
assertTimestamp(log);
160+
assertThat(log, containsString(String.format("%s=%s", JsonLayout.LEVEL_ATTR_NAME, Level.INFO)));
161+
assertThat(log, containsString(String.format("%s=%s", JsonLayout.THREAD_ATTR_NAME, "main")));
162+
assertThat(log, containsString(String.format("%s=%s", JsonLayout.LOGGER_ATTR_NAME, loggerName)));
163+
assertThat(log, containsString(String.format("%s=%s", JsonLayout.FORMATTED_MESSAGE_ATTR_NAME, message)));
164+
assertThat(log, containsString(String.format("%s=%s", JsonLayout.MARKER_ATTR_NAME, marker.getName())));
165+
166+
jsonLayout.setIncludeContextName(true);
167+
jsonLayout.setIncludeMDC(true);
168+
jsonLayout.setIncludeLoggerName(true);
169+
jsonLayout.setIncludeException(true);
170+
jsonLayout.setIncludeMessage(true);
171+
jsonLayout.setIncludeMarker(true);
172+
173+
RuntimeException exception = new RuntimeException("Exception");
174+
ILoggingEvent eventWithException = new LoggingEvent("my.class.name", logger, Level.DEBUG, debugMessage, exception, null);
175+
((LoggingEvent)eventWithException).setMarker(marker);
176+
String logWithException = jsonLayout.doLayout(eventWithException);
177+
178+
assertTimestamp(logWithException);
179+
assertThat(logWithException, containsString(String.format("%s=%s", JsonLayout.LEVEL_ATTR_NAME, Level.DEBUG)));
180+
assertThat(logWithException, containsString(String.format("%s=%s", JsonLayout.LOGGER_ATTR_NAME, loggerName)));
181+
assertThat(logWithException, containsString(String.format("%s=%s", JsonLayout.FORMATTED_MESSAGE_ATTR_NAME, debugMessage)));
182+
assertThat(logWithException, containsString(String.format("%s=%s", JsonLayout.MESSAGE_ATTR_NAME, debugMessage)));
183+
assertThat(logWithException, containsString(String.format("%s=%s", JsonLayout.EXCEPTION_ATTR_NAME, exception.toString())));
184+
assertThat(logWithException, containsString(String.format("%s=%s", JsonLayout.MARKER_ATTR_NAME, marker.getName())));
185+
186+
}
140187

141188
private void assertTimestamp(String log) {
142189
int timestamp = log.indexOf(JsonLayout.TIMESTAMP_ATTR_NAME);

0 commit comments

Comments
 (0)