CompactFormatter extends java.util.logging.Formatter { public String formatThrown(final LogRecord record) { String msg; final Throwable t = record.getThrown(); if (t != null) { final Throwable root = apply(t); if (root != null) { msg = formatMessage(t); String site = formatBackTrace(record); msg = root.getClass().getSimpleName() + ": " + msg + (isNullOrSpaces(site) ? "" : ' ' + site); } else { msg = ""; } } else { msg = ""; } return msg; } CompactFormatter();  CompactFormatter(final String format); @Override String format(final LogRecord record); @Override String formatMessage(final LogRecord record); String formatMessage(final Throwable t); String formatLevel(final LogRecord record); String formatSource(final LogRecord record); String formatLoggerName(final LogRecord record); String formatThrown(final LogRecord record); String formatBackTrace(LogRecord record);  }
@Test(timeout = 30000) public void testFormatThrownEvilThrown() { LogRecord record = new LogRecord(Level.SEVERE, ""); record.setThrown(createEvilThrowable()); CompactFormatter cf = new CompactFormatter(); cf.formatThrown(record); }
@Test public void testFormatThrown() { Exception e = new IOException("Fake I/O"); e = new Exception(e.toString(), e); assertNotNull(e.getMessage(), e.getMessage()); CompactFormatter cf = new CompactFormatter(); LogRecord record = new LogRecord(Level.SEVERE, ""); record.setThrown(e); String result = cf.formatThrown(record); assertTrue(result, result.startsWith(e.getCause().getClass().getSimpleName())); assertTrue(result, result.contains(cf.formatMessage(record.getThrown()))); assertTrue(result, result.endsWith(cf.formatBackTrace(record))); }
@Test(expected = NullPointerException.class) public void testFormatThrownNullRecord() { CompactFormatter cf = new CompactFormatter(); cf.formatThrown((LogRecord) null); fail(cf.toString()); }