Log Groups in Spring Boot 2.1 – Spring Boot 2.1中的日志组

最后修改: 2020年 4月 22日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

Spring Boot provides many auto-configurations to facilitate writing enterprise applications. However, it was always a bit cumbersome to apply the same logging configuration to a set of loggers.

Spring Boot提供了许多自动配置,以方便编写企业应用程序。然而,将相同的logging配置应用于一组记录器总是有些麻烦。

In this quick tutorial, we’re going to see how the new log groups feature is going to fix this issue.

在这个快速教程中,我们将看到新的日志组功能将如何解决这个问题。

2. Log Groups

2.日志组

As of Spring Boot 2.1, it’s possible to group multiple loggers together and then configure them at the same time.

Spring Boot 2.1开始,可以将多个记录仪分组,然后同时对它们进行配置。

In order to use this feature, first, we should declare a group via the logging.group configuration property:

为了使用这个功能,首先,我们应该通过logging.group配置属性声明一个组。

logging.group.rest=com.baeldung.web,org.springframework.web,org.springframework.http

Here we’re creating a group named rest containing three different logger names. Grouping loggers is as simple as separating their respective logger names with a comma.

在这里,我们要创建一个名为rest的组,其中包含三个不同的记录器名称。对记录仪进行分组,就像用逗号分隔各自的记录仪名称一样简单。

Then we can apply configurations to all loggers in a group at once. For instance, here we’re changing the log level for this group to debug:

然后,我们可以将配置一次性应用于一个组中的所有记录器。例如,在这里我们要把这个组的日志级别改为debug:

logging.level.rest=DEBUG

As a result, Spring Boot applies the same log level for all three group members.

因此,Spring Boot对所有三个组成员适用相同的日志级别。

2.1. Built-in Groups

2.1.内置组

By default, Spring Boot comes with two built-in groups: sql and web. 

默认情况下,Spring Boot有两个内置组。sqlweb。sql web。

Currently, the web group consists of the following loggers:

目前网络组包括以下记录器。

  • org.springframework.core.codec
  • org.springframework.http
  • org.springframework.web
  • org.springframework.boot.actuate.endpoint.web
  • org.springframework.boot.web.servlet.ServletContextInitializerBeans

Similarly, the sql group contains the following loggers:

同样地,sql 组包含以下记录器。

  • org.springframework.jdbc.core
  • org.hibernate.SQL
  • org.jooq.tools.LoggerListener

Configuring the log level for either of these groups would be applied to all group members automatically.

为这些组中的任何一个组配置日志级别,都会自动应用于所有组成员。

3. Conclusion

3.总结

In this short article, we familiarized ourselves with the log groups in Spring Boot. This feature enables us to apply a log configuration to a set of loggers at once.

在这篇短文中,我们熟悉了Spring Boot中的日志组。这个功能使我们能够将一个日志配置一次性应用到一组日志器上。

As usual, the sample code is available over on GitHub.

像往常一样,样本代码可在GitHub上获得。