spring 启动、登录和 logging.config 属性

spring boot, logback and logging.config property

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

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

The various logging systems can be activated by including the appropriate libraries on the classpath, and further customized by providing a suitable configuration file in the root of the classpath, or in a location specified by the Spring Environment property logging.config. (Note however that since logging is initialized before the ApplicationContext is created, it isn’t possible to control logging from @PropertySources in Spring @Configuration files. System properties and the conventional Spring Boot external configuration files work just fine.)

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

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

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

我觉得logging是所有使用springboot的项目都遇到过的通病。我采用上述方法是否走在正确的轨道上?我还有其他可行的解决方案,但我发现它们并不那么优雅(在 logback.xml 文件或命令行 属性 中使用 Janino 进行条件解析)。

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

解决方法及解释:

初始化日志记录时,spring 引导仅在 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 属性的示例:-Dspring.profiles.active=dev

参考文档:

编辑(多个活动配置文件): 为了避免多个文件,我们可以使用需要 Janino 依赖的条件处理(setup here), see 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。

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

应用-prod.properties

logging.config=classpath:logback-prod.xml

应用-dev.properties

logging.config=classpath:logback-dev.xml

应用-local.properties

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

使用 logback 的条件处理将是一个没有很多 logback 文件的解决方案。这是 a link 和一个带有 spring 配置文件的示例 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>

与其为每个配置文件添加单独的 logback xml 或具有 IF 条件,不如建议以下内容(如果 xml 的差异较小)以便于条件处理:

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

Spring 支持 Logback XML 文件中的下一个标签 <springProperty/>,该标签描述了 here。这意味着您可以轻松地从 Spring 属性 文件添加变量,即使这个变量值通过 Spring.

从 environment/system 变量解析

您可以为不同的配置文件指定不同的 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>

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