SLF4J的debug​方法(String msg,Throwable t)进行惰性求值。

huangapple go评论71阅读模式
英文:

SLF4J debug​(String msg, Throwable t) lazy evaluation

问题

在SLF4J中,参数化消息被用于提升日志记录性能。例如,如果你有以下代码:

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

将会在是否处于调试模式下评估entry。然而,通过使用参数化,只有在调试模式下才会对其进行评估:

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

我的问题是,是否有一种类似的方式用于异常日志记录,以避免在非调试模式下冗余的评估?

debug(String msg, Throwable t)
英文:

In SLF4J, parameterized messages are used to boost logging performance. For example, if you have the following:

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

Entry will be evaluate whether or not you are in debug mode. However, by using parameterized, it will only be evaluated if you are in debug mode.

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

My question is, is there a similar way for the exception logging to avoid redundant evaluation in non-debug mode?

debug​(String msg, Throwable t)

答案1

得分: 1

似乎在 slf4j 常见问题解答中找到了答案这里

从 SLF4J 1.6.0 版开始,只要异常对象是最后一个参数,错误日志记录中支持参数化日志记录。

String s = "Hello world";
try {
  Integer i = Integer.valueOf(s);
} catch (NumberFormatException e) {
  logger.error("无法格式化 {}", s, e); // 注意只有一对大括号。
}
英文:

Looks like it is answered in slf4j faq here

As of SLF4J 1.6.0, parameterized logging is supported in error method provided that the exception object is the last parameter.

String s = "Hello world";
try {
  Integer i = Integer.valueOf(s);
} catch (NumberFormatException e) {
  logger.error("Failed to format {}", s, e); // Notice only 1 pair of brackets. 
}

huangapple
  • 本文由 发表于 2020年10月14日 23:23:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/64356571.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定