java - Spring Boot、logback 和 logging.config 属性

标签 java spring spring-boot logging logback

我正在使用 logback 库在 Spring Boot 项目中实现日志记录。我想根据我的 Spring 配置文件(属性 spring.pofiles.active)加载不同的日志记录配置文件。我有 3 个文件:

  • logback-dev.xml
  • logback-inte.xml
  • logback-prod.xml

我使用的是 Spring Boot 版本 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是所有使用Spring Boot的项目都会遇到的通病。我采用上述方法是否正确?

我还有其他可行的解决方案,但我发现它们并不那么优雅(在 logback.xml 文件或命令行属性中使用 Janino 进行条件解析)。

最佳答案

我找到了一个解决方案,并且我理解了为什么 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 属性示例:-Dspring.profiles.active=dev


引用文档


编辑(多个 Activity 配置文件)

为了避免多个文件,我们可以使用需要 Janino 依赖项的条件处理(setup here),参见 conditional documentation .

使用这种方法,我们还可以同时检查多个 Activity 配置文件。例如(我没有测试这个解决方案,所以如果它不起作用请评论):

<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条件处理的另一个例子。

关于java - Spring Boot、logback 和 logging.config 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48144955/

相关文章:

java - Spring 配置文件配置

java - 无法使用@value 或使用自动连接到环境从属性文件中读取值

java - Spring Cloud Gateway 使连接过早关闭

Spring scala 示例项目可用吗?

java - jsp 不显示 spring validator 错误

Spring Boot 1.5.8 未登录 JBoss EAP 7

java - 综合参数与隐式参数

java - JUnit : org. junit.experimental.Theories

java - j2me中如何读写文本文件

java - 通过JDBC访问Cloudera Hive时如何设置执行引擎spark