📖 Scenario: You are building a simple Spring Boot application that needs to log messages for monitoring and debugging. You will set up SLF4J with Logback, the default logging framework in Spring Boot, to log messages at different levels.
🎯 Goal: Learn how to create a logger using SLF4J, configure Logback with a simple XML file, and log messages at INFO and ERROR levels in a Spring Boot application.
📋 What You'll Learn
Create a Spring Boot application class
Create a logger instance using SLF4J LoggerFactory
Configure Logback with a basic logback.xml file
Log messages at INFO and ERROR levels in the application
💡 Why This Matters
🌍 Real World
Logging is essential in real applications to track what the app is doing and find problems quickly.
💼 Career
Understanding SLF4J and Logback is important for Java developers working with Spring Boot to maintain and debug applications.
Progress0 / 4 steps
1
Create Spring Boot Application Class with Logger
Create a Spring Boot application class named LoggingDemoApplication in package com.example.logging. Inside it, create a private static final logger called logger using LoggerFactory.getLogger(LoggingDemoApplication.class).
Spring Boot
Hint
Use LoggerFactory.getLogger(LoggingDemoApplication.class) to create the logger.
2
Add Basic Logback Configuration File
Create a logback.xml file in src/main/resources folder. Add a configuration root element with a console appender named STDOUT that outputs logs to the console with pattern %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n. Set the root logger level to INFO and attach the STDOUT appender.
Spring Boot
Hint
Use a ConsoleAppender with the specified pattern and set root level to INFO.
3
Log INFO and ERROR Messages in main Method
Inside the main method of LoggingDemoApplication, after starting the application, add a log message at INFO level with text "Application started successfully" and a log message at ERROR level with text "An error occurred during startup".
Spring Boot
Hint
Use logger.info() and logger.error() with the exact messages.
4
Add Spring Boot Starter Logging Dependency
In your pom.xml file, ensure you have the dependency spring-boot-starter-logging included. Add the following dependency inside <dependencies>: <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-logging</artifactId></dependency>
Spring Boot
Hint
This dependency is included by default in Spring Boot starter projects but add it explicitly if missing.
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
Step 1: Understand SLF4J's role
SLF4J acts as a simple facade or interface for various logging frameworks, allowing consistent logging calls.
Step 2: Differentiate from log storage
SLF4J itself does not handle storing or formatting logs; that is done by the logging backend like Logback.
Final Answer:
To provide a simple and consistent logging interface -> Option A
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
Step 1: Recall SLF4J logger creation syntax
The standard way is to use LoggerFactory.getLogger with the class name as parameter.
Step 2: Check each option
private static final Logger logger = LoggerFactory.getLogger(MyClass.class); matches the correct syntax; others use incorrect methods or constructors.
Final Answer:
private static final Logger logger = LoggerFactory.getLogger(MyClass.class); -> Option C
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
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.
Step 2: Check logger creation and SLF4J support
The logger is created correctly and SLF4J supports debug messages, so those are not the cause.
Final Answer:
The log level is set higher than DEBUG in Logback configuration -> Option B
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?
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.
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.
Final Answer:
The configuration with ERROR LevelFilter on FILE appender -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.