Different Log4j2 Configurations per Spring Profile – 每个Spring Profile的不同Log4j2配置

最后修改: 2021年 9月 16日

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

1. Overview

1.概述

In our previous tutorials, Spring Profiles and Logging in Spring Boot, we showed how to activate different profiles and use Log4j2 in Spring.

在我们之前的教程Spring ProfilesLogging in Spring Boot中,我们展示了如何在Spring中激活不同的配置文件和使用Log4j2。

In this short tutorial, we’ll learn how to use different Log4j2 configurations per Spring profile.

在这个简短的教程中,我们将学习如何按Spring配置文件使用不同的Log4j2配置

2. Use Different Properties Files

2.使用不同的属性文件

For example, suppose we have two files, log4j2.xml and log4j2-dev.xml, one for the default profile and the other for the “dev” profile.

例如,假设我们有两个文件,log4j2.xmllog4j2-dev.xml,一个用于默认配置文件,另一个用于 “dev “配置文件。

Let’s create our application.properties file and tell it where to find the logging config file:

让我们创建我们的application.properties文件并告诉它在哪里找到日志配置文件。

logging.config=/path/to/log4j2.xml

Next, let’s create a new properties file for our “dev” profile named application-dev.properties and add a similar line:

接下来,让我们为我们的 “dev “配置文件创建一个新的属性文件,命名为application-dev.properties并添加类似的行。

logging.config=/path/to/log4j2-dev.xml

If we have other profiles – for example, “prod” – we only need to create a similarly named properties file – application-prod.properties for our “prod” profile. Profile-specific properties always override the default ones.

如果我们有其他配置文件–例如,”prod”–我们只需要为我们的 “prod “配置文件创建一个类似名称的属性文件–application-prod.properties特定配置文件的属性总是覆盖默认的属性

3. Programmatic Configuration

3.程序化配置

We can programmatically choose which Log4j2 configuration file to use by changing our Spring Boot Application class:

我们可以通过改变Spring Boot的Application类,以编程方式选择使用哪个Log4j2配置文件。

@SpringBootApplication
public class Application implements CommandLineRunner {

    @Autowired
    private Environment env;

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Override
    public void run(String... param) {
        if (Arrays.asList(env.getActiveProfiles()).contains("dev")) {
            Configurator.initialize(null, "/path/to/log4j2-dev.xml");
        } else {
            Configurator.initialize(null, "/path/to/log4j2.xml");
        }
    }
}

Configurator is a class of the Log4j2 library. It provides several ways to construct a LoggerContext using the location of the configuration file and various optional parameters.

Configurator是Log4j2库的一个类。它提供了几种方法来构建一个LoggerContext,使用配置文件的位置和各种可选参数。

This solution has one drawback: The application boot process won’t be logged using Log4j2.

这个解决方案有一个缺点。应用程序的启动过程不会使用Log4j2进行记录

4. Conclusion

4.总结

In summary, we’ve seen two approaches to using different Log4j2 configurations per Spring profile. First, we saw that we could provide a different properties file for each profile. Then, we saw an approach for configuring Log4j2 programmatically at application startup based on the active profile.

综上所述,我们已经看到了两种为每个Spring配置文件使用不同Log4j2配置的方法。首先,我们看到我们可以为每个配置文件提供一个不同的属性文件。然后,我们看到了一种方法,即在应用程序启动时根据活动配置文件以编程方式配置Log4j2。