Bird
0
0

You want to log a message only when the log level is DEBUG or lower, but avoid expensive string concatenation if logging is disabled. Which SLF4J pattern should you use?

hard📝 Application Q8 of 15
Spring Boot - Logging
You want to log a message only when the log level is DEBUG or lower, but avoid expensive string concatenation if logging is disabled. Which SLF4J pattern should you use?
AUse logger.info("Message: {}", expensiveMethod());
BUse logger.debug("Message: " + expensiveMethod());
CUse System.out.println(expensiveMethod());
DUse logger.debug("Message: {}", expensiveMethod());
Step-by-Step Solution
Solution:
  1. Step 1: Understand SLF4J parameterized logging

    Using placeholders with logger.debug delays expensiveMethod() evaluation until needed.
  2. Step 2: Compare with string concatenation

    Concatenation always evaluates expensiveMethod(), wasting resources if debug is off.
  3. Final Answer:

    Use logger.debug("Message: {}", expensiveMethod()); -> Option D
  4. Quick Check:

    Use placeholders to avoid unnecessary computation = C [OK]
Quick Trick: Use {} placeholders to delay expensive calls [OK]
Common Mistakes:
  • Concatenating strings inside logger calls
  • Using wrong log level
  • Using System.out.println instead of logger

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes