Laurent,

I believe the awt/net code started logging in 1.4 and 1.5 using j.u.logging that was really early taker before the performance overhead was considered.

I filed a bug 8011557: improve the logging documentation to advice on performance consideration as we may want to mention this in the tutorial or other docs as well. This should belong to java.util.logging instead of sun.util.logging.

Having a second thought, Supplier may not address the memory overhead concern you raise. Worth considering any API improvement to address both the runtime and memory concern. It would also be helpful if IDE and code analysis tool can hint the developers of such pattern.

Mandy
P.S. I'll be pushing the changeset today.

On 4/5/2013 9:02 AM, Laurent Bourgès wrote:
Mandy,

I agree it should be well known; but I fixed several cases in awt/net code where isLoggable() calls were missing.

I think it is quite cheap to remind good practices in the PlatformLogger / jul Logger javadoc ...

PS: maybe some quality control tools could check such missing tests (PMD can do it)...

    I believe this was mentioned somewhere in j.u.logging.  A better
    solution may be to take java.util.function.Supplier parameter that
    constructs the log message lazily (see
    
http://download.java.net/jdk8/docs/api/java/util/logging/Logger.html#fine(java.util.function.Supplier)
    
<http://download.java.net/jdk8/docs/api/java/util/logging/Logger.html#fine%28java.util.function.Supplier%29>.
    I can file a RFE to investigate the use of Supplier as in
    j.u.l.Logger.


Very interesting feature, but I am still not aware of such JDK 8 features (lambda) ... it seems nice but maybe the syntax will be more complicated / verbose than our usual log statements:
log.fine(""...)

Laurent



    On 4/5/2013 1:55 AM, Laurent Bourgès wrote:
    Mandy,

    I would like to add few performance advices in the PlatformLogger
    Javadoc:
    "
    NOTE: For performance reasons, PlatformLogger usages should take
    care of avoiding useless / wasted object creation and method
    calls related to *disabled* log statements:
    Always use isLoggable(level) wrapping logs at levels (FINEST,
    FINER, FINE, CONFIG):

    Bad practices:
    - string concat:
    log.fine("message" + value); // means
    StringBuilder(message).append(String.valueOf(value)).toString():
    2 objects created and value.toString() called
    - varags:
    log.fine("message {0}", this); // create an Object[]

    Best practices:
    if (log.isLoggable(PlatformLogger.FINE) {
    log.fine("message" + value);
    }

    if (log.isLoggable(PlatformLogger.FINE) {
    log.fine("message {0}", this);
    }
    "

    What is your opinion ?

    Thanks for the given explanations and I hope that this patch will
    be submitted soon to JDK8 repository.

    Laurent



Reply via email to