Search code examples
javaloggingjakarta-eeapache-commonsjava.util.logging

Get LogLevel for current class/bean


I am using the following approach when logging from my class:

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;    
...
private static Log log = LogFactory.getLog(MyClass.class);
...
log.debug("...");

It has come to my attention that all log statements are always executed no matter what loglevel is applied. I do not want to have debug-related statements executed when I do not need it (performance is an issue here).

So I am looking for something like this:

if (LogLevel == debug) {
   log.debug("...");
   ...
}

How can I obtain the current LogLevel being used for that class?


Solution

  • Each Log instance in the commons logging package can tell you if the specific level is enabled. Use this code:

    if ( log.isDebugEnabled() ) 
    {
        // Debug log statements
    }
    

    Check the Commons Logging JavaDoc for more information.