使用多个配置文件配置回溯

Jua*_*ual 34 logback spring-boot

我试图通过springboot下的配置文件拆分我的logback.xml,这是我的方法:

的logback-prod.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml" />
<property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:- ${java.io.tmpdir:-/tmp}}/}spring.log}"/>
<include resource="org/springframework/boot/logging/logback/file- appender.xml" />

<root level="INFO">
    <appender-ref ref="CONSOLE" />
</root>
</configuration> 
Run Code Online (Sandbox Code Playgroud)

的logback-dev.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml" />
<include resource="org/springframework/boot/logging/logback/console-appender.xml" />

<root level="DEBUG">
    <appender-ref ref="CONSOLE" />
</root>
Run Code Online (Sandbox Code Playgroud)

logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="logback-${spring.profiles.active}.xml"/>

<root level="INFO">
    <appender-ref ref="FILE" />
    <appender-ref ref="CONSOLE" />
</root>
Run Code Online (Sandbox Code Playgroud)

最后使用:

-Dspring.profiles.active=dev
 or
-Dspring.profiles.active=prod
Run Code Online (Sandbox Code Playgroud)

我进入了控制台:

13:01:44,673 |-ERROR in ch.qos.logback.core.joran.spi.Interpreter@2:16 - no  applicable action for [configuration], current ElementPath  is [[configuration][configuration]]
13:01:44,674 |-ERROR in ch.qos.logback.core.joran.spi.Interpreter@3:81 - no applicable action for [include], current ElementPath  is [[configuration][configuration][include]]
13:01:44,674 |-ERROR in ch.qos.logback.core.joran.spi.Interpreter@4:89 - no applicable action for [include], current ElementPath  is [[configuration][configuration][include]]
13:01:44,674 |-ERROR in ch.qos.logback.core.joran.spi.Interpreter@6:25 - no applicable action for [root], current ElementPath  is [[configuration][configuration][root]]
13:01:44,674 |-ERROR in ch.qos.logback.core.joran.spi.Interpreter@7:39 - no applicable action for [appender-ref], current ElementPath  is [[configuration][configuration][root][appender-ref]]
13:01:44,675 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to INFO
Run Code Online (Sandbox Code Playgroud)

rho*_*ath 121

Spring启动文档建议使用logback-spring.xml而不是logback.xml在其中使用spring profile标签:

<configuration>
  <springProfile name="workspace">
    ...
  </springProfile>
  <springProfile name="dev,prd">
    ...
  </springProfile>
</configuration>
Run Code Online (Sandbox Code Playgroud)


Bah*_*ğan 10

如果要为不同的配置文件使用不同的logback配置文件,则可以从application-*.properties文件中进行更改。

例如,您application-prod.properties可以说:

logging.config=src/main/resources/logback-prod.xml
Run Code Online (Sandbox Code Playgroud)

  • 这在 IDE 中运行时有效,但在另一台计算机上打包和执行时无效。您可以使用logging.config=classpath:logback-prod.xml在类路径上搜索(/sf/answers/2214190261/) (6认同)

Ale*_*mov 7

配置文件中的替代方式logback.xml,配置的分离取决于 Spring Profile,如下所示:

<!--    Loggers setup according to Spring Profile-->
<springProfile name="localdev">
    <root level="INFO">
        <appender-ref ref="STDOUT"/>
        <appender-ref ref="FILE"/>
    </root>
    <logger name="com.myapp" level="debug" additivity="false">
        <appender-ref ref="STDOUT"/>
        <appender-ref ref="FILE"/>
    </logger>
</springProfile>

<springProfile name="test">
    <root level="INFO">
        <appender-ref ref="STDOUT"/>
    </root>
    <logger name="com.myapp" level="debug">
        <appender-ref ref="STDOUT"/>
    </logger>
</springProfile>
Run Code Online (Sandbox Code Playgroud)