无论是 java.util.logging、commons-logging、log4j,甚至是最新的 slf4j 和 logback,这些 API 都没有提出方法,例如:
void debug(String format, Object... args);
其中 format
是使用 Formatter
解析的字符串。相反,他们坚持使用 MessageFormat
。
我知道它会与现有原型(prototype)冲突,所以为什么不使用 debugf()
或类似的方法,但为什么没有这样的方法,因为 Formatter
是和 Java 1.5 一样古老?
是否出于性能原因、向后兼容性...?
(注意,我真的在谈论Formatter
,即包括占位符,例如%s
、%02x
等)
最佳答案
slf4j 可以。参见 http://www.slf4j.org/apidocs/org/slf4j/Logger.html .这是 slf4j 相对于 log4j 的主要优势(对我而言)之一。它避免了到处都需要 isDebugEnabled()
。
编辑:
slf4j documentation解释了为什么它不使用 JDK 的标准格式:
SLF4J uses its own message formatting implementation which differs from that of the Java platform. This is justified by the fact that SLF4J's implementation performs about 10 times faster but at the cost of being non-standard and less flexible.
关于java - 出于好奇——为什么日志记录 API 不实现类似 printf() 的日志记录方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8635769/