Log4j隐式字符串格式 [英] Log4j Implicit String Formatting
问题描述
我正在使用log4j v1.2.14
登录项目,也正在使用Java 7
I am using log4j v1.2.14
for logging in my project and I am also using Java 7
String.format() to put variables in my output. Currently I am writing
LOGGER.info(String.format("Your var is [%s] and you are [%s]", myVar, myVar1));
这真的是输出字符串的最佳方法吗?我觉得log4j
应该如下隐式实现:
Is this really the best way to output strings? I feel that log4j
should have this implemented implicitly as below:
LOGGER.info("Your var is [%s] and you are [%s]", myVar, myVar1);
我错过了什么吗?此外,是否有任何Java日志记录框架支持此功能?
Have I missed something? Further, are there any Java logging frameworks that support this?
推荐答案
slf4j 的api提供了"参数化日志记录",尽管语法略有不同,但您可以做到这一点.示例如下:
slf4j's api provides "parameterized logging", which allows you to do exactly that, although with a slightly different syntax. The example there is:
logger.debug("Value {} was inserted between {} and {}.", newVal, below, above);
对于实现,您可以使用本地实现slf4j的Logback,也可以使用slf4j绑定与log4j或其他记录器连接. 用户手册对此进行了解释,并附有一个简短的示例.
For an implementation, you can use Logback which implements slf4j natively, or the slf4j bindings to connect with log4j or other loggers. The User Manual explains that, along with a short example.
这篇关于Log4j隐式字符串格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!