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.
Log levels (TRACE, DEBUG, INFO, WARN, ERROR) in 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.
logger.trace("Starting detailed trace of method execution");logger.debug("User input received: " + userInput);logger.info("Application started successfully");logger.warn("Disk space is running low");logger.error("Failed to connect to database", exception);This Spring Boot app logs messages at all levels when it starts. You can see how each level is used for different importance.
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"); } }
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.
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.
Practice
Solution
Step 1: Understand log level hierarchy
TRACE is the most detailed log level, showing fine-grained events.Step 2: Compare with other levels
INFO, WARN, and ERROR show less detail and focus on general info or problems.Final Answer:
TRACE -> Option DQuick Check:
Most detailed log = TRACE [OK]
- Confusing INFO with TRACE as most detailed
- Thinking ERROR shows all details
- Assuming WARN is more detailed than DEBUG
application.properties for Spring Boot?Solution
Step 1: Recall Spring Boot logging property format
The correct property islogging.level.rootto set root log level.Step 2: Check option syntax
logging.level.root=WARN uses correct key and uppercase level, matching Spring Boot conventions.Final Answer:
logging.level.root=WARN -> Option AQuick Check:
Correct property key = logging.level.root [OK]
- Using log.level instead of logging.level.root
- Incorrect property key like logging.root.level
- Using lowercase level names incorrectly
logger.debug("Debug message");
logger.info("Info message");
logger.error("Error message");If the log level is set to INFO, which messages will appear in the logs?
Solution
Step 1: Understand log level filtering
Setting level to INFO shows logs at INFO and higher severity (WARN, ERROR).Step 2: Check which messages qualify
DEBUG is lower than INFO, so it is hidden; INFO and ERROR show.Final Answer:
Info message, Error message -> Option AQuick Check:
INFO level shows INFO and ERROR logs [OK]
- Assuming DEBUG logs show at INFO level
- Thinking ERROR logs are hidden at INFO level
- Confusing INFO with DEBUG level
logging.level.com.example=DEBUG in application.properties, but DEBUG messages from com.example.service do not appear. What is the most likely cause?Solution
Step 1: Check property correctness
If the package name is misspelled, the setting won't apply to the intended classes.Step 2: Evaluate other options
DEBUG is supported; root level does not block specific package settings; restart is usually required but not the main cause.Final Answer:
The package name is misspelled in the property -> Option CQuick Check:
Correct package name is critical for log level settings [OK]
- Assuming root level overrides package level always
- Believing DEBUG is unsupported
- Ignoring typos in package names
com.example.repository package but keep the rest of the application logging at WARN level. Which application.properties configuration achieves this?Solution
Step 1: Set global log level to WARN
Setlogging.level.root=WARNto limit logs globally to warnings and errors.Step 2: Override package log level to TRACE
Setlogging.level.com.example.repository=TRACEto get detailed logs only for that package.Final Answer:
logging.level.root=WARN logging.level.com.example.repository=TRACE -> Option BQuick Check:
Global WARN + package TRACE = detailed logs only there [OK]
- Setting root level too low, flooding logs
- Overriding package with higher level than root
- Confusing package and root property keys
Ready to prove it?
Pass the test and this pattern moves unknown
