0
0
Spring Bootframework~15 mins

Logger creation in classes in Spring Boot - Mini Project: Build & Apply

Choose your learning style9 modes available
Logger Creation in Spring Boot Classes
📖 Scenario: You are building a Spring Boot application that needs to record messages for debugging and monitoring. Logging helps developers see what the app is doing behind the scenes, like a diary for the program.
🎯 Goal: Create a logger inside a Spring Boot class using the recommended pattern. This logger will allow the class to write messages to the console or log files.
📋 What You'll Learn
Create a Spring Boot class named MyService
Add a private static final logger variable named logger using LoggerFactory.getLogger()
Use the MyService.class as the argument to getLogger()
Write a method logMessage() that logs an info-level message "Logging is set up!"
💡 Why This Matters
🌍 Real World
Logging is essential in real applications to understand app behavior and troubleshoot issues.
💼 Career
Knowing how to set up and use loggers is a basic skill for Java developers working with Spring Boot.
Progress0 / 4 steps
1
Create the Spring Boot class
Create a public class named MyService in Java.
Spring Boot
Need a hint?

Use public class MyService { } to define the class.

2
Add the logger variable
Inside the MyService class, add a private static final logger variable named logger initialized with LoggerFactory.getLogger(MyService.class). Import org.slf4j.Logger and org.slf4j.LoggerFactory.
Spring Boot
Need a hint?

Use private static final Logger logger = LoggerFactory.getLogger(MyService.class); inside the class.

3
Create the logMessage method
Add a public method named logMessage inside MyService that calls logger.info("Logging is set up!").
Spring Boot
Need a hint?

Define public void logMessage() { logger.info("Logging is set up!"); } inside the class.

4
Complete the class with imports and method
Ensure the MyService class includes the imports for Logger and LoggerFactory, the private static final logger variable, and the logMessage() method.
Spring Boot
Need a hint?

Check that all parts are present: imports, logger variable, and method.