【问题标题】:Configure logback using several profiles使用多个配置文件配置 logback
【发布时间】:2016-06-09 15:22:17
【问题描述】:

我正在尝试通过 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> 

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>

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>

最后使用:

-Dspring.profiles.active=dev
 or
-Dspring.profiles.active=prod

我进入控制台:

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

【问题讨论】:

    标签: spring-boot logback


    【解决方案1】:

    Spring boot documentation 建议使用logback-spring.xml 而不是logback.xml 并且可以在其中使用spring profile 标签:

    <configuration>
      <springProfile name="workspace">
        ...
      </springProfile>
      <springProfile name="dev,prd">
        ...
      </springProfile>
    </configuration>
    

    【讨论】:

      【解决方案2】:

      如果您想为不同的配置文件使用不同的 logback 配置文件,您可以从您的 application-*.properties 文件中更改它。

      例如在你的application-prod.properties 你可以说:

      logging.config=src/main/resources/logback-prod.xml
      

      【讨论】:

      • 或 yml -> logging.config: src/main/resources/logback-prod.xml
      • 这在 IDE 中运行时有效,但在另一台机器上打包和执行时无效。您可以使用 logging.config=classpath:logback-prod.xml 在类路径上搜索 (stackoverflow.com/a/31631289/3340372)
      【解决方案3】:

      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>
      

      【讨论】:

        【解决方案4】:

        包含的 XML 应具有顶部节点 included 而不是 configuration

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2023-03-30
          • 2021-05-19
          • 2020-09-24
          • 2018-03-27
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多