Log.info using log.isInfoEnabled

We use SLF4J

Based on recent team discussion

if(LOG.isDebugEnabled()){
  LOG.debug("hello " + a + " world" + b);
}

better than

LOG.debug("hello {} world {}", a, b);

because in the latter case, String hello {} world {}is created even if "debug" is not enabled. In other words, we always create strings, even if it is not required.

I like the latest version, as it greatly improves readability.

Can someone ask for input?

Hi,

Shardul.

EDIT

Let me put it another way.

What is the best approach OR Which one is most common?

Shardul.

+5
source share
2 answers

, "hello {} world {}". . , , int.

, .

- LOG.debug , , isDebugEnabled() . , , .

, 99% . , isDebugEnabled, - , , , :

if(LOG.isDebugEnabled()){
  LOG.debug("hello {} world {}", cheap, veryExpensive());
}

veryExpensive(), ehhem, , , . BTW veryExpensive() , , ...

+12

String . SLF4J "... , " , .

" ", . , , ( ) isXEnabled mehod.

+1

All Articles