Java Log4j 隐式字符串格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30975026/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 10:27:28  来源:igfitidea点击:

Log4j Implicit String Formatting

javalogginglog4jstring-formatting

提问by Rossiar

I am using log4j v1.2.14for logging in my project and I am also using Java 7String.format()to put variables in my output. Currently I am writing

log4j v1.2.14用于登录我的项目,我还使用Java 7String.format()将变量放入我的输出中。目前我正在写

LOGGER.info(String.format("Your var is [%s] and you are [%s]", myVar, myVar1));

Is this really the best way to output strings? I feel that log4jshould have this implemented implicitly as below:

这真的是输出字符串的最佳方式吗?我觉得log4j应该隐式实现如下:

LOGGER.info("Your var is [%s] and you are [%s]", myVar, myVar1);

Have I missed something? Further, are there any Java logging frameworks that support this?

我错过了什么吗?此外,是否有任何 Java 日志框架支持这一点?

采纳答案by Yosef Weiner

slf4j's api provides "parameterized logging", which allows you to do exactly that, although with a slightly different syntax. The example there is:

slf4j的 api 提供了“参数化日志记录”,它允许您完全做到这一点,尽管语法略有不同。那里的例子是:

logger.debug("Value {} was inserted between {} and {}.", newVal, below, above);

For an implementation, you can use Logback which implements slf4j natively, or the slf4j bindings to connect with log4j or other loggers. The User Manualexplains that, along with a short example.

对于实现,您可以使用本地实现 slf4j 的 Logback,或者使用 slf4j 绑定来连接 log4j 或其他记录器。用户手册解释了这一点,以及一个简短的例子。

回答by ring bearer

Btw, In this scenario there is not much difference between using +to add your variables to the string and String.format- unless you really want to reuse the "Your var is..." in all your logs.

顺便说一句,在这种情况下,使用+将变量添加到字符串和String.format- 除非您真的想在所有日志中重用“您的 var 是...”之间没有太大区别。

slf4j lets you log as

slf4j 让你登录为

log.info("Your var is {} and you are {}", myVar, myVar1);

log.info("Your var is {} and you are {}", myVar, myVar1);

Note the use of {}instead of print formatters. Also this requires Java >= 1.5

注意使用{}而不是打印格式化程序。这也需要 Java >= 1.5

回答by Mathieu

Using String.format, +, or a string formatter other than the one provided by your logging system (log4jfor example) is considered as a bad practice. Usually, in the code there are lots of low level logs (debug, info) you don't want to see in production. If you use for example String.formatto format the string to log, then you will generate and format a new string which can be very long and consume resources, even if at the end nothing will be logged (for example if the log4jmin level is set to warning or error). By using the logger formatter system (like the one from log4j), you allow your logger to avoid the generation of the formatted string if it doesn't need to be logged.

使用String.format+或不同于日志系统提供的字符串格式化程序(log4j例如)被认为是一种不好的做法。通常,在代码中有很多您不想在生产中看到的低级日志(调试、信息)。如果您使用例如String.format格式化要记录的字符串,那么您将生成并格式化一个可能很长并消耗资源的新字符串,即使最后不会记录任何内容(例如,如果log4j最小级别设置为警告)或错误)。通过使用记录器格式化系统(如 中的log4j),您可以让记录器避免生成不需要记录的格式化字符串。

This may make a great difference in some cases.

在某些情况下,这可能会产生很大的不同。

回答by bobanahalf

Log4j supports internal formatting. I haven't found it documented anywhere, but I saw an example of it here:

Log4j 支持内部格式化。我没有在任何地方找到它的记录,但我在这里看到了一个例子:

https://logging.apache.org/log4j/2.x/manual/markers.html

https://logging.apache.org/log4j/2.x/manual/markers.html

I tried it out and it works! I'm on log4j 2.11.2.

我试过了,它有效!我在 log4j 2.11.2 上。

int i = 42;
String str1 = "the answer";
String str2 = "life, the universe, and everything";
console.info("{} is {} to {}", i, str1, str2);

Looking at the javadoc for Logger, I'd say it was introduced in Lo4j 2, and supports up to 10 parameters.

查看 Logger 的 javadoc,我会说它是在 Lo4j 2 中引入的,并且最多支持 10 个参数。

https://logging.apache.org/log4j/2.x/log4j-api/apidocs/org/apache/logging/log4j/Logger.html

https://logging.apache.org/log4j/2.x/log4j-api/apidocs/org/apache/logging/log4j/Logger.html