Performance: SLF4J and Logback basics
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 |
logger.info("User {} logged in", username);username is "Alice"?Logger logger = LoggerFactory.getLogger(MyClass.class);
logger.debug("Debug message");errors.log and all logs to the console. Which Logback configuration snippet correctly achieves this?ch.qos.logback.classic.filter.LevelFilter with <level>ERROR</level>, <onMatch>ACCEPT</onMatch>, <onMismatch>DENY</onMismatch>. This ensures only ERROR logs go to errors.log.<root level="DEBUG"> attaches both appenders, so all logs (DEBUG and above) go to console.Pass the test and this pattern moves unknown