Log levels help you control what messages your application writes to logs. They let you see detailed info when debugging and less noise in normal use.
0
0
Log levels (TRACE, DEBUG, INFO, WARN, ERROR) in Spring Boot
Introduction
When you want to see very detailed messages to find tricky bugs (TRACE).
When you want to check detailed info during development (DEBUG).
When you want to log general information about app progress (INFO).
When you want to warn about possible problems that don't stop the app (WARN).
When you want to log serious errors that need fixing (ERROR).
Syntax
Spring Boot
logger.trace("message"); logger.debug("message"); logger.info("message"); logger.warn("message"); logger.error("message");
Use the logger object from a logging framework like SLF4J or Logback.
Choose the level based on how important or detailed the message is.
Examples
TRACE level logs very detailed info, useful for deep debugging.
Spring Boot
logger.trace("Starting detailed trace of method execution");DEBUG level logs useful info for developers during development.
Spring Boot
logger.debug("User input received: " + userInput);INFO level logs general app events that are normal but important.
Spring Boot
logger.info("Application started successfully");WARN level logs potential problems that should be checked.
Spring Boot
logger.warn("Disk space is running low");ERROR level logs serious problems that need immediate attention.
Spring Boot
logger.error("Failed to connect to database", exception);Sample Program
This Spring Boot app logs messages at all levels when it starts. You can see how each level is used for different importance.
Spring Boot
package com.example.demo; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class DemoApplication implements CommandLineRunner { private static final Logger logger = LoggerFactory.getLogger(DemoApplication.class); public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } @Override public void run(String... args) { logger.trace("Trace: Starting application run method"); logger.debug("Debug: Initializing variables"); logger.info("Info: Application is running"); logger.warn("Warn: Low memory warning"); logger.error("Error: Unable to load configuration file"); } }
OutputSuccess
Important Notes
Log output depends on your logging configuration. By default, TRACE and DEBUG may not show unless enabled.
Use INFO or higher levels in production to avoid too much log noise.
Logging exceptions with error level helps find problems quickly.
Summary
Log levels control how much detail you see in logs.
Use TRACE and DEBUG for development and troubleshooting.
Use INFO, WARN, and ERROR for normal and problem situations.