Sub topic 5
Setting log levels in application properties
This chapter assumes you can run a Spring Boot application, edit application.properties or application.yml, and write basic SLF4J log statements. It covers how Spring Boot decides which log statements are printed, and how to control that with properties.
How a level decides what is printed
Every logger has an effective level. A log statement is printed only if its level is at or above the logger's effective level. The levels, from most to least verbose, are TRACE, DEBUG, INFO, WARN, and ERROR. Spring Boot also accepts FATAL and OFF. Logback has no FATAL level, so Boot maps FATAL to ERROR.
Spring Boot's default level is INFO for the root logger. With no configuration, log.debug(...) calls are dropped and log.info(...), log.warn(...) and log.error(...) calls are printed.
The logging.level property
You set a level with the property logging.level.<logger-name>=<LEVEL>. The logger name is usually a package name or a fully qualified class name, because the common convention is LoggerFactory.getLogger(MyClass.class).
logging.level.root=warn
logging.level.com.example.orders=debug
logging.level.org.springframework.web=infoHere is what each line does:
logging.level.root=warnsets the default for every logger that has no more specific setting. OnlyWARNandERRORare printed for them.logging.level.com.example.orders=debugturns on debug output for your own order code.logging.level.org.springframework.web=infokeeps Spring MVC atINFO, even though the root is stricter.
Level names are not case sensitive. debug, DEBUG and Debug all work.
The YAML form uses nesting:
logging:
level:
root: warn
com.example.orders: debug
org.springframework.web: infoLogger hierarchy and inheritance
Loggers form a tree based on dots in their names. The logger com.example.orders.OrderService is a child of com.example.orders, which is a child of com.example, then com, then the root. A logger with no level of its own inherits the level of its nearest ancestor that has one.
The diagram below shows this for the example properties.
The most specific match wins. If you also set logging.level.com.example.orders.OrderService=trace, that class prints TRACE messages while the rest of the package stays at DEBUG.
What a level does and does not control
A logger level filters at the logger. It does not change what the log looks like or where it goes. Two points cause confusion:
- Raising the level of a package does not lower the level of its children if they have their own setting. A child with an explicit level ignores the parent's.
- Setting a level to
debugdoes not guarantee output. Appenders (the components that write to the console or a file) can have their own filters, and a customlogback-spring.xmlcan override what properties set.
The debug and trace flags
Spring Boot has two shortcut properties:
debug=truedebug=true does not set the root to DEBUG. It enables debug output for a selected set of core loggers: the embedded container, Hibernate, Spring Boot itself, and a few others. It also prints the auto-configuration report. Your own code stays at its normal level.
trace=true does the same for TRACE on that set of loggers.
You can also pass these on the command line, which is useful for a one-off investigation:
java -jar app.jar --debugEnvironment variables and command line overrides
Spring Boot's relaxed binding lets you set levels without editing files. This matters in containers, where the image is fixed and the environment changes.
| Source | Example |
|---|---|
| Properties file | `logging.level.com.example.orders=debug` |
| Command line argument | `--logging.level.com.example.orders=debug` |
| System property | `-Dlogging.level.com.example.orders=debug` |
| Environment variable | `LOGGING_LEVEL_COM_EXAMPLE_ORDERS=debug` |
In the environment variable form, dots become underscores and the name is uppercase. Root is LOGGING_LEVEL_ROOT. Environment variables have higher priority than application.properties, so they win when both are set.
Profile-specific levels
Because levels are ordinary properties, you can vary them by Spring profile with profile-specific files. Create application-dev.properties:
logging.level.com.example.orders=debugAnd application-prod.properties:
logging.level.root=info
logging.level.com.example.orders=infoStart with --spring.profiles.active=dev and the dev file overrides the base file for any key it defines. Keys it does not define keep the base value. A later chapter covers springProfile in logback-spring.xml, which is the alternative when you need more than levels to change.
Choosing levels in practice
A reasonable starting point for each environment:
| Environment | Root | Your packages | Why |
|---|---|---|---|
| Local development | `info` | `debug` | See your own flow, avoid framework noise |
| Test or staging | `info` | `debug` or `info` | Close to production, with detail when needed |
| Production | `warn` or `info` | `info` | Keep volume and cost under control |
Turn on framework debug logging narrowly. For example, to see SQL statements from Hibernate:
logging.level.org.hibernate.SQL=debugSetting logging.level.org.hibernate=debug instead floods the output, since it covers every Hibernate class.
Common errors
The property has no effect. Check these in order:
- The logger name is misspelled. Spring Boot does not warn about names that match no logger.
- The package of your class differs from the one you configured. Verify with the logger name printed in the log line.
- A
logback-spring.xml,logback.xmlorlog4j2file sets the level explicitly. File-based configuration can fight with properties, so check what is on the classpath. - A higher-priority property source overrides yours, such as an environment variable.
- The statement is at a level you are not enabling. A
log.tracecall needstrace, notdebug.
Using logging.level with a class that logs under a different name. A class can create its logger with a name string, such as LoggerFactory.getLogger("audit"). The property must then be logging.level.audit=debug, not the package of the class.
YAML key problems. In YAML, a level value of off can be parsed as the boolean false by some YAML parsers. Quote it to be safe:
logging:
level:
com.example.noisy: "off"Verifying your configuration
A short class lets you check which levels are active:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
@Component
public class LevelProbe implements CommandLineRunner {
private static final Logger log = LoggerFactory.getLogger(LevelProbe.class);
@Override
public void run(String... args) {
log.trace("trace is on");
log.debug("debug is on");
log.info("info is on");
log.warn("warn is on");
log.error("error is on");
}
}Place it in com.example.orders and run with the properties above. You should see all five lines at DEBUG... except trace, which stays hidden because DEBUG is more strict than TRACE. Change the property to trace and all five appear.
Exercise
An application has logging.level.root=warn and logging.level.com.example=debug. A class com.example.billing.InvoiceService calls log.debug(...), and a class org.apache.http.Client calls log.debug(...). Which debug lines are printed, and why? What single property would show the second one too, without changing the root level?
Show answer
The InvoiceService line is printed. Its logger has no level of its own, so it inherits DEBUG from the nearest ancestor with a level, com.example. The org.apache.http.Client line is not printed because that logger inherits WARN from the root. Adding logging.level.org.apache.http=debug enables it without touching the root.
Exercise
You deploy a container image and need com.example.orders at DEBUG for one run without rebuilding. Give two ways to do it, and name the one that wins if application.properties already sets that logger to info.
Show answer
Pass the command line argument --logging.level.com.example.orders=debug, or set the environment variable LOGGING_LEVEL_COM_EXAMPLE_ORDERS=debug. Both are higher priority than application.properties, so either wins over the info setting.