【问题标题】:spring boot, logback and logging.config propertyspring boot、logback 和 logging.config 属性
【发布时间】:2015-06-08 08:38:00
【问题描述】:

我正在使用 logback 库在 Spring Boot 项目中实现日志记录。我想根据我的弹簧配置文件(属性'spring.pofiles.active')加载不同的日志记录配置文件。我有 3 个文件:logback-dev.xml、logback-inte.xml 和 logback-prod.xml。我正在使用 Spring Boot 版本 1.2.2.RELEASE。

正如您在spring boot documentation 中看到的那样:

可以通过在类路径中包含适当的库来激活各种日志系统,并通过在类路径的根目录中或在 Spring Environment 属性 日志记录指定的位置提供合适的配置文件来进一步定制。配置。 (但请注意,由于在创建 ApplicationContext 之前已初始化日志记录,因此无法从 Spring @Configuration 文件中的 @PropertySources 控制日志记录。系统属性和传统的 Spring Boot 外部配置文件都可以正常工作。)

所以我尝试在 application.properties 文件中设置“logging.config”属性:

logging.config=classpath:/logback-${spring.profiles.active}.xml

但是当我启动我的应用程序时,我的 logback-{profile}.xml 没有加载...

我认为日志记录是所有使用spring boot的项目都遇到的常见问题。我在上述方法的正确轨道上吗?我还有其他可行的解决方案,但我发现它们没有那么优雅(在 logback.xml 文件或命令行属性中使用 Janino 进行条件解析)。

【问题讨论】:

标签: java spring spring-boot logging logback


【解决方案1】:

我找到了一个解决方案,我明白为什么 spring 不使用我在 application.properties 文件中定义的“logging.config”属性。

解决方案及说明:

在初始化日志时,spring Boot 只查看classpath or environment variables

我使用的解决方案是包含一个父 logback.xml 文件,该文件根据 spring 配置文件包含正确的日志记录配置文件。

logback.xml:

<configuration>
    <include resource="logback-${spring.profiles.active}.xml"/>
</configuration>

logback-[profile].xml(在本例中为 logback-dev.xml):

<included>

    <!-- put your appenders -->
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
    <!-- encoders are assigned the type
     ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
       <encoder>
           <pattern>%d{ISO8601} %p %t %c{0}.%M - %m%n</pattern>
           <charset>utf8</charset>
        </encoder>
    </appender>

    <!-- put your loggers here -->
    <logger name="org.springframework.web" additivity="false" level="INFO">
        <appender-ref ref="CONSOLE" />
    </logger>

    <!-- put your root here -->
    <root level="warn">
        <appender-ref ref="CONSOLE" />
    </root>

</included>

注意: 启动应用程序时,必须在命令行参数中设置“spring.profiles.active”。 JVM 属性的 E.G:-Dspring.profiles.active=dev

参考文档:

编辑(多个活动配置文件): 为了避免多个文件,我们可以使用需要 Janino 依赖的条件处理(setup here),请参阅conditional documentation。 使用这种方法,我们还可以同时检查多个活动配置文件。 E.G(我没有测试这个解决方案,所以如果它不起作用请评论):

<configuration>

    <if condition='"${spring.profiles.active}".contains("profile1")'>
        <then>
         <!-- do whatever you want for profile1 -->
        </then>
    </if>

    <if condition='"${spring.profiles.active}".contains("profile2")'>
        <then>
         <!-- do whatever you want for profile2 -->
        </then>
    </if>

    <!-- common config -->

</configuration>

有关条件处理的另一个示例,请参见 javasenior answer。

【讨论】:

  • 这种方法似乎适合您的用例,但是,我们可能在环境中定义了多个配置文件,这会导致包含中断。即 spring.profiles.active=profile1,profile2。
  • 是的,这是真的。在这种情况下,我会使用 janino 库。
  • 当您同时拥有多个活动配置文件时,是否有任何关于优雅解决方案的更新?
  • 查看 Spring Boot 1.3.0 中的 springProfile extension
  • 您也可以在 application.properties 中定义配置文件,默认 dev 为 spring.profiles.active=${dev:dev}。如果你不通过 -Dspring.profiles.active 默认将是 dev
【解决方案2】:

另一种可以处理多个配置文件的方法是为每个环境创建一个单独的属性文件。

应用程序-prod.properties

logging.config=classpath:logback-prod.xml

应用程序-dev.properties

logging.config=classpath:logback-dev.xml

应用程序本地属性

logging.config=classpath:logback-local.xml

注意

如果你不小心,你最终可能会在意想不到的地方登录

-Dspring.profiles.active=local,dev //will use logback-dev.xml
-Dspring.profiles.active=dev,local //will use logback-local.xml

【讨论】:

    【解决方案3】:

    我建议不要为每个配置文件添加单独的 logback xmls 或具有 IF 条件,而是建议以下(如果您的 xmls 差异较小)以便于条件处理:

    <springProfile name="dev">
    <logger name="org.sample" level="DEBUG" />
    </springProfile>
    <springProfile name="prod">
    <logger name="org.sample" level="TRACE" />
    </springProfile>
    

    【讨论】:

    【解决方案4】:

    使用 logback 进行条件处理将是一个没有很多 logback 文件的解决方案。这是a link 和一个带有弹簧配置文件的示例 logback 配置。

    <configuration>
    
        <property name="LOG_LEVEL" value="INFO"/>
    
            <if condition='"product".equals("${spring.profiles.active}")'>
               <then>
                    <property name="LOG_LEVEL" value="INFO"/>
               </then>
               <else>
                    <property name="LOG_LEVEL" value="ERROR"/>
               </else>
            </if>
    
             .
             .
             appender, logger tags etc.
             .
             .
    
             <root level="${LOG_LEVEL}">
                 <appender-ref ref="STDOUT"/>
             </root>
    
    </configuration>
    

    另外,您可能必须将其添加到您的 pom.xml 中

    <dependency>
        <groupId>org.codehaus.janino</groupId>
        <artifactId>janino</artifactId>
        <version>3.0.6</version>
    </dependency>
    

    【讨论】:

      【解决方案5】:

      Spring 在 Logback XML 文件中支持下一个标签 &lt;springProperty/&gt;,这个标签描述了 here 。这意味着您可以轻松地从 Spring 属性文件中添加变量,即使该变量值由 Spring 从环境/系统变量中解析。

      【讨论】:

        【解决方案6】:

        您可以为不同的配置文件指定不同的 logback.xml,只需 3 步:

        1、在application.propertiesapplication.yml中指定激活的配置文件:

        spring.profiles.active: test
        

        2,通过配置文件配置 logback 以包含不同的配置:

        <!DOCTYPE configuration>
        <configuration scan="true" scanPeriod="30 seconds">
            <springProperty scope="context" name="profile" source="spring.profiles.active"/>
            <include resource="logback.${profile}.xml"/>
        </configuration>
        

        3、创建配置文件logback.test.xml

        <?xml version="1.0" encoding="UTF-8"?>
        <included>
            <include resource="org/springframework/boot/logging/logback/base.xml"/>
            <root level="INFO"/>
        </included>
        

        很简单,不需要做任何其他事情。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-01-16
          • 1970-01-01
          • 1970-01-01
          • 2019-03-04
          • 2023-04-04
          • 2019-01-19
          • 1970-01-01
          相关资源
          最近更新 更多