0
0
Spring Bootframework~30 mins

Log levels (TRACE, DEBUG, INFO, WARN, ERROR) in Spring Boot - Mini Project: Build & Apply

Choose your learning style9 modes available
Log levels (TRACE, DEBUG, INFO, WARN, ERROR)
📖 Scenario: You are building a simple Spring Boot application that logs messages at different levels to help developers understand what is happening inside the app.
🎯 Goal: Create a Spring Boot application with a logger. Configure a log level variable. Use the logger to print messages at TRACE, DEBUG, INFO, WARN, and ERROR levels. Finally, set the log level in the application properties.
📋 What You'll Learn
Create a Spring Boot application class with a logger
Add a variable to hold the current log level
Use the logger to log messages at TRACE, DEBUG, INFO, WARN, and ERROR levels
Set the log level in application.properties to INFO
💡 Why This Matters
🌍 Real World
Logging helps developers see what the application is doing and find problems quickly by showing messages of different importance.
💼 Career
Understanding logging and log levels is essential for debugging and monitoring applications in software development jobs.
Progress0 / 4 steps
1
Create Spring Boot application with logger
Create a Spring Boot application class named LoggingDemoApplication. Inside it, create a private static final logger called logger using LoggerFactory.getLogger(LoggingDemoApplication.class).
Spring Boot
Need a hint?

Use LoggerFactory.getLogger(LoggingDemoApplication.class) to create the logger.

2
Add a log level variable
Inside the LoggingDemoApplication class, add a private static final String variable named LOG_LEVEL and set it to "INFO".
Spring Boot
Need a hint?

Declare LOG_LEVEL as a private static final String and assign it "INFO".

3
Log messages at different levels
Inside the main method, after SpringApplication.run(...), add logger calls to log messages at TRACE, DEBUG, INFO, WARN, and ERROR levels. Use logger.trace("Trace message"), logger.debug("Debug message"), logger.info("Info message"), logger.warn("Warn message"), and logger.error("Error message") respectively.
Spring Boot
Need a hint?

Use the logger variable to call trace, debug, info, warn, and error methods with messages.

4
Set log level in application.properties
Create a file named src/main/resources/application.properties. Inside it, add the line logging.level.root=INFO to set the root log level to INFO.
Spring Boot
Need a hint?

This file controls Spring Boot logging settings. Setting logging.level.root=INFO means only INFO and higher messages show.