简体   繁体   中英

Share Spring Boot YAML configuration between two applications

I have two applications in the same maven project and have given each of them their own configuration file by setting the spring.config.name property for each before invoking SpringApplication.run().

This in the first application, I set spring.config.name to server1 so it looks for server1 instead of application.yml . In the second I have set spring.config.name to server2 .

However I would like them to share the same logging configuration. Unfortunately, logging configuration cannot be imported via @PropertySource since logging is already configured before property-sources are read - see Logging section of Spring Boot manual.

Is there any way I can do this?

Spring Boot uses as default Logback. You can put a logback.xml file in src/main/resources to configure the log. And both applications will automatically use this file to configure their logging engine.

You can learn how to configure Logback here: http://logback.qos.ch/manual/configuration.html

A simple example. It will set the log level to INFO and log to the Console:

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM