开发者

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?


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.


Use slf4j as the logging facade for your logging framework, since it solves the problem you mentioned by using parametrized logging for improved performance.

If you want an even faster logging framework, use Logback.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