How does Log4j implement lazy argument evaluation?

十年热恋 提交于 2019-12-04 00:09:26

I guess what Log4j means, is that with the curly brackets, they avoid constructing a String when its not necessary (e.g. the Level is not Debug):

With

logger.debug("Entry number: " + i + " is " + String.valueOf(entry[i]));

The String is always calculated even when its not logged.

with

logger.debug("Entry number: {} is {}", i, entry[i]);

Log4j can check the Log-Level first and then decide if its worth to construct the Message-String.

Log4j uses a internal Class (org.slf4j.helpers.MessageFormatter) that replaces every {} with the arguments provided. You can have a look at the Sourcecode in org.slf4j.impl.Log4jLoggerAdapter.debug(String, Object[])

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!