简体   繁体   English

log.info使用log.isInfoEnabled

[英]log.info using log.isInfoEnabled

We are using SLF4J 我们正在使用SLF4J

Based on a recent discussion in the team 基于团队最近的讨论

if(LOG.isDebugEnabled()){
  LOG.debug("hello " + a + " world" + b);
}

is better than 比...更好

LOG.debug("hello {} world {}", a, b);

because in the latter case, the String hello {} world {} is created even if "debug" is not enabled. 因为在后一种情况下,即使未启用“debug”,也会创建String hello {} world {} In other words, we are always creating Strings even when it is not required. 换句话说,即使不需要,我们也总是创建字符串。

I like the latter version as it improves readability significantly. 我喜欢后一版本,因为它显着提高了可读性。

Can anybody please provide inputs? 有人可以提供意见吗?

Regards, 问候,

Shardul. Shardul。

EDIT 编辑

Let me put it differently. 让我换一点吧。

Which is a better approach OR Which is the most widely accepted approach? 哪种方法更好?或者哪种方法最受欢迎?

Shardul. Shardul。

No, the shorter version does not create the " hello {} world {} " string. 不,较短的版本不会创建hello {} world {} ”字符串。 The string is already created and placed in the constant pool during compilation and class loading. 在编译和类加载期间,该字符串已创建并放置在常量池中。 You are always referencing the same instance from the constant pool, it is as cheap as referencing constant int . 您始终从常量池引用相同的实例,它与引用常量int一样便宜。

But the string is created in the first form since you are using string concatenation. 字符串在第一种形式,因为你使用字符串连接创建。

The only extra overhead is calling LOG.debug with three arguments which does nothing as it calls isDebugEnabled() internally. 唯一的额外开销是使用三个参数调用LOG.debug ,因为它在内部调用了isDebugEnabled() Also chances are it will be inlined. 它也有可能被内联。

That being said I would go for shorter form in 99% of the cases. 话虽如此,我会在99%的情况下采用缩短形式。 The only situation where I would explicitly call isDebugEnabled is when computing the message to be logged has a significant cost: 我明确调用isDebugEnabled的唯一情况是计算要记录的消息时会产生很大的成本:

if(LOG.isDebugEnabled()){
  LOG.debug("hello {} world {}", cheap, veryExpensive());
}

If veryExpensive() is, ekhem, very expensive, then it is smart to avoid calling it if it will be discarded. 如果非常昂贵veryExpensive() ,ekhem,非常昂贵,那么如果它将被丢弃则避免调用它是明智的。 BTW veryExpensive() should not have any side effects and it is hard to imagine long-running, side-effect free method... BTW veryExpensive()不应该有任何副作用 ,很难想象长期运行,无副作用的方法......

The performance impact of creating the String would be negligible. 创建String的性能影响可以忽略不计。 But as SLF4J documentation specifies "...this form does not incur the cost of parameter construction in case the log statement is disabled " meaning that the String is not even created. 但是,由于SLF4J文档指定“......如果禁用日志语句,此表单不会产生参数构造的成本”,这意味着甚至不创建字符串。

After some consideration, the "human overhead", ie the importance of code clarity made us decide in favor fo the second example. 经过一番考虑后,“人为开销”,即代码清晰度的重要性使我们决定支持第二个例子。 More so as there is also overhead involved in calling (and writing and reading) the isXEnabled mehod. 因此,调用(以及写入和读取) isXEnabled方法也会产生开销。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM