英文:
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.
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论