JsonLayout extends JsonLayoutBase<ILoggingEvent> { public JsonLayout() { super(); this.includeLevel = true; this.includeThreadName = true; this.includeMDC = true; this.includeLoggerName = true; this.includeFormattedMessage = true; this.includeException = true; this.includeContextName = true; this.throwableProxyConverter = new ThrowableProxyConverter(); } JsonLayout(); @Override void start(); @Override void stop(); boolean isIncludeLevel(); void setIncludeLevel(boolean includeLevel); boolean isIncludeLoggerName(); void setIncludeLoggerName(boolean includeLoggerName); boolean isIncludeFormattedMessage(); void setIncludeFormattedMessage(boolean includeFormattedMessage); boolean isIncludeMessage(); void setIncludeMessage(boolean includeMessage); boolean isIncludeMDC(); void setIncludeMDC(boolean includeMDC); boolean isIncludeThreadName(); void setIncludeThreadName(boolean includeThreadName); boolean isIncludeException(); void setIncludeException(boolean includeException); boolean isIncludeContextName(); void setIncludeContextName(boolean includeContextName); ThrowableHandlingConverter getThrowableProxyConverter(); void setThrowableProxyConverter(ThrowableHandlingConverter throwableProxyConverter); static final String TIMESTAMP_ATTR_NAME; static final String LEVEL_ATTR_NAME; static final String THREAD_ATTR_NAME; static final String MDC_ATTR_NAME; static final String LOGGER_ATTR_NAME; static final String FORMATTED_MESSAGE_ATTR_NAME; static final String MESSAGE_ATTR_NAME; static final String EXCEPTION_ATTR_NAME; static final String CONTEXT_ATTR_NAME; }
@Test public void jsonLayout() throws Exception { configure("src/test/input/json/jsonLayout.xml"); String loggerName = "ROOT"; String message = "Info message"; String debugMessage = "Debug message"; Logger logger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME); logger.info("Test"); ILoggingEvent event = new LoggingEvent("my.class.name", logger, Level.INFO, message, null, null); 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))); jsonLayout.setIncludeContextName(true); jsonLayout.setIncludeMDC(true); jsonLayout.setIncludeLoggerName(true); jsonLayout.setIncludeException(true); jsonLayout.setIncludeMessage(true); RuntimeException exception = new RuntimeException("Exception"); ILoggingEvent eventWithException = new LoggingEvent("my.class.name", logger, Level.DEBUG, debugMessage, exception, null); 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()))); }