簡體   English   中英

Log4J2:替換參數無法正常工作

[英]Log4J2: Substituting Parameters does not work properly

我正在使用Log4J 2.8.2。

手冊( https://logging.apache.org/log4j/2.0/manual/api.html )表示,使用占位符可以更好地替換參數:

那意味着

logger.debug("Logging in user {} with birthday {}", user.getName(), user.getBirthdayCalendar());

勝過

if (logger.isDebugEnabled()) {
    logger.debug("Logging in user " + user.getName() + " with birthday " + user.getBirthdayCalendar());
}

因為:

日志記錄級別將僅被檢查一次,並且只有在啟用調試日志記錄時才會進行字符串構造。

所以可以說我有以下兩種方法:

public static String getSentence() {
        System.out.println("Sentence Invoked!");
        return "{} im the Best!";
    }

    public static String expensiveOperation() {
        System.out.println("Expensive Invoked!");
        return "John Doe";
    }

現在,rootLogger的級別設置為INFO 如果我以以下方式登錄:

LOGGER.debug(getSentence(), expensiveOperation());

我得到以下輸出:

Sentence Invoked!
Expensive Invoked!

這意味着將調用這兩種方法,盡管由於LOGGER的級別為DEBUG且rootLoggers的級別為INFO ,所以沒有日志記錄發生。

我本來期望是,無論是方法getSentence()expensiveOperation()將被調用。 我做錯什么了嗎?

根據設置的日志級別過濾日志(Handler將僅接收過濾后的日志)。 但是jvm確實執行每個日志。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM