|
21 | 21 | import ch.qos.logback.core.joran.spi.JoranException;
|
22 | 22 | import org.junit.Test;
|
23 | 23 | import org.slf4j.LoggerFactory;
|
| 24 | +import org.slf4j.Marker; |
| 25 | +import org.slf4j.MarkerFactory; |
24 | 26 |
|
25 | 27 | import java.util.*;
|
26 | 28 |
|
@@ -137,6 +139,51 @@ public void jsonLayout() throws Exception {
|
137 | 139 | assertThat(logWithException, containsString(String.format("%s=%s", JsonLayout.MESSAGE_ATTR_NAME, debugMessage)));
|
138 | 140 | assertThat(logWithException, containsString(String.format("%s=%s", JsonLayout.EXCEPTION_ATTR_NAME, exception.toString())));
|
139 | 141 | }
|
| 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 | + } |
140 | 187 |
|
141 | 188 | private void assertTimestamp(String log) {
|
142 | 189 | int timestamp = log.indexOf(JsonLayout.TIMESTAMP_ATTR_NAME);
|
|
0 commit comments