ExceptionMessageConverter extends ThrowableHandlingConverter { @Override public String convert(ILoggingEvent event) { Integer whitespace = getLeadingWhitespace(); if (whitespace < 0) { addWarn("Cannot render whitespace less than 0!"); whitespace = 0; } Integer depth = getDepth(); if (depth < 1) { addWarn("Cannot render depth less than 1!"); depth = 1; } String prefix = getPrefix(); String sep = getSeparator(); String suffix = getSuffix(); IThrowableProxy ex = event.getThrowableProxy(); if (ex == null) { return ""; } return processException(ex, whitespace, depth, prefix, sep, suffix); }  @Override String convert(ILoggingEvent event);  }
@Test public void testNoException() { ExceptionMessageConverter converter = new ExceptionMessageConverter(); LoggerContext context = new LoggerContext(); converter.setContext(context); converter.start(); LoggingEvent infoEvent = new LoggingEvent("fcqn", context.getLogger("fcqn"), Level.INFO, "info", null, null); String actual = converter.convert(infoEvent); assertThat(actual).contains(""); }
@Test public void testSingleMessage() { ExceptionMessageConverter converter = new ExceptionMessageConverter(); LoggerContext context = new LoggerContext(); converter.setContext(context); converter.start(); RuntimeException ex = new RuntimeException("Hello world"); LoggingEvent infoEvent = new LoggingEvent("fcqn", context.getLogger("fcqn"), Level.INFO, "info", ex, null); String actual = converter.convert(infoEvent); assertThat(actual).isEqualTo(" [Hello world]"); }
@Test public void testNestedMessages() { ExceptionMessageConverter converter = new ExceptionMessageConverter(); LoggerContext context = new LoggerContext(); converter.setContext(context); converter.start(); RuntimeException one = new RuntimeException("One"); RuntimeException two = new RuntimeException("Two", one); RuntimeException three = new RuntimeException("Three", two); RuntimeException four = new RuntimeException("Four", three); LoggingEvent infoEvent = new LoggingEvent("fcqn", context.getLogger("fcqn"), Level.INFO, "info", four, null); String actual = converter.convert(infoEvent); assertThat(actual).isEqualTo(" [Four > Three > Two > One]"); }
@Test public void testNestedMessagesWithCutOff() { ExceptionMessageConverter converter = new ExceptionMessageConverter(); converter.setOptionList(Arrays.asList("1", "2")); LoggerContext context = new LoggerContext(); converter.setContext(context); converter.start(); RuntimeException one = new RuntimeException("One"); RuntimeException two = new RuntimeException("Two", one); RuntimeException three = new RuntimeException("Three", two); RuntimeException four = new RuntimeException("Four", three); LoggingEvent infoEvent = new LoggingEvent("fcqn", context.getLogger("fcqn"), Level.INFO, "info", four, null); String actual = converter.convert(infoEvent); assertThat(actual).isEqualTo(" [Four > Three]"); }
@Test public void testNestedMessagesSeperator() { ExceptionMessageConverter converter = new ExceptionMessageConverter(); converter.setOptionList(Arrays.asList("1", "4", "[", " ! ")); LoggerContext context = new LoggerContext(); converter.setContext(context); converter.start(); RuntimeException one = new RuntimeException("One"); RuntimeException two = new RuntimeException("Two", one); RuntimeException three = new RuntimeException("Three", two); RuntimeException four = new RuntimeException("Four", three); LoggingEvent infoEvent = new LoggingEvent("fcqn", context.getLogger("fcqn"), Level.INFO, "info", four, null); String actual = converter.convert(infoEvent); assertThat(actual).isEqualTo(" [Four ! Three ! Two ! One]"); }
@Test public void testCustomPrefixSuffix() { ExceptionMessageConverter converter = new ExceptionMessageConverter(); converter.setOptionList(Arrays.asList("0", "4", "<", "|", ">")); LoggerContext context = new LoggerContext(); converter.setContext(context); converter.start(); RuntimeException one = new RuntimeException("One"); RuntimeException two = new RuntimeException("Two", one); RuntimeException three = new RuntimeException("Three", two); RuntimeException four = new RuntimeException("Four", three); LoggingEvent infoEvent = new LoggingEvent("fcqn", context.getLogger("fcqn"), Level.INFO, "info", four, null); String actual = converter.convert(infoEvent); assertThat(actual).isEqualTo("<Four|Three|Two|One>"); }