我正在使用 java.util.logging 为我的应用程序创建日志文件。到目前为止,效果非常好。
现在我遇到的问题是 info() 方法只需要一个字符串作为参数。但我有不同的数据类型要记录,例如整数、 double 、自定义对象。
我知道我可以自己构建字符串,我可以使用 String.format 等。
而且我也知道我可以使用 log() 方法。但这里我必须每次都设置日志级别并创建一个对象数组。
我正在寻找的是可以设置全局日志级别(一次),然后调用如下方法的东西:
log.info(String message, Object... values);
有支持的框架吗?
最佳答案
是的,有,它叫slf4j您正在寻找的特定 API 就是这个
org.slf4j.Logger#info(java.lang.String, java.lang.Object...)
This form avoids superfluous string concatenation when the logger is disabled for the INFO level. However, this variant incurs the hidden (and relatively small) cost of creating an
Object[]
before invoking the method, even if this logger is disabled for INFO. The variants taking {@link #info(String, Object) one} and {@link #info(String, Object, Object) two} arguments exist solely in order to avoid this hidden cost.
您应该看看jcl-over-slf4j.jar
在 slf4j 文档页面
Our JCL over SLF4J implementation will allow you to migrate to SLF4J gradually, especially if some of the libraries your software depends on continue to use JCL for the foreseeable future. You can immediately enjoy the benefits of SLF4J's reliability and preserve backward compatibility at the same time. Just replace commons-logging.jar with jcl-over-slf4j.jar
关于java - 具有不同数据类型的日志消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32949897/