简体   繁体   English

Spring Boot:如何根据当前环境或spring配置文件使用自定义logback.xml

[英]Spring Boot: How to use custom logback.xml depending on current environment or spring profile

I have read the following two sources: 我已经阅读了以下两个来源:

https://examples.javacodegeeks.com/enterprise-java/spring/load-environment-configurations-and-properties-with-spring-example/ https://examples.javacodegeeks.com/enterprise-java/spring/load-environment-configurations-and-properties-with-spring-example/

spring-boot logback.xml property depending on profile spring-boot logback.xml属性,具体取决于配置文件

And I'm trying to do the following: 我正在尝试执行以下操作:

When I run my Spring Boot application with a specific spring active profile ( gradlew bootRun -Dspring.profiles.active=sst , which we use for single service tests), I need the application use specific logging configuration (let's say with specific log level or using logging that we could capture output of from the tests). 当我使用特定的Spring活动配置文件( gradlew bootRun -Dspring.profiles.active=sst ,我们用于单个服务测试)运行Spring Boot应用程序时,我需要应用程序使用特定的日志记录配置(假设具有特定的日志级别或使用我们可以捕获测试输出的日志记录)。

We do have a custom application-sst.properties file configured and that is picked up and works all right. 我们确实配置了一个自定义的application-sst.properties文件,并且该文件已被拾取并且可以正常工作。

Is there a way for me to do something similar for the logback.xml - such as adding logback-sst.xml so that is used within the SST context? 有没有办法让我为logback.xml做类似的事情 - 例如添加logback-sst.xml以便在SST上下文中使用?

Some suggestions: 一些建议:

  1. If you need something a special configuration for tests only, there is a simple solution: Place logback-test.xml in src/test/resources and you're good to go. 如果您只需要一些特殊的测试配置,那么有一个简单的解决方案:将logback-test.xml放在src/test/resources ,你就可以了。

  2. Logback supports a concept of spring profiles that allow placing configurations for different profiles in the same file: Logback支持弹簧配置文件的概念,允许在同一文件中放置不同配置文件的配置:

Example: 例:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

<springProfile name="dev">
   <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
   ... 
  </appender>

  <root level="DEBUG">
    <appender-ref ref="CONSOLE"/>
  </root>
</springProfile>

<springProfile name="staging">
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
     ...
    </appender>

    <root level="DEBUG">
      <appender-ref ref="CONSOLE"/>
    </root>
</springProfile>

</configuration>

Here you can find the relevant tutorial 在这里您可以找到相关的教程

Spring will only pickup profile information in your logback config when naming your config file " logback-spring.xml " instead of "logback.xml". 在命名配置文件“ logback-spring.xml ”而不是“logback.xml”时,Spring只会在您的logback配置中获取配置文件信息。

Inside the config file you can simply use your profiles like this example: 在配置文件中,您可以像以下示例一样使用您的配置文件:

 <springProfile name="sst">
     <appender-ref ref="JSON_STDOUT"/>
     ...
 </springProfile>

You can even use negation for your profiles: 您甚至可以对您的个人资料使用否定:

 <springProfile name="!prod">
     <appender-ref ref="JSON_STDOUT"/>
     ...
 </springProfile>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM