CompactFormatter extends java.util.logging.Formatter { public String formatBackTrace(LogRecord record) { String site = ""; final Throwable t = record.getThrown(); if (t != null) { final Throwable root = apply(t); if (root != null) { site = findAndFormat(root.getStackTrace()); if (isNullOrSpaces(site)) { int limit = 0; for (Throwable c = t; c != null; c = c.getCause()) { site = findAndFormat(c.getStackTrace()); if (!isNullOrSpaces(site)) { break; } if (++limit == (1 << 16)) { break; } } } } } return site; } 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 public void testFormatBackTrace() { 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.formatBackTrace(record); assertTrue(result, result.startsWith("CompactFormatterTest")); assertTrue(result, result.contains("testFormatBackTrace")); assertTrue(result, Character.isDigit(result.charAt(result.length() - 2))); }
@Test public void testFormatBackTraceUnknown() { Exception e = new IOException("Fake I/O"); e.setStackTrace(new StackTraceElement[]{ new StackTraceElement(CompactFormatterTest.class.getName(), "testFormatBackTrace", null, -2)}); assertNotNull(e.getMessage(), e.getMessage()); CompactFormatter cf = new CompactFormatter(); LogRecord record = new LogRecord(Level.SEVERE, ""); record.setThrown(e); String result = cf.formatBackTrace(record); assertTrue(result, result.startsWith("CompactFormatterTest")); assertTrue(result, result.contains("testFormatBackTrace")); }
@Test(expected = NullPointerException.class) public void testFormatBackTraceNull() { CompactFormatter cf = new CompactFormatter(); cf.formatBackTrace((LogRecord) null); fail(cf.toString()); }
@Test(timeout = 30000) public void testFormatBackTraceEvil() { LogRecord record = new LogRecord(Level.SEVERE, ""); record.setThrown(createEvilThrowable()); CompactFormatter cf = new CompactFormatter(); cf.formatBackTrace(record); }