Share Spring 在两个应用程序之间启动 YAML 配置

Share Spring Boot YAML configuration between two applications

我在同一个 Maven 项目中有两个应用程序,并且在调用 SpringApplication.run() 之前通过为每个应用程序设置 spring.config.name 属性 为每个应用程序提供了自己的配置文件。

因此,在第一个应用程序中,我将 spring.config.name 设置为 server1,以便它查找 server1 而不是 application.yml。在第二个中,我将 spring.config.name 设置为 server2

但是我希望他们共享相同的日志记录配置。不幸的是,无法通过@PropertySource 导入日志记录配置,因为在 属性 之前已经配置了日志记录 - 读取源 - 请参阅 Spring 引导手册的 Logging section

有什么办法可以做到这一点吗?

Spring Boot 使用默认的 Logback。你可以在src/main/resources中放一个logback.xml文件来配置日志。并且这两个应用程序将自动使用此文件来配置其日志记录引擎。

您可以在此处了解如何配置 Logback:http://logback.qos.ch/manual/configuration.html

一个简单的例子。它会将日志级别设置为 INFO 并记录到控制台:

<configuration>
  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
      <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
  </appender>

  <root level="info">
    <appender-ref ref="STDOUT" />
  </root>
</configuration>