JDK-6594697 : varargs message and Throwable methods for java.util.Logger
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.util.logging
  • Affected Version: 7
  • Priority: P3
  • Status: Open
  • Resolution: Unresolved
  • OS: linux
  • CPU: x86
  • Submitted: 2007-08-20
  • Updated: 2015-10-16
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
tbd_majorUnresolved
Description
A DESCRIPTION OF THE REQUEST :
It would be nice if there were printf-style, varargs methods for logging in the Logger class.

JUSTIFICATION :
Being able to specify: logger.log(Level.DEBUG, "yada yada %s %s %s", s1, s2, s3, ex); Could increase performance by allowing the string concat & formatting to only take place if the debug level enable. This would clean up a lot of code that currently does:

if(logger.isDebugEnabled() {
   logger.log(level.DEBUG, "yada yada" + s1 + s2 + s3", ex);
}

If the last argument in the vararg list is a throwable, then it could be considered the "thrown" argument.

Comments
Using a Supplier<String> seems an adequate solution for this use case. logger.log(Level.DEBUG, ex, () -> String.format("yada yada %s %s %s", s1, s2, s3)); In addition - java.util.Formatter only supports java.text.MessageFormat - not String.format. Using a Supplier<String> makes it possible to choose your own format.
2015-10-16

In jdk8, you can define your Supplier of the log message using lambda: logger.log(Level.DEBUG, ex, () -> System.out.format("yada yada %s %s %s", s1, s2, s3));
2013-09-26

I think what we should have along these lines is something like this: public void log(Level level, String msg, Throwable thrown, Object... params); public void logp(Level level, String sourceClass, String sourceMethod, String msg, Throwable thrown, Object... params); public void logrb(Level level, String sourceClass, String sourceMethod, String bundleName, String msg, Throwable thrown, Object... param); It would be more general and satisfy the stated need well.
2012-10-25