2014-06-11 46 views
0

使用類似slf4j的優點之一是避免使用參數化日誌記錄進行字符串連接。但是,如果您有很長的日誌消息 ,您如何避免性能下降?避免字符串連接以獲取大型日誌消息

logger.debug("This is a very long message that prints two values." + 
    " However since the message is long, we still incur the performance hit" + 
    " of String concatenation when logging {} and {} ", value1, value2); 

是否有任何方法可以避免這種性能成本,而不使用難看的if塊來檢查日誌級別?

if (logger.isDebugEnabled()) { 
    logger.debug("This is a very long message that prints two values." + 
     " However since the message is long, we still incur the performance hit" + 
     " of String concatenation when logging {} and {}", value1, value2); 
} 

回答

4

沒有字符串連接。

編譯器將爲您創建一個長的字符串文字。

+0

即使有連接,在日誌記錄的情況下,它在大多數地區的表現並不是非常關鍵的影響 – Rogue

+2

@Rogue取決於您做了多少記錄。 –

+0

那麼這就是爲什麼我說「最」;) – Rogue

相關問題