diff --git a/json/classic/src/main/java/ch/qos/logback/contrib/json/classic/JsonLayout.java b/json/classic/src/main/java/ch/qos/logback/contrib/json/classic/JsonLayout.java index f71ff29..2c86583 100755 --- a/json/classic/src/main/java/ch/qos/logback/contrib/json/classic/JsonLayout.java +++ b/json/classic/src/main/java/ch/qos/logback/contrib/json/classic/JsonLayout.java @@ -21,6 +21,8 @@ import java.util.LinkedHashMap; import java.util.Map; +import org.slf4j.Marker; + /** * A JsonLayout builds its {@link #toJsonMap(ch.qos.logback.classic.spi.ILoggingEvent) jsonMap} from a * source {@link ch.qos.logback.classic.spi.ILoggingEvent ILoggingEvent} with the following keys/value pairs: @@ -111,6 +113,7 @@ public class JsonLayout extends JsonLayoutBase { public static final String MESSAGE_ATTR_NAME = "raw-message"; public static final String EXCEPTION_ATTR_NAME = "exception"; public static final String CONTEXT_ATTR_NAME = "context"; + public static final String MARKER_ATTR_NAME = "marker"; protected boolean includeLevel; protected boolean includeThreadName; @@ -120,6 +123,7 @@ public class JsonLayout extends JsonLayoutBase { protected boolean includeMessage; protected boolean includeException; protected boolean includeContextName; + protected boolean includeMarker; private ThrowableHandlingConverter throwableProxyConverter; @@ -133,6 +137,7 @@ public JsonLayout() { this.includeException = true; this.includeContextName = true; this.throwableProxyConverter = new ThrowableProxyConverter(); + this.includeMarker = true; } @Override @@ -147,6 +152,10 @@ public void stop() { this.throwableProxyConverter.stop(); } + protected String getMarkerNameOrNull (Marker marker) { + return marker != null ? marker.getName() : null; + } + @Override protected Map toJsonMap(ILoggingEvent event) { @@ -154,6 +163,7 @@ protected Map toJsonMap(ILoggingEvent event) { addTimestamp(TIMESTAMP_ATTR_NAME, this.includeTimestamp, event.getTimeStamp(), map); add(LEVEL_ATTR_NAME, this.includeLevel, String.valueOf(event.getLevel()), map); + add(MARKER_ATTR_NAME, this.includeMarker, getMarkerNameOrNull(event.getMarker()), map); add(THREAD_ATTR_NAME, this.includeThreadName, event.getThreadName(), map); addMap(MDC_ATTR_NAME, this.includeMDC, event.getMDCPropertyMap(), map); add(LOGGER_ATTR_NAME, this.includeLoggerName, event.getLoggerName(), map); @@ -261,4 +271,13 @@ public ThrowableHandlingConverter getThrowableProxyConverter() { public void setThrowableProxyConverter(ThrowableHandlingConverter throwableProxyConverter) { this.throwableProxyConverter = throwableProxyConverter; } + + public boolean isIncludeMarker() { + return includeMarker; + } + + public void setIncludeMarker(boolean includeMarker) { + this.includeMarker = includeMarker; + } + } diff --git a/json/classic/src/test/java/ch/qos/logback/contrib/json/classic/JsonLayoutTest.java b/json/classic/src/test/java/ch/qos/logback/contrib/json/classic/JsonLayoutTest.java index 1732b64..4cd7dc0 100644 --- a/json/classic/src/test/java/ch/qos/logback/contrib/json/classic/JsonLayoutTest.java +++ b/json/classic/src/test/java/ch/qos/logback/contrib/json/classic/JsonLayoutTest.java @@ -21,6 +21,8 @@ import ch.qos.logback.core.joran.spi.JoranException; import org.junit.Test; import org.slf4j.LoggerFactory; +import org.slf4j.Marker; +import org.slf4j.MarkerFactory; import java.util.*; @@ -137,6 +139,51 @@ public void jsonLayout() throws Exception { assertThat(logWithException, containsString(String.format("%s=%s", JsonLayout.MESSAGE_ATTR_NAME, debugMessage))); assertThat(logWithException, containsString(String.format("%s=%s", JsonLayout.EXCEPTION_ATTR_NAME, exception.toString()))); } + + @Test + public void jsonLayoutWithMarker() throws Exception { + configure("src/test/input/json/jsonLayout.xml"); + String loggerName = "ROOT"; + String message = "Info message with Marker"; + String debugMessage = "Debug message with Marker"; + Logger logger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME); + Marker marker = MarkerFactory.getMarker("MYMARKER"); + logger.info(marker, "Test"); + ILoggingEvent event = new LoggingEvent("my.class.name", logger, Level.INFO, message, null, null); + ((LoggingEvent)event).setMarker(marker); + + JsonLayout jsonLayout = new JsonLayout(); + jsonLayout.setContext(context); + String log = jsonLayout.doLayout(event); + + assertTimestamp(log); + assertThat(log, containsString(String.format("%s=%s", JsonLayout.LEVEL_ATTR_NAME, Level.INFO))); + assertThat(log, containsString(String.format("%s=%s", JsonLayout.THREAD_ATTR_NAME, "main"))); + assertThat(log, containsString(String.format("%s=%s", JsonLayout.LOGGER_ATTR_NAME, loggerName))); + assertThat(log, containsString(String.format("%s=%s", JsonLayout.FORMATTED_MESSAGE_ATTR_NAME, message))); + assertThat(log, containsString(String.format("%s=%s", JsonLayout.MARKER_ATTR_NAME, marker.getName()))); + + jsonLayout.setIncludeContextName(true); + jsonLayout.setIncludeMDC(true); + jsonLayout.setIncludeLoggerName(true); + jsonLayout.setIncludeException(true); + jsonLayout.setIncludeMessage(true); + jsonLayout.setIncludeMarker(true); + + RuntimeException exception = new RuntimeException("Exception"); + ILoggingEvent eventWithException = new LoggingEvent("my.class.name", logger, Level.DEBUG, debugMessage, exception, null); + ((LoggingEvent)eventWithException).setMarker(marker); + String logWithException = jsonLayout.doLayout(eventWithException); + + assertTimestamp(logWithException); + assertThat(logWithException, containsString(String.format("%s=%s", JsonLayout.LEVEL_ATTR_NAME, Level.DEBUG))); + assertThat(logWithException, containsString(String.format("%s=%s", JsonLayout.LOGGER_ATTR_NAME, loggerName))); + assertThat(logWithException, containsString(String.format("%s=%s", JsonLayout.FORMATTED_MESSAGE_ATTR_NAME, debugMessage))); + assertThat(logWithException, containsString(String.format("%s=%s", JsonLayout.MESSAGE_ATTR_NAME, debugMessage))); + assertThat(logWithException, containsString(String.format("%s=%s", JsonLayout.EXCEPTION_ATTR_NAME, exception.toString()))); + assertThat(logWithException, containsString(String.format("%s=%s", JsonLayout.MARKER_ATTR_NAME, marker.getName()))); + + } private void assertTimestamp(String log) { int timestamp = log.indexOf(JsonLayout.TIMESTAMP_ATTR_NAME);