Sub topic 5
Setting log levels in application properties
This chapter assumes you can write SLF4J log statements, know the five common levels (TRACE, DEBUG, INFO, WARN, ERROR), and have a Spring Boot application that reads application.properties. Spring Boot's default backend is Logback.
How the level setting works
Every logger has a name. For most classes, the name is the fully qualified class name, such as com.example.orders.OrderService. Logger names form a hierarchy based on the dots. A logger named com.example.orders is the parent of com.example.orders.OrderService.
A logger with no level of its own inherits the level of its nearest configured ancestor. The top of the hierarchy is the root logger. A statement is written only if its level is at or above the effective level of its logger.
The logging.level properties
Spring Boot reads properties that start with logging.level.. The part after the prefix is a logger name, or root. The value is a level.
logging.level.root=warn
logging.level.com.example.orders=debug
logging.level.org.springframework.web=infoThese three lines have the following effect:
- Every logger without a more specific setting logs WARN and ERROR only.
- Everything under
com.example.orderslogs DEBUG and above. - Spring MVC classes under
org.springframework.weblog INFO and above.
The most specific matching name wins. A class in com.example.orders uses debug, even though the root level is warn.
Valid values are trace, debug, info, warn, error, fatal, and off. Values are not case sensitive. Logback has no FATAL level and maps it to ERROR. The value off silences a logger completely.
The same settings in YAML look like this:
logging:
level:
root: warn
com.example.orders: debug
org.springframework.web: infoDefault level and quick switches
If you set nothing, the root level is INFO. You can also raise detail for the whole application when you start it, without editing any file:
java -jar app.jar --debugThe --debug flag, or debug=true in the properties file, makes selected core loggers (embedded container, Hibernate, Spring Boot itself) more verbose. It does not set the root level to DEBUG, so your own code stays at INFO. There is a matching --trace flag.
Seeing the hierarchy in action
The following class logs at every level. Which lines appear depends only on the configuration.
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
@Component
public class LevelDemo implements CommandLineRunner {
private static final Logger log = LoggerFactory.getLogger(LevelDemo.class);
@Override
public void run(String... args) {
log.trace("trace message");
log.debug("debug message");
log.info("info message");
log.warn("warn message");
log.error("error message");
}
}If LevelDemo is in com.example.orders and the properties above are active, you see debug, info, warn, and error lines. Move the class to com.example.other, and only warn and error appear.
Common errors
- Typos in the logger name. A misspelled package matches nothing, and Spring Boot does not warn you. Copy the package name from your IDE.
- Invalid level values. A value such as
verboseis not a valid level. Logback treats an unrecognized value as DEBUG, so the result can surprise you. - Expecting a lower level to show more than the code writes. Setting
traceon a class that has no trace statements prints nothing extra. - Setting root to debug. This floods the output with framework messages. Raise the level for the packages you are investigating instead.
- Relaxed binding with names. Logger names in property keys are case sensitive. Use
com.example.Ordersonly if the package really has that capitalization.
Exercise
Your properties contain logging.level.root=warn and logging.level.com.example=info. A class in com.example.billing calls log.debug(...) and log.info(...). Which call is written, and why?
Show answer
Only log.info(...) is written. The class has no setting of its own, so it inherits info from its nearest configured ancestor, com.example. DEBUG is below INFO, so it is dropped. The root level of warn is not used because a more specific ancestor is configured.