获取当前类/bean 的 LogLevel

发布于 2024-10-06 18:46:35 字数 424 浏览 0 评论 0原文

从类中记录时,我使用以下方法:

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

我注意到,无论应用什么日志级别,所有日志语句始终都会执行。我不想在不需要时执行与调试相关的语句(性能是这里的一个问题)。

所以我正在寻找这样的东西:

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

如何获取该类当前使用的 LogLevel?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甚是思念 2024-10-13 18:46:35

commons 日志包中的每个 Log 实例都可以告诉您是否启用了特定级别。使用此代码:

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

检查 Commons Logging JavaDoc了解更多信息。

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.

只怪假的太真实 2024-10-13 18:46:35

使用 slf4j 作为日志框架的日志外观,因为它通过使用 < 解决了您提到的问题a href="http://www.slf4j.org/faq.html#logging_performance" rel="nofollow">参数化日志记录以提高性能。

如果您想要更快的日志记录框架,请使用 Logback

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文