Sub topic 6
Grouping loggers to change levels together
This chapter assumes you can set logging.level.* properties and know that a logger name is usually a package or class name. It also assumes your project has the Actuator dependency if you want to try the runtime example at the end.
The problem groups solve
A feature in your application rarely lives in one package. An order flow might touch com.example.orders, com.example.billing, and a client library for a payment provider. When you debug it, you want all of those at DEBUG and everything else left alone.
Without groups you repeat the level once per package:
logging.level.com.example.orders=debug
logging.level.com.example.billing=debug
logging.level.com.example.payments.client=debugThis works, but you must edit three lines to turn the feature off again. A log group gives the set of loggers a name, so you change one line instead.
Defining and using a group
A group takes two properties. logging.group.<name> lists the member loggers, separated by commas. logging.level.<name> sets one level for every member.
logging.level.root=warn
logging.group.orders=com.example.orders,com.example.billing,com.example.payments.client
logging.level.orders=debugAt startup, Spring Boot sees that orders is a group name. It then applies DEBUG to each member logger. The result is the same as the three separate lines above.
The diagram shows how the single level property fans out.
If you use application.yml, the same configuration looks like this:
logging:
level:
root: warn
orders: debug
group:
orders: com.example.orders,com.example.billing,com.example.payments.clientMembers still follow the logger hierarchy
Each member is an ordinary logger name. Logger names form a tree, so setting com.example.orders also covers com.example.orders.web and com.example.orders.OrderService. A group does not change this rule. It only saves you from repeating the level.
Groups that Spring Boot already defines
Spring Boot ships two groups, so you can use them without a logging.group line.
| Group | What it covers |
|---|---|
| `web` | Spring MVC and HTTP internals, such as `org.springframework.web`, `org.springframework.http`, and `org.springframework.core.codec` |
| `sql` | SQL-related loggers, such as `org.springframework.jdbc.core`, `org.hibernate.SQL`, and `org.jooq.tools.LoggerListener` |
You can turn on request-handling detail or SQL statements with one property each:
logging.level.web=debug
logging.level.sql=debugThe exact member list depends on your Spring Boot version, and it can change between releases. Treat the table as a guide and check the reference documentation for your version if you depend on a specific logger.
Choosing group names
A group name shares the namespace of the logging.level.* keys. If you name a group root or reuse a name that is also a real logger, the property is ambiguous to read. Pick a short, descriptive name that is not a package name:
ordersorcheckout-flowdescribe a feature.persistenceoroutbound-httpdescribe a technical concern.comororgshould be avoided because they look like logger names.
Because the group name is a key in a properties file, keep to lowercase letters, digits, and hyphens.
Overlap and precedence
Put each logger in one group, and do not also set it by name. Suppose you write this:
logging.group.orders=com.example.orders,com.example.billing
logging.level.orders=debug
logging.level.com.example.billing=infoNow com.example.billing is set twice, once through the group and once directly. Each setting is applied to the same logger, so only one can win. Do not rely on which one it is. Treat this as a configuration mistake and remove one of the two lines.
Overlap between groups has the same problem. If two groups share a member and set different levels, the outcome is not something to design around.
A hierarchy is different from an overlap. These two settings are fine together:
logging.group.orders=com.example.orders
logging.level.orders=debug
logging.level.com.example.orders.legacy=warncom.example.orders.legacy is a child with its own explicit level, so it is more specific than the parent. The logging framework gives the closest configured ancestor priority over a farther one. Logs from legacy stay at warn, while the rest of orders is at debug.
Changing a group at runtime
Groups also work with the Actuator loggers endpoint, so you can change several loggers in a running application with one request. First expose the endpoint:
management.endpoints.web.exposure.include=health,loggersThen read the group. The response lists its configuredLevel and its members:
curl http://localhost:8080/actuator/loggers/ordersTo change the level for every member without a restart, send a POST to the group name:
curl -X POST http://localhost:8080/actuator/loggers/orders \
-H 'Content-Type: application/json' \
-d '{"configuredLevel": "TRACE"}'To remove the override, send null. Each member goes back to the level it inherits from its parent:
curl -X POST http://localhost:8080/actuator/loggers/orders \
-H 'Content-Type: application/json' \
-d '{"configuredLevel": null}'The runtime change lasts until the application restarts. The properties file is not modified.
Common errors
| Symptom | Likely cause |
|---|---|
| The level has no effect and the group is ignored | The group name in `logging.level.<name>` does not match the name in `logging.group.<name>`, so Boot treats it as a logger called `<name>`. |
| A member logs at an unexpected level | The same logger is set directly or in another group. Remove the duplicate. |
| Only some classes in a package change | The member name is a class or sub-package, not the parent package. Use the shared parent. |
| A space after a comma breaks a member name | Remove spaces in the list, or check that your Boot version trims them. |
| `POST` to the group returns 404 | The `loggers` endpoint is not exposed, or the name is not a known group or logger. |
When to use a group
Use a group when the same level applies to loggers that belong together and you expect to switch them as a unit. This covers a feature, a layer, or a third-party library with several packages. Do not use one for a single package, because logging.level.com.example.orders=debug is already a one-line change. Do not use groups to hide a messy package layout. If you often need three packages together, that may point to a design problem rather than a logging one.
Exercise
Your application has packages com.example.inventory and com.example.shipping. You want both at DEBUG while everything else stays at WARN, and you want to switch both with one property. Write the properties, then name the Actuator request that would set both to INFO at runtime.
Show answer
Properties: logging.level.root=warn, logging.group.fulfilment=com.example.inventory,com.example.shipping, and logging.level.fulfilment=debug. At runtime, send a POST to /actuator/loggers/fulfilment with the body {"configuredLevel": "INFO"}. This needs loggers in management.endpoints.web.exposure.include. The change affects both members and lasts until restart.
Exercise
A teammate defines logging.group.orders=com.example.orders,com.example.billing and logging.level.orders=debug. They also keep the line logging.level.com.example.billing=error. The billing logs behave unpredictably. What is wrong, and how do you fix it?
Show answer
com.example.billing is configured twice: once through the group and once by name, so the two settings compete for the same logger. Remove one. If billing needs a different level, take it out of the group and keep only its own line. If it should follow the group, delete the error line.