有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java使用slf4j和log4j与单独使用log4j相比,是否存在性能开销?

我正在考虑将Java1.4.2项目从log4j迁移到slf4j。这是参数化日志记录和代码清晰性的优点(如果log.isdebug..,则无需使用)这正吸引着我去考虑这些问题

转换为slf4j是否存在性能开销

上次我检查是否可以使用logback时,它在jdk上有一个要求,要求它至少达到1.5,这就是我考虑使用slf4j和log4j的原因


共 (1) 个答案

  1. # 1 楼答案

    不,如果有的话,您应该从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);