是否需要进行 if(log.isDebugEnabled()) { ... } 检查?

发布于 2024-11-17 09:34:55 字数 611 浏览 0 评论 0原文

是否需要进行显式 if(log.isDebugEnabled()) { ... } 检查?

我的意思是我看到一些帖子提到 log.debug("something") 在进行日志记录之前会进行隐式调用以查看是否已启用调试模式日志记录。我是否遗漏了某些内容,或者在使用它之前是否需要执行中间步骤?

谢谢!

log.debug("ResultSet rs is retrieved from OracleTypes");

if(log.isDebugEnabled()){
     log.debug("ResultSet rs is retrieved from OracleTypes");
}

编辑: 对此写过一篇文章: http://java.sg/whether-to -do-a-isdebugenabled-checking-before-printing-out-your-log-statement/

is there a need to do an explicit if(log.isDebugEnabled()) { ... } check?

I mean i have seen some post mentioning that log.debug("something") does an implicit call to see if debug mode logging has been enabled, before it does the logging. Am i missing something or is there an intermediary step that is to be performed before this is used?

Thanks!

log.debug("ResultSet rs is retrieved from OracleTypes");

vs

if(log.isDebugEnabled()){
     log.debug("ResultSet rs is retrieved from OracleTypes");
}

Edit:
Did a write up on this:
http://java.sg/whether-to-do-a-isdebugenabled-checking-before-printing-out-your-log-statement/

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

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

发布评论

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

