Logger { public void error(Throwable exception, String format, Object... args) { if (logger.isLoggable(SEVERE)) { String message; try { message = format(format, args); } catch (IllegalFormatException e) { logger.log(SEVERE, illegalFormatMessageFor(Level.ERROR, format, args), e); message = rawMessageFor(format, args); } logger.log(SEVERE, message, exception); } } Logger(java.util.logging.Logger logger); static Logger get(Class<?> clazz); static Logger get(String name); void trace(String format, Object... args); void debug(String format, Object... args); void debug(Throwable exception, String format, Object... args); void info(String format, Object... args); void warn(Throwable exception, String format, Object... args); void warn(String format, Object... args); void error(Throwable exception, String format, Object... args); void error(Throwable exception); void error(String format, Object... args); boolean isDebugEnabled(); boolean isInfoEnabled();  }
@Test public void testErrorFormat() { logger.error("hello, %s", "you"); assertLog(Level.SEVERE, "hello, you"); @SuppressWarnings("ThrowableInstanceNeverThrown") Throwable exception = new Throwable(); logger.error(exception, "got exception: %s", "foo"); assertLog(Level.SEVERE, "got exception: foo", exception); @SuppressWarnings("ThrowableInstanceNeverThrown") Throwable exception2 = new Throwable("the message"); logger.error(exception2); assertLog(Level.SEVERE, exception2.getMessage(), exception2); }
@Test public void testErrorShortCircuit() { inner.setLevel(Level.OFF); logger.error("hello"); assertTrue(handler.isEmpty()); }
@Test public void testErrorWithThrowableShortCircuit() { inner.setLevel(Level.OFF); @SuppressWarnings("ThrowableInstanceNeverThrown") Throwable e = new Throwable(); logger.error(e, "hello"); assertTrue(handler.isEmpty()); }
@Test public void testErrorWithThrowableNoMessageShortCircuit() { inner.setLevel(Level.OFF); @SuppressWarnings("ThrowableInstanceNeverThrown") Throwable e = new Throwable(); logger.error(e); assertTrue(handler.isEmpty()); }
@Test public void testInsufficientArgsLogsErrorForError() { String format = "some message: %s, %d"; String param = "blah"; logger.error(format, param); assertLogLike(Level.SEVERE, ImmutableList.of("Invalid format", "ERROR", format, param), IllegalArgumentException.class); assertLog(Level.SEVERE, String.format("'%s' [%s]", format, param)); }
@Test public void testInsufficientArgsLogsOriginalExceptionForError() { Throwable exception = new Throwable("foo"); String format = "some message: %s, %d"; String param = "blah"; logger.error(exception, format, param); assertLogLike(Level.SEVERE, ImmutableList.of("Invalid format", "ERROR", format, param), IllegalArgumentException.class); assertLog(Level.SEVERE, String.format("'%s' [%s]", format, param), exception); }