我们在项目中遇到了类似的问题,我们在一个 ibm liberty Web 容器中部署了两个 Spring Boot War。我们通过提供自定义 logback-spring.xml 来修复它。以下是修复它的配置:
logback-spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<springProperty scope="context" name="springAppName" source="spring.application.name"/>
<property name="LOG_LEVEL_PATTERN" value="%clr(%5p) %clr([${springAppName},%X{X-B3-TraceId:-},%X{X-B3-SpanId:-},%X{X-Span-Export:-}]){yellow}" />
<!-- You can override this to have a custom pattern -->
<property name="CONSOLE_LOG_PATTERN"
value="%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}"/>
<appender name="FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<encoder>
<pattern>${CONSOLE_LOG_PATTERN}</pattern>
</encoder>
<file>${server.output.dir}logs/myapplication1.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${server.output.dir}logs/myapplication1%d{yyyy-MM-dd}.log</fileNamePattern>
<!--<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">-->
<!--<maxFileSize>1MB</maxFileSize>-->
<!--</timeBasedFileNamingAndTriggeringPolicy>-->
</rollingPolicy>
</appender>
<logger name="com.sample.controllers.MyController" level="DEBUG" additivity="false">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="FILE"/>
</logger>
<logger name="org.springframework.jdbc.core.StatementCreatorUtils" level="TRACE" additivity="false">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="FILE"/>
</logger>
<root level="INFO">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="FILE"/>
</root>
确保它被 maven 包含在类路径中
<profiles>
<profile>
<id>default</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/application.properties</include>
<include>**/*.xml</include>
<include>**/*.yml</include>
</includes>
</resource>
</resources>
</build>
</profile>
</profiles>
并在您的 src/main/resources/bootstrap.yml 中添加以下条目
春天:应用程序:名称:myapplication1 jmx:默认域:myapplication1
在具有不同应用程序名称的两个 Spring Boot 应用程序中的每一个中都有此配置