评论(5

神经暖 2024-11-24 09:34:55

该声明:

if(log.isDebugEnabled()){

仅出于性能原因而使用。它的使用是可选的,因为它是由 log 方法内部调用的。

但现在你问这个检查是内部做的,那我为什么要用它呢?
这非常简单:如果您记录像这样简单的内容:

log.debug("ResultSet rs is retrieved from OracleTypes");

那么您不需要进行任何检查。如果您使用追加运算符 (+) 编写要记录的字符串,如下所示:

log.debug("[" + System.getTimeInMillis() + "] ResultSet rs is retrieved from OracleTypes");

在这种情况下,您应该检查是否启用了日志,因为如果没有启用,即使不创建日志,字符串组成也是如此。而且我必须提醒您,使用运算符“+”来连接字符串的效率非常低。

The statement:

if(log.isDebugEnabled()){

Is used just for performance reasons. It's use is optional since it is called by the log method internally.

But now you ask if this check is made internally, so why should I use it?
It's very simple: if you log something as simple as this:

log.debug("ResultSet rs is retrieved from OracleTypes");

Then you don't need to do any check. If you compose a string to log using the append operator (+) like this:

log.debug("[" + System.getTimeInMillis() + "] ResultSet rs is retrieved from OracleTypes");

In this case you should check if the log is enabled or not, because if it isn't, even if the log is not made, the string composition is. And I must remind you that the use of operator "+" to concatenate strings is very inefficient.

花之痕靓丽 2024-11-24 09:34:55

我知道这已经很旧了,但是对于任何刚刚发现这个的人来说...

如果您使用 SLF4J,您可以通过使用消息格式来避免 isDebugEnabled() 调用。

例如,而不是:

Object entry = new SomeObject();
logger.debug("The entry is " + entry + ".");

使用:

Object entry = new SomeObject();
logger.debug("The entry is {}.", entry);

除非启用调试,否则不会评估消息格式。

因此,对于简单的情况,您可以避免 isDebugEnabled()。

但在构建其中一个参数可能成本高昂的情况下,您仍然需要使用 isDebugEnabled() (即使使用 SLF4J)。

例如:

if (logger.isDebugEnabled()) {
    logger.debug("Here is the SQL: {}", sqlWrapper.buildSQL());  // assume buildSQL() is an expensive operation
}

在这种情况下,除非实际启用了调试,否则您不想评估 buildSQL()。

对于 SLF4J,关于始终使用它还是有选择地使用它存在一些争论。这确实取决于个人喜好。您可能希望在任何地方使用,以防止其他开发人员(在不知不觉中)将您的日志消息更改为将来更复杂/昂贵的内容。

I know this is old, but for anyone just finding this...

If you use SLF4J, you can avoid the isDebugEnabled() call, by using messaging formatting.

For example, instead of:

Object entry = new SomeObject();
logger.debug("The entry is " + entry + ".");

Use:

Object entry = new SomeObject();
logger.debug("The entry is {}.", entry);

The message formatting will not be evaluated unless debug is enabled.

So, for simple cases, you could avoid isDebugEnabled().

But in a case where building one of the parameters might be expensive, you would still want to use isDebugEnabled() (even with SLF4J).

For example:

if (logger.isDebugEnabled()) {
    logger.debug("Here is the SQL: {}", sqlWrapper.buildSQL());  // assume buildSQL() is an expensive operation
}

In that case, you don't want to evaluate buildSQL() unless debug is actually enabled.

With SLF4J, there's some debate over using it always vs using it selectively. It really boils down to personal preference. You may want to use everywhere, to guard against another developer (unknowingly) changing your log message to something more complex/expensive in the future.

傾城如夢未必闌珊 2024-11-24 09:34:55

Logger 的最新版本简化了这一点,因此没有太大区别。

最大的区别是您不必创建要记录的内容 - 有时会添加大量字符串。

The recent versions of Logger streamline this, so there's not much difference.

The biggest difference is you don't have to create the stuff to log - sometimes there's a lot of string addition going on.

Oo萌小芽oO 2024-11-24 09:34:55

我通过在代码中执行检查与不执行检查来检查以下代码。有趣的是,如果在我们的代码中对执行一百万次的 4 个日志语句执行检查,则需要额外花费 400 毫秒。我正在使用 SLF4J 1.6.6。如果您可以承受每百万个请求 400 毫秒的损失,则不需要检查。

    long startTime = System.currentTimeMillis();
    for (int i = 0; i < 1000000; i++) {
        if (logger.isTraceEnabled()) {
            logger.trace(request.getUserID());
            logger.trace(request.getEntitlementResource().getResourceString());
            logger.trace(request.getEntitlementResource().getActionString());
            logger.trace(request.getContextMap());
        }
    }
    long endTime = System.currentTimeMillis();
    logger.fatal("With Check Enabled : " + (endTime - startTime) + " ms");

    startTime = System.currentTimeMillis();
    for (int i = 0; i < 1000000; i++) {

        logger.trace(request.getUserID());
        logger.trace(request.getEntitlementResource().getResourceString());
        logger.trace(request.getEntitlementResource().getActionString());
        logger.trace(request.getContextMap());

    }
    endTime = System.currentTimeMillis();
    logger.fatal("With Check Disabled : " + (endTime - startTime)  + " ms");

---输出---

*2016-01-07 10:49:11,501 错误 [:http-bio-8080-exec-3] [com.citi.cmb.entitlement.service.EntitlementServiceImpl][]- 启用检查: 661 毫秒

2016-01-07 10:49:57,141 错误[:http-bio-8080-exec-3] [com.citi.cmb.entitlement.service.EntitlementServiceImpl][]- 禁用检查:1043 毫秒

I checked with the below code by performing the check in my code vs not performing the check. Interestingly if the check is performed in our code for a 4 log statement executed a million times it takes 400 ms extra. I am using SLF4J 1.6.6. If you can afford to loose 400 ms per million request ,you don't need the check.

    long startTime = System.currentTimeMillis();
    for (int i = 0; i < 1000000; i++) {
        if (logger.isTraceEnabled()) {
            logger.trace(request.getUserID());
            logger.trace(request.getEntitlementResource().getResourceString());
            logger.trace(request.getEntitlementResource().getActionString());
            logger.trace(request.getContextMap());
        }
    }
    long endTime = System.currentTimeMillis();
    logger.fatal("With Check Enabled : " + (endTime - startTime) + " ms");

    startTime = System.currentTimeMillis();
    for (int i = 0; i < 1000000; i++) {

        logger.trace(request.getUserID());
        logger.trace(request.getEntitlementResource().getResourceString());
        logger.trace(request.getEntitlementResource().getActionString());
        logger.trace(request.getContextMap());

    }
    endTime = System.currentTimeMillis();
    logger.fatal("With Check Disabled : " + (endTime - startTime)  + " ms");

---OUTPUT---

*2016-01-07 10:49:11,501 ERROR [:http-bio-8080-exec-3] [com.citi.cmb.entitlement.service.EntitlementServiceImpl][]- With Check Enabled : 661 ms

2016-01-07 10:49:57,141 ERROR [:http-bio-8080-exec-3] [com.citi.cmb.entitlement.service.EntitlementServiceImpl][]- With Check Disabled : 1043 ms

不可一世的女人 2024-11-24 09:34:55

这样做的原因是出于性能原因。如果首先检查这一点,则不应评估 log.debug(... 语句。

它在功能上确实是相同的。

The reason this is done is for performance reasons. If checking this first, the log.debug(... statement shouldn't be evaluated.

It is indeed functionally the same.

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