LogDataFormatter { static void formatBadLogData( RuntimeException error, LogData badLogData, SimpleLogHandler receiver) { StringBuilder errorMsg = new StringBuilder("LOGGING ERROR: ").append(error.getMessage()).append('\n'); int length = errorMsg.length(); try { appendLogData(badLogData, errorMsg); } catch (RuntimeException e) { errorMsg.setLength(length); errorMsg.append("Cannot append LogData: ").append(e); } Level level = badLogData.getLevel().intValue() < WARNING.intValue() ? WARNING : badLogData.getLevel(); receiver.handleFormattedLogMessage(level, errorMsg.toString(), error); } private  LogDataFormatter();   }
@Test public void testFormatBadLogData() { Throwable cause = new Throwable("Original Cause"); LogData data = FakeLogData.withPrintfStyle("Hello World").addMetadata(LogContext.Key.LOG_CAUSE, cause); SimpleLogHandler handler = getSimpleLogHandler(); RuntimeException error = new RuntimeException("Runtime Error"); LogDataFormatter.formatBadLogData(error, data, handler); String message = handler.toString(); assertThat(message).contains("message: Hello World"); assertThat(message).contains("level: INFO"); assertThat(message).contains("Original Cause"); }