CompactFormatter extends java.util.logging.Formatter { public String formatSource(final LogRecord record) { String source = record.getSourceClassName(); if (source != null) { if (record.getSourceMethodName() != null) { source = simpleClassName(source) + " " + record.getSourceMethodName(); } else { source = simpleClassName(source); } } else { source = simpleClassName(record.getLoggerName()); } return source; } 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 testFormatSourceByLogger() { CompactFormatter cf = new CompactFormatter(); LogRecord record = new LogRecord(Level.SEVERE, ""); record.setSourceMethodName(null); record.setSourceClassName(null); record.setLoggerName(Object.class.getName()); String result = cf.formatSource(record); assertEquals(Object.class.getSimpleName(), result); }
@Test(expected = NullPointerException.class) public void testFormatSourceNull() { CompactFormatter cf = new CompactFormatter(); cf.formatSource((LogRecord) null); fail(cf.toString()); }
@Test public void testFormatSourceByClass() { CompactFormatter cf = new CompactFormatter(); LogRecord record = new LogRecord(Level.SEVERE, ""); record.setSourceMethodName(null); record.setSourceClassName(Object.class.getName()); record.setLoggerName(""); String result = cf.formatSource(record); assertEquals(Object.class.getSimpleName(), result); }
@Test public void testFormatSourceByClassAndMethod() { CompactFormatter cf = new CompactFormatter(); LogRecord record = new LogRecord(Level.SEVERE, ""); record.setSourceMethodName("method"); record.setSourceClassName(Object.class.getName()); record.setLoggerName(""); String result = cf.formatSource(record); assertFalse(result, record.getSourceClassName().equals(record.getSourceMethodName())); assertTrue(result, result.startsWith(Object.class.getSimpleName())); assertTrue(result, result.endsWith(record.getSourceMethodName())); }