Log4j隐式字符串格式化[英] Log4j Implicit String Formatting

本文是小编为大家收集整理的关于Log4j隐式字符串格式化的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

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

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

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

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

我错过了什么吗?此外,是否有Java Logging框架支持此?

推荐答案

slf4j 的API提供"

对于实现,您可以使用原生实现SLF4J的logback,或者使用SLF4J绑定来与Log4J或其他日志记录器连接. 用户手册解释了这一点,以及一个简短的示例.

其他推荐答案

使用String.format,+或除了记录系统提供的字符串格式(例如log4j)被视为不良练习.

通常,在代码中,您不想在生产中看到很多低级日志(调试,信息).如果例如String.format将字符串格式化以日志的格式示例如果将log4j min级别设置为警告或错误).

通过使用Logger Formatter系统(例如log4j的一个系统),您允许记录器避免使用格式的字符串生成,如果不需要记录.

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

其他推荐答案

log4j支持内部格式.我在任何地方都没有发现它,但是我在这里看到了一个例子:

https://logging.apache.org.org/log4j/2. X/Manual/Markers.html

我尝试了一下,它有效!我在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);

查看Logger的Javadoc,我说它是在LO4J 2中引入的,最多支持10个参数.

本文地址:https://www.itbaoku.cn/post/1574867.html

问题描述

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));

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);

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

推荐答案

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);

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.

其他推荐答案

Using String.format, +, or a string formatter other than the one provided by your logging system (log4j for 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.format to format the string to log, then you will allocate on the heap 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 log4j min 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.

This may make a great difference in some cases.

其他推荐答案

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

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

I tried it out and it works! I'm on 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.

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