Performance: SLF4J and Logback basics
MEDIUM IMPACT
Logging configuration affects application startup time and runtime responsiveness by controlling how much logging data is processed and written.
import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class MyService { private static final Logger logger = LoggerFactory.getLogger(MyService.class); public void process() { if (logger.isDebugEnabled()) { logger.debug("Processing data: {}", expensiveOperation()); } } private String expensiveOperation() { // heavy computation return "result"; } }
import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class MyService { private static final Logger logger = LoggerFactory.getLogger(MyService.class); public void process() { logger.debug("Processing data: " + expensiveOperation()); } private String expensiveOperation() { // heavy computation return "result"; } }
| Pattern | CPU Usage | I/O Load | Startup Impact | Verdict |
|---|---|---|---|---|
| Unconditional expensive logging | High (runs expensive code always) | High (many logs) | Medium (slower startup) | [X] Bad |
| Conditional logging with isDebugEnabled | Low (runs code only if needed) | Medium (logs only when debug) | Low (faster startup) | [OK] Good |
| Root level debug logging | Medium | High (large log files) | Medium | [!] OK |
| Root level info logging | Low | Low (smaller logs) | Low | [OK] Good |