Bird
Raised Fist0
Spring Bootframework~20 mins

SLF4J and Logback basics in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
SLF4J and Logback Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What is the output of this SLF4J logging code snippet?
Consider this Spring Boot component using SLF4J for logging:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MyService {
private static final Logger logger = LoggerFactory.getLogger(MyService.class);

public void process() {
logger.info("Process started");
logger.debug("Debugging process");
logger.error("Error occurred");
}
}

If the logging level is set to INFO, which messages will appear in the console?
Spring Boot
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MyService {
  private static final Logger logger = LoggerFactory.getLogger(MyService.class);

  public void process() {
    logger.info("Process started");
    logger.debug("Debugging process");
    logger.error("Error occurred");
  }
}
AAll three messages will appear: 'Process started', 'Debugging process', and 'Error occurred'.
BNo messages will appear because INFO level disables all logging.
COnly 'Debugging process' and 'Error occurred' messages will appear.
DOnly 'Process started' and 'Error occurred' messages will appear.
Attempts:
2 left
💡 Hint
Remember that INFO level shows messages at INFO and higher severity.
📝 Syntax
intermediate
2:00remaining
Which Logback configuration snippet correctly sets the root logger to WARN level?
Given these Logback XML configuration options, which one correctly sets the root logger level to WARN?
Spring Boot
<configuration>
  <!-- options below -->
</configuration>
A
&lt;root level="warn"&gt;
  &lt;appender-ref ref="STDOUT" /&gt;
&lt;/root&gt;
B
&lt;root level="Warn"&gt;
  &lt;appender-ref ref="STDOUT" /&gt;
&lt;/root&gt;
C
&lt;root level="WARN"&gt;
  &lt;appender-ref ref="STDOUT" /&gt;
&lt;/root&gt;
D
&lt;root level="warning"&gt;
  &lt;appender-ref ref="STDOUT" /&gt;
&lt;/root&gt;
Attempts:
2 left
💡 Hint
Logback levels are case-sensitive and must be uppercase.
🔧 Debug
advanced
2:00remaining
Why does this SLF4J logging statement cause an error?
Examine this code snippet:
logger.info("User {} logged in at {}", userName);

What error will this cause and why?
Spring Boot
logger.info("User {} logged in at {}", userName);
ASyntax error because of missing semicolon.
BRuntime error due to missing argument for second placeholder.
CNo error; logs message with 'null' for second placeholder.
DCompile-time error due to wrong method signature.
Attempts:
2 left
💡 Hint
Count the placeholders and arguments carefully.
❓ state_output
advanced
2:00remaining
What is the effect of setting additivity to false in Logback logger configuration?
Given this Logback snippet:
<logger name="com.example" level="DEBUG" additivity="false">
  <appender-ref ref="FILE" />
</logger>

What happens to log messages from 'com.example' package?
Spring Boot
<logger name="com.example" level="DEBUG" additivity="false">
  <appender-ref ref="FILE" />
</logger>
AMessages are logged only to FILE appender, not to parent appenders.
BMessages are logged only to parent appenders, ignoring FILE appender.
CMessages are ignored because additivity=false disables logging.
DMessages are logged to FILE and all parent appenders.
Attempts:
2 left
💡 Hint
Additivity controls if logs bubble up to parent loggers.
🧠 Conceptual
expert
3:00remaining
Which statement best describes the relationship between SLF4J and Logback in a Spring Boot application?
Choose the most accurate description of how SLF4J and Logback work together in Spring Boot logging.
ASLF4J is a logging facade that delegates logging calls to Logback, which is the actual logging implementation.
BLogback is a facade that delegates logging calls to SLF4J, which handles the actual logging.
CSLF4J and Logback are independent logging frameworks that cannot be used together.
DSLF4J replaces Logback entirely and provides all logging features by itself.
Attempts:
2 left
💡 Hint
Think about the role of a facade versus an implementation.

Practice

(1/5)
1. What is the main purpose of SLF4J in a Spring Boot application?
easy
A. To provide a simple and consistent logging interface
B. To store log files on disk
C. To send logs to a remote server
D. To format log messages with colors

Solution

  1. Step 1: Understand SLF4J's role

    SLF4J acts as a simple facade or interface for various logging frameworks, allowing consistent logging calls.
  2. Step 2: Differentiate from log storage

    SLF4J itself does not handle storing or formatting logs; that is done by the logging backend like Logback.
  3. Final Answer:

    To provide a simple and consistent logging interface -> Option A
  4. Quick Check:

    SLF4J = Simple logging interface [OK]
