0
0
Spring Bootframework~10 mins

Logger creation in classes in Spring Boot - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Logger creation in classes
Start Class
Declare Logger
Initialize Logger with LoggerFactory
Use Logger in Methods
Log Messages at Different Levels
End Class
This flow shows how a logger is declared, initialized, and used inside a Spring Boot class to log messages.
Execution Sample
Spring Boot
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

  public void doWork() {
    logger.info("Work started");
  }
}
This code creates a logger in a Spring Boot class and logs an info message inside a method.
Execution Table
StepActionEvaluationResult
1Class MyService loadedClass loadedReady to declare logger
2Declare logger variableprivate static final Logger loggerLogger variable declared but not initialized
3Initialize logger with LoggerFactory.getLogger(MyService.class)LoggerFactory creates logger instanceLogger instance assigned to logger variable
4Call doWork() methodMethod invokedInside method body
5Execute logger.info("Work started")Log message at INFO levelMessage sent to logging system
6Method doWork() endsNo return valueMethod completes
7Class usage endsNo further actionsProgram continues or ends
💡 Logging completes after method execution; no more code to run in this example.
Variable Tracker
VariableStartAfter InitializationAfter Method CallFinal
loggernullLogger instance for MyServiceLogger instance for MyServiceLogger instance for MyService
Key Moments - 3 Insights
Why do we use LoggerFactory.getLogger(MyService.class) instead of creating Logger directly?
LoggerFactory.getLogger(MyService.class) creates a logger tied to the class name, which helps identify log messages' source. This is shown in execution_table step 3 where the logger instance is created.
Why is the logger declared as static and final?
Declaring logger as static means one shared instance per class, and final means it cannot be reassigned. This is important for efficiency and consistency, as seen in execution_table step 2.
What happens when logger.info() is called inside the method?
The log message is sent to the logging system at INFO level, which can be seen in execution_table step 5 where the message is processed.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the state of the logger variable after step 3?
AIt holds a Logger instance for MyService
BIt holds a String message
CIt is null
DIt is uninitialized
💡 Hint
Check the 'Result' column in step 3 of the execution_table.
At which step does the log message "Work started" get sent to the logging system?
AStep 3
BStep 5
CStep 2
DStep 6
💡 Hint
Look for the step where logger.info() is executed in the execution_table.
If the logger was not declared static, what would change in the variable_tracker?
ALogger would remain null
BLogger would be shared across all classes
CLogger would be initialized multiple times per instance
DLogger would be a local variable
💡 Hint
Consider how static affects variable initialization shown in variable_tracker.
Concept Snapshot
Logger creation in Spring Boot classes:
- Declare logger as private static final
- Initialize with LoggerFactory.getLogger(ClassName.class)
- Use logger methods (info, debug, error) inside class methods
- Static ensures one logger per class
- Logger helps track runtime events clearly
Full Transcript
In Spring Boot, to create a logger inside a class, you declare a private static final Logger variable. You initialize it using LoggerFactory.getLogger with the class name. This ties the logger to the class, making log messages easy to identify. Inside your methods, you call logger methods like info() to send messages to the logging system. The static keyword means only one logger instance exists per class, improving efficiency. This process helps track what your application is doing while it runs.