Sub topic 6
Grouping loggers to change levels together
This chapter assumes you can set per-package levels in application.properties and know that logger names usually match fully qualified class names. It builds on that by showing how to set one level for several packages at once.
The problem groups solve
A feature often spans several packages. An order flow might touch com.example.orders and com.example.billing. To debug it, you would write one logging.level.* line per package and remember to remove each one afterward. Missing one leaves noisy debug output in place.
A log group is a named list of loggers. You set a level on the group name, and Spring Boot applies that level to every logger in the list.
Defining and using a group
Define the group with logging.group.<name>, using a comma-separated list of logger names. Then set its level with logging.level.<name>.
logging.level.root=warn
logging.group.orders=com.example.orders,com.example.billing
logging.level.orders=debugWith this configuration, com.example.orders and com.example.billing both log at DEBUG. Each entry is a normal logger name, so child packages such as com.example.orders.web are included through the usual logger hierarchy.
To turn the group off again, change one line:
logging.level.orders=warnBuilt-in groups
Spring Boot ships with predefined groups, so you can use them without defining anything.
| Group | What it covers |
|---|---|
| `web` | `org.springframework.core.codec`, `org.springframework.http`, `org.springframework.web` and related web classes |
| `sql` | `org.springframework.jdbc.core`, `org.hibernate.SQL`, `org.jooq.tools.LoggerListener` |
For example, this shows the SQL statements your application runs:
logging.level.sql=debugThe exact members of each built-in group can change between Spring Boot versions. Check the reference documentation for your version if you depend on them.
How groups interact with other level settings
A group is only a shortcut for setting the same level on several loggers. Two points follow from that.
- If a logger is named in a group and also has its own
logging.level.<logger>entry, the two settings compete on one logger. Avoid listing the same logger both ways, because the result is hard to predict when you read the file later. - A more specific logger still overrides its parent. If
com.example.ordersis in a group set todebugand you addlogging.level.com.example.orders.legacy=error, thelegacypackage logs only errors.
logging.group.orders=com.example.orders,com.example.billing
logging.level.orders=debug
logging.level.com.example.orders.legacy=errorCommon mistakes
- Using the group name before defining it.
logging.level.orders=debughas no effect on your packages if nologging.group.ordersline exists. - Putting spaces or typos in the logger list. Write
com.example.orders,com.example.billingand check each name against the real package. - Choosing a group name that matches a real logger name. Pick names such as
ordersorpayments-flowthat are clearly labels, not packages.
Exercise
Your application has packages com.example.inventory and com.example.shipping. Write the properties that make both log at TRACE while everything else stays at warn.
Show answer
logging.level.root=warn logging.group.fulfilment=com.example.inventory,com.example.shipping logging.level.fulfilment=trace
Exercise
You set logging.level.orders=debug but see no debug output from com.example.orders. What is the most likely cause?
Show answer
The group was never defined. There must be a logging.group.orders=... line that lists the packages. Without it, orders is treated as an ordinary logger name and matches nothing in your code.