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 没有加载...

我认为日志记录是所有使用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 属性的 E.G:-Dspring.profiles.active=dev

引用文档:

编辑(多个 Activity 配置文件): 为了避免多个文件,我们可以使用需要 Janino 依赖(setup here)的条件处理,参见 conditional documentation . 使用这种方法,我们还可以同时检查多个 Activity 配置文件。 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。

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

相关文章:

java - 如何通过 spring 集成来管道化 amqp 队列?

java - Spring 安全: How I make to follow a link after logging and not going to the homepage

java - 在java中使用jackson反序列化日期字段时抛出自定义异常

Spring Test Junit 5 & Security boot 2.2 如何模拟认证?

java - 为什么当我尝试将 Spring boot 应用程序推送到 heroku 时它返回 "Fatal error compiling: invalid target release: 11"

java - 线程中断不起作用(Java Android)

java - Aparapium OpenCL 无法在带有 NVidia 显卡的 Manjaro Arch 上运行

java - 返回与原始字符串几乎相同的子字符串

java - 需要使用Java自动连接到ssh

java - 如何在 Spring MVC 3 中使用 Servlet 3 @WebServlet 和异步?