gpt4 book ai didi

java - 与单独使用 log4j 相比,将 slf4j 与 log4j 结合使用是否有任何性能开销?

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:01:02 24 4
gpt4 key购买 nike

我正在考虑将 java 1.4.2 项目从 log4j 迁移到 slf4j。这是参数化日志记录和代码清晰度的优势(如果 log.isdebug 则不需要......)这让我想到了这样的考虑。

转换为 slf4j 是否有性能开销?

上次我检查是否可以使用 logback,它对 jdk 有要求,要求它至少为 1.5,这就是我考虑 slf4j 和 log4j 的原因。

最佳答案

不,如果有的话,您应该从 slf4j 获得性能优势,因为(如果您使用参数化消息)那么 slf4j defers turning its parameters to strings until it has checked that the message won't be filtered out by log level :

There exists a very convenient alternative based on message formats. Assuming entry is an object, you can write:

Object entry = new SomeObject();
logger.debug("The entry is {}.", entry);

After evaluating whether to log or not, and only if the decision is affirmative, will the logger implementation format the message and replace the '{}' pair with the string value of entry. In other words, this form does not incur the cost of parameter construction in case the log statement is disabled.

The following two lines will yield the exact same output. However, the second form will outperform the first form by a factor of at least 30, in case of a disabled logging statement.

logger.debug("The new entry is "+entry+".");
logger.debug("The new entry is {}.", entry);

关于java - 与单独使用 log4j 相比,将 slf4j 与 log4j 结合使用是否有任何性能开销?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7094217/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com