📖 Scenario: You are building a simple Spring Boot application that processes user requests. To understand what happens inside your app and to find problems quickly, you need to add logging. Logging is like keeping a diary of your app's actions and errors.
🎯 Goal: Learn how to add basic logging to a Spring Boot application and see how logs help track app behavior and errors.
📋 What You'll Learn
Create a Spring Boot application class
Add a logger instance using LoggerFactory
Write log messages at different levels (INFO, ERROR)
Run the app and observe the log output
💡 Why This Matters
🌍 Real World
In real applications, logging helps developers and operators understand app behavior and quickly fix issues.
💼 Career
Knowing how to add and use logging is a key skill for developers and DevOps engineers to maintain reliable software.
Progress0 / 4 steps
1
Create a Spring Boot application class
Create a class called LoggingDemoApplication with the @SpringBootApplication annotation and a main method that runs SpringApplication.run(LoggingDemoApplication.class, args).
Spring Boot
Hint
Use @SpringBootApplication on the class and write a main method that calls SpringApplication.run.
2
Add a logger instance
Inside the LoggingDemoApplication class, add a private static final logger variable called logger using LoggerFactory.getLogger(LoggingDemoApplication.class). Import org.slf4j.Logger and org.slf4j.LoggerFactory.
Spring Boot
Hint
Use LoggerFactory.getLogger(LoggingDemoApplication.class) to create the logger.
3
Write log messages at INFO and ERROR levels
Inside the main method, add a log message using logger.info("Application started"). Then add a try-catch block that throws and catches an exception, and inside the catch block log the error with logger.error("An error occurred", e).
Spring Boot
Hint
Use logger.info for a start message and a try-catch block to log an error with logger.error.
4
Run the app and observe the log output
Run the LoggingDemoApplication class. Observe the console output showing the INFO message "Application started" and the ERROR message with the exception stack trace.
Spring Boot
Hint
Run the app and look at the console. You should see the INFO and ERROR log messages with the exception details.
Practice
(1/5)
1. Why is logging important in a Spring Boot application?
easy
A. It improves the speed of the application.
B. It automatically fixes bugs in the code.
C. It helps track what the application is doing and find problems.
D. It replaces the need for testing.
Solution
Step 1: Understand the purpose of logging
Logging records events and actions in the application, showing what is happening internally.
Step 2: Identify the benefit of logging
It helps developers find and fix problems by providing detailed information about the app's behavior.
Final Answer:
It helps track what the application is doing and find problems. -> Option C
Quick Check:
Logging shows app activity [OK]
Hint: Logging tracks app actions and errors clearly [OK]
Common Mistakes:
Thinking logging fixes bugs automatically
Confusing logging with performance improvement
Believing logging replaces testing
2. Which of the following is the correct way to log an info message in Spring Boot using SLF4J?
easy
A. logger.info("Application started successfully");
B. logger.logInfo("Application started successfully");
C. logger.printInfo("Application started successfully");
D. logger.writeInfo("Application started successfully");
Solution
Step 1: Recall SLF4J logging syntax
The SLF4J logger uses methods like info(), debug(), error() to log messages at different levels.
Step 2: Identify the correct method name
The correct method to log an info message is info(), so logger.info("message") is correct.
Final Answer:
logger.info("Application started successfully"); -> Option A
Quick Check:
SLF4J info method = info() [OK]
Hint: Use logger.info() for info messages in Spring Boot [OK]
Common Mistakes:
Using incorrect method names like logInfo or printInfo
Confusing logging methods with print statements
Missing parentheses after method name
3. Given the following Spring Boot code snippet, what will be the output if the log level is set to WARN?
When log level is set to WARN, only messages with level WARN and ERROR (higher severity) are shown.
Step 2: Identify which messages appear
Info messages are below WARN level, so they are ignored. Warning and error messages are shown.
Final Answer:
Warning message, Error message -> Option D
Quick Check:
Log level WARN shows WARN and ERROR [OK]
Hint: Log level filters out lower severity messages [OK]
Common Mistakes:
Expecting info messages to appear at WARN level
Confusing log levels order
Assuming all messages always print
4. You see no logs appearing in your Spring Boot console even though you added logger.info("Starting app");. What is the most likely cause?
medium
A. The log level is set higher than INFO, so info messages are hidden.
B. The logger.info() method is misspelled.
C. Spring Boot does not support logging by default.
D. The application has no internet connection.
Solution
Step 1: Check log level configuration
If the log level is set to WARN or ERROR, info messages will not show in the console.
Step 2: Rule out other causes
Logger method spelling is correct, Spring Boot supports logging by default, and internet connection does not affect logging.
Final Answer:
The log level is set higher than INFO, so info messages are hidden. -> Option A
Quick Check:
Log level hides lower severity logs [OK]
Hint: Check log level if logs don't appear [OK]
Common Mistakes:
Assuming logging needs extra setup always
Blaming internet connection for logging issues
Ignoring log level settings
5. You want to log an error with a message and the exception stack trace in Spring Boot. Which code snippet correctly does this?
hard
A. logger.warn("Error occurred", exception);
B. logger.error("Error occurred", exception);
C. logger.info("Error occurred" + exception);
D. logger.error(exception);
Solution
Step 1: Understand logging exceptions
To log an error with stack trace, use logger.error(message, throwable) method.
Step 2: Identify correct usage
logger.error("Error occurred", exception); passes both a message and the exception object, so it logs the message and stack trace properly.
Step 3: Rule out other options
Other options are incorrect: logging only the exception misses the message, using warn level is less severe than error, using info level and string concatenation does not properly log the stack trace.
Final Answer:
logger.error("Error occurred", exception); -> Option B
Quick Check:
Use logger.error(message, exception) for errors [OK]
Hint: Use logger.error(msg, exception) to log errors with stack trace [OK]
Common Mistakes:
Logging exceptions as strings instead of passing throwable
Using wrong log level for errors
Omitting the error message
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.