Performance: Logger creation in classes
This affects application startup time and memory usage due to logger instance creation in each class.
public class MyService { private static final Logger logger = LoggerFactory.getLogger(MyService.class); // reuse logger efficiently } // or use centralized logging configuration
public class MyService { private Logger logger = LoggerFactory.getLogger(MyService.class); // class code } // repeated in every class
| Pattern | Memory Usage | Startup Impact | Reuse Efficiency | Verdict |
|---|---|---|---|---|
| Static final logger per class | Low (one instance per class) | Minimal | High (reused efficiently) | [OK] Good |
| Dynamic logger creation per method call | High (many instances) | Noticeable delay | Low (new instances each time) | [X] Bad |
MyService?private static final Logger logger = LoggerFactory.getLogger(App.class);
public void run() {
logger.info("Start running");
logger.debug("Debugging info");
logger.error("An error occurred");
}OrderService:private Logger logger = LoggerFactory.getLogger(OrderService);
UserController that logs a warning message only when a user ID is missing. Which of the following code snippets correctly creates the logger and logs the warning message?Pass the test and this pattern moves unknown