记录器格式和throwable,slf4j,参数 [英] Logger format and throwable, slf4j, arguments
问题描述
在将某些旧记录器从String.format
转换为较新的slf4j {}变体的过程中,我偶然发现了这种情况:
In the process of converting some old loggers from String.format
to the newer slf4j {} variant, I stumbled upon this case:
logger.error(String.format("%s ... %s ... %s", ...), e);
我只想使用{}并删除String格式,但是, 包括throwable的logger方法签名是:
I would like to use only {} and remove the String format, however, the logger method signature which includes the throwable is:
error(String msg, Throwable t)
所以在这种情况下我必须保留String.format
吗?
So I do have to keep the String.format
in this case ?!
为什么没有:
error(Throwable t, String format, Object... arguments)
推荐答案
从SLF4J 1.6.0开始,在存在多个参数的情况下,如果日志记录语句中的最后一个参数是异常,则SLF4J将假定用户希望将最后一个参数视为异常,而不是简单参数.
As of SLF4J 1.6.0, in the presence of multiple parameters and if the last argument in a logging statement is an exception, then SLF4J will presume that the user wants the last argument to be treated as an exception and not a simple parameter.
因此,编写(在SLF4J版本1.7.x及更高版本中)
So, writing (in SLF4J version 1.7.x and later)
logger.error("one two three: {} {} {}", "a", "b",
"c", new Exception("something went wrong"));
将做您想实现的目标...
Will do what you want to achieve...
这篇关于记录器格式和throwable,slf4j,参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!