Unable to understand why to use parameterized logging

Viewed 835

I was reading upon parameterized logging in Java, it is said to use parameterized logging instead of concatenation as it performs lazy evaluation.

So instead of

logger.debug("My text is" + x);

use:

logger.debug("My text is {}", x);

If the trace level is set to only info logs, why does the concatenation happen in the first scenario?

Also if I have a time consuming function in log, it is said to use:

logger.debug("My text is {}", () -> compute());

instead of

logger.debug("My text is {}", compute());

In this case, why is the lambda method considered a better approach. Won't compute also be called lazily like in the string concatenation case?

3 Answers
Related