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

Added Marker to JSON layout. #35

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
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -111,6 +113,7 @@ public class JsonLayout extends JsonLayoutBase<ILoggingEvent> {
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;
Expand All @@ -120,6 +123,7 @@ public class JsonLayout extends JsonLayoutBase<ILoggingEvent> {
protected boolean includeMessage;
protected boolean includeException;
protected boolean includeContextName;
protected boolean includeMarker;

private ThrowableHandlingConverter throwableProxyConverter;

Expand All @@ -133,6 +137,7 @@ public JsonLayout() {
this.includeException = true;
this.includeContextName = true;
this.throwableProxyConverter = new ThrowableProxyConverter();
this.includeMarker = true;
}

@Override
Expand All @@ -147,13 +152,18 @@ public void stop() {
this.throwableProxyConverter.stop();
}

protected String getMarkerNameOrNull (Marker marker) {
return marker != null ? marker.getName() : null;
}

@Override
protected Map toJsonMap(ILoggingEvent event) {

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

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);
Expand Down Expand Up @@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;

Expand Down Expand Up @@ -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);
Expand Down