United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-6594697 varargs message and Throwable methods for java.util.Logger
JDK-6594697 : varargs message and Throwable methods for java.util.Logger

Details
Type:
Enhancement
Submit Date:
2007-08-20
Status:
Open
Updated Date:
2013-09-26
Project Name:
JDK
Resolved Date:
Component:
core-libs
OS:
linux
Sub-Component:
java.util.logging
CPU:
x86
Priority:
P3
Resolution:
Unresolved
Affected Versions:
7
Targeted Versions:
9

Related Reports

Sub Tasks

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
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
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



Hardware and Software, Engineered to Work Together