0
0
Spring Bootframework~30 mins

Why logging matters in Spring Boot - See It in Action

Choose your learning style9 modes available
Why logging matters
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Run the app and look at the console. You should see the INFO and ERROR log messages with the exception details.