Spring Sweets: Group Loggers With Logical Name [Snippet]

DZone 's Guide to

Spring Sweets: Group Loggers With Logical Name [Snippet]

Check out the latest feature from Spring Boot 2.1 — group loggers.

· Java Zone ·
Free Resource

Spring Boot 2.1 introduced log groups. A log group is a logical name for one or more loggers. We can define log groups in our application configuration. Then, we can set the log level for a group, so all loggers in the group will get the same log level. This can be very useful to change a log level for multiple loggers that belong together with one setting. Spring Boot already provides two log groups by default: web and sql. In the following list, we see which loggers are part of the default log groups:

  • web: org.springframework.core.codec, org.springframework.http, org.springframework.web, org.springframework.boot.actuate.endpoint.web, org.springframework.boot.web.servlet.ServletContextInitializerBeans
  • sql: org.springframework.jdbc.core, org.hibernate.SQL

To define our own log group, we must add in our application configuration the key logging.group. followed by our log group name. Next, we assign all loggers that we want to be part of the group. Once we have defined our group, we can set the log level using the group name prefixed with the configuration key logging.level..

In the following example configuration, we define a new groupcontrollers that consists of two loggers from different packages. We set the log level for this group to DEBUG. We also set the log level of the default group web to DEBUG:

# src/main/resources/application.properties

# Define a new log group controllers.
logging.group.controllers=mrhaki.hello.HelloController, mrhaki.sample.SampleController

# Set log level to DEBUG for group controllers.
# This means the log level for the loggers
# mrhaki.hello.HelloController and mrhaki.sample.SampleController
# are set to DEBUG.

# Set log level for default group web to DEBUG.

Written with Spring Boot 2.1.3.RELEASE

code snippet ,group loggers ,java ,log ,spring ,spring boot ,spring boot 2.1

Published at DZone with permission of Hubert Klein Ikkink , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}