JsonLogger extends AbstractLogger { @Override protected String format(LogLevel level, String message, Throwable e) { LogEvent logEvent = new LogEvent(level, message, e, this.targetClass); Map<String, String> log = new HashMap<>(); for (Converter converter : this.converters) { log.put(converter.getKey(), converter.convert(logEvent)); } return this.gson.toJson(log); } JsonLogger(Class<?> targetClass, Gson gson);  JsonLogger(String targetClass, Gson gson);   }
@Test public void testLog() { final IWriter output = Mockito.mock(IWriter.class); JsonLogger logger = new JsonLogger(JsonLoggerTest.class, new Gson()) { @Override protected void logger(LogLevel level, String message, Throwable e) { output.write(format(level, message, e)); } }; Assert.assertTrue(logger.isDebugEnable()); Assert.assertTrue(logger.isInfoEnable()); Assert.assertTrue(logger.isWarnEnable()); Assert.assertTrue(logger.isErrorEnable()); ArgumentCaptor<String> argument = ArgumentCaptor.forClass(String.class); logger.debug("hello world"); Mockito.verify(output, times(1)).write(argument.capture()); Assert.assertThat(argument.getValue(), createMatcher("hello world", "DEBUG", null)); logger.debug("hello {}", "world"); Mockito.verify(output, times(2)).write(argument.capture()); Assert.assertThat(argument.getValue(), createMatcher("hello world", "DEBUG", null)); logger.info("hello world"); Mockito.verify(output, times(3)).write(argument.capture()); Assert.assertThat(argument.getValue(), createMatcher("hello world", "INFO", null)); logger.info("hello {}", "world"); Mockito.verify(output, times(4)).write(argument.capture()); Assert.assertThat(argument.getValue(), createMatcher("hello world", "INFO", null)); logger.warn("hello {}", "world"); Mockito.verify(output, times(5)).write(argument.capture()); Assert.assertThat(argument.getValue(), createMatcher("hello world", "WARN", null)); logger.warn("hello world"); Mockito.verify(output, times(6)).write(argument.capture()); Assert.assertThat(argument.getValue(), createMatcher("hello world", "WARN", null)); logger.error("hello world"); Mockito.verify(output, times(7)).write(argument.capture()); Assert.assertThat(argument.getValue(), createMatcher("hello world", "ERROR", null)); Throwable t = new NullPointerException(); logger.error("hello world", t); Mockito.verify(output, times(8)).write(argument.capture()); Assert.assertThat(argument.getValue(), createMatcher("hello world", "ERROR", t)); logger.error(t, "hello {}", "world"); Mockito.verify(output, times(9)).write(argument.capture()); Assert.assertThat(argument.getValue(), createMatcher("hello world", "ERROR", t)); }