Hint: SLF4J is the logging interface, not the storage tool [OK]
Common Mistakes:
  • Confusing SLF4J with the logging backend
  • Thinking SLF4J formats or stores logs
  • Assuming SLF4J sends logs remotely
2. Which of the following is the correct way to create a logger using SLF4J in a Spring Boot class?
easy
A. private Logger logger = LoggerFactory.createLogger();
B. Logger logger = new Logger();
C. private static final Logger logger = LoggerFactory.getLogger(MyClass.class);
D. Logger logger = Logger.getLogger();

Solution

  1. Step 1: Recall SLF4J logger creation syntax

    The standard way is to use LoggerFactory.getLogger with the class name as parameter.
  2. Step 2: Check each option

    private static final Logger logger = LoggerFactory.getLogger(MyClass.class); matches the correct syntax; others use incorrect methods or constructors.
  3. Final Answer:

    private static final Logger logger = LoggerFactory.getLogger(MyClass.class); -> Option C
  4. Quick Check:

    Logger creation = LoggerFactory.getLogger(Class) [OK]
Hint: Use LoggerFactory.getLogger with your class name [OK]
Common Mistakes:
  • Trying to instantiate Logger with new keyword
  • Using Logger.getLogger() without parameters
  • Missing static or final keywords
3. Given the following code snippet in a Spring Boot app using SLF4J and Logback:
logger.info("User {} logged in", username);

What will be the output if username is "Alice"?
medium
A. User {} logged in Alice
B. User {} logged in
C. User username logged in
D. User Alice logged in

Solution

  1. Step 1: Understand SLF4J placeholder usage

    The curly braces {} act as placeholders replaced by the following arguments in order.
  2. Step 2: Substitute username value

    Since username is "Alice", it replaces the {} placeholder in the message.
  3. Final Answer:

    User Alice logged in -> Option D
  4. Quick Check:

    Placeholders {} replaced by arguments [OK]
Hint: {} placeholders get replaced by arguments in order [OK]
Common Mistakes:
  • Printing the placeholder {} literally
  • Printing variable name instead of value
  • Appending variable after the message
4. You have this code snippet:
Logger logger = LoggerFactory.getLogger(MyClass.class);
logger.debug("Debug message");

But you never see the debug message in your console logs. What is the most likely reason?
medium
A. The logger was not created correctly
B. The log level is set higher than DEBUG in Logback configuration
C. SLF4J does not support debug messages
D. You must call logger.info() instead of logger.debug()

Solution

  1. Step 1: Understand log levels and filtering

    Logback filters messages below the configured log level, so DEBUG messages may be hidden if level is INFO or higher.
  2. Step 2: Check logger creation and SLF4J support

    The logger is created correctly and SLF4J supports debug messages, so those are not the cause.
  3. Final Answer:

    The log level is set higher than DEBUG in Logback configuration -> Option B
  4. Quick Check:

    Log level filtering hides DEBUG if set higher [OK]
Hint: Check Logback log level to see debug messages [OK]
Common Mistakes:
  • Assuming logger creation failed
  • Thinking SLF4J lacks debug support
  • Believing debug logs always show by default
5. You want to configure Logback to write error logs to a file named errors.log and all logs to the console. Which Logback configuration snippet correctly achieves this?
hard
A. errors.log %d %-5level %logger{36} - %msg%n ERROR ACCEPT DENY %d %-5level %logger{36} - %msg%n
B. errors.log %d %-5level %logger{36} - %msg%n %d %-5level %logger{36} - %msg%n
C. errors.log %d %-5level %logger{36} - %msg%n INFO ACCEPT DENY %d %-5level %logger{36} - %msg%n
D. errors.log %d %-5level %logger{36} - %msg%n %d %-5level %logger{36} - %msg%n

Solution

  1. Step 1: Locate the ERROR LevelFilter on FILE appender

    The FILE appender uses 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.
  2. Step 2: Confirm console appender and root logger

    The CONSOLE appender has no filter. The <root level="DEBUG"> attaches both appenders, so all logs (DEBUG and above) go to console.
  3. Final Answer:

    The configuration with ERROR LevelFilter on FILE appender -> Option A
  4. Quick Check:

    File appender filters ERROR, console shows all [OK]
Hint: Use LevelFilter on file appender for ERROR logs only [OK]
Common Mistakes:
  • Not filtering file appender logs by level
  • Setting root level too high hiding logs
  • Missing console appender or not attaching it
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard