0
0
Spring Bootframework~20 mins

Logger creation in classes in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Logger Mastery in Spring Boot
Get all challenges correct to earn this badge!
Test your skills under time pressure!
📝 Syntax
intermediate
2:00remaining
Correct Logger Declaration in a Spring Boot Class
Which option correctly declares a logger in a Spring Boot class using SLF4J?
Spring Boot
public class MyService {
    // Logger declaration here
}
Aprivate Logger logger = LoggerFactory.getLogger();
Bprivate static final Logger logger = LoggerFactory.getLogger(MyService.class);
Cpublic static Logger logger = new Logger(MyService.class);
DLogger logger = LoggerFactory.getLogger("MyService");
Attempts:
2 left
💡 Hint
Remember to use LoggerFactory and pass the class type for proper logger creation.
component_behavior
intermediate
2:00remaining
Logger Output Behavior in Spring Boot
Given a Spring Boot class with a logger declared as in option A of the previous question, what will be the output when calling logger.info("Hello World") if the logging level is set to WARN?
Spring Boot
private static final Logger logger = LoggerFactory.getLogger(MyService.class);

public void greet() {
    logger.info("Hello World");
}
AAn error will occur because the logger is not initialized.
BThe message will be logged but only in the debug log file.
CThe message 'Hello World' will be printed to the console.
DNo output will be shown because INFO is lower than WARN level.
Attempts:
2 left
💡 Hint
Check the logging level hierarchy: ERROR > WARN > INFO > DEBUG.
🔧 Debug
advanced
2:00remaining
Identify the Logger Initialization Error
What error will occur when running this Spring Boot class code snippet?
Spring Boot
public class UserService {
    private static Logger logger = LoggerFactory.getLogger();

    public void process() {
        logger.info("Processing user");
    }
}
ANo error, logs 'Processing user' correctly.
BNullPointerException at runtime because logger is null.
CCompilation error: getLogger() requires a Class or String argument.
DIllegalArgumentException because logger name is empty.
Attempts:
2 left
💡 Hint
Check the LoggerFactory.getLogger method signature.
state_output
advanced
2:00remaining
Logger Instance Identity in Multiple Class Instances
If you create two instances of a Spring Boot class with a static logger declared as private static final Logger logger = LoggerFactory.getLogger(MyClass.class);, how many distinct logger instances exist in memory?
Spring Boot
public class MyClass {
    private static final Logger logger = LoggerFactory.getLogger(MyClass.class);

    public void log() {
        logger.info("Logging");
    }
}

MyClass a = new MyClass();
MyClass b = new MyClass();
AOne single logger instance shared by both class instances.
BNo logger instances until log() is called.
CTwo separate logger instances, one per class instance.
DA new logger instance is created each time logger.info is called.
Attempts:
2 left
💡 Hint
Static fields belong to the class, not to instances.
🧠 Conceptual
expert
3:00remaining
Best Practice for Logger Declaration in Spring Boot
Which statement best explains why logger fields are declared as private static final in Spring Boot classes?
ATo ensure a single shared logger per class, improve performance, and prevent modification.
BTo allow each instance to have its own logger with different configurations.
CTo enable logger injection by Spring's dependency injection container.
DTo make the logger accessible from other classes without creating an instance.
Attempts:
2 left
💡 Hint
Think about memory usage and immutability of logger objects.