The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > java.util.logging  [20 examples]

e389. Minimizing the Impact of Logging Code

It is good to add logging code to an application, but the logging code should minimize its impact on the application, especially if the logging is not enabled. In particular, if the message to be potentially logged needs to be constructed, the method call should be wrapped in a cheaper check. For example, the method call
    int count = 123;
    Logger logger = Logger.getLogger("com.mycompany.MyClass");
    logger.finest("count: "+count);
will cause the count to be converted to a string and then concatenated to another string. This is a lot of wasted work if the message will not be logged. To avoid this overhead, use Logger.isLoggable() to check if the message would be logged before calling the logging method. For example,
    
    if (logger.isLoggable(Level.FINEST)) {
        logger.finest("count: "+count);
    }

 Related Examples
e385. The Quintessential Logging Program
e386. Determining If a Message Will Be Logged
e387. Logging a Method Call
e388. Logging an Exception
e390. Preventing a Logger from Forwarding Log Records to Its Parent
e391. Writing Log Records to a Log File
e392. Writing Log Records to Standard Error
e393. Writing Log Records Only After a Condition Occurs
e394. Setting a Filter on a Logger Handler

See also: Configuration    File Size    Formatters    Levels   


© 2002 Addison-Wesley.