【问题标题】:How to specify default Logback configuration for any spring profile that is not already present in the config如何为配置中尚未存在的任何 spring 配置文件指定默认 Logback 配置
【发布时间】:2020-06-01 10:24:24
【问题描述】:

这是我的简化 logback-spring.xml 配置:

<configuration>

    <!-- appender config -->

    <springProfile name=dev>
        <!-- dev specific config here -->
    </springProfile>

    <springProfile name=prod>
        <!-- prod specific config here -->
    </springProfile>

    <!-- other profiles -->

</configuration>

当我使用一些不同的配置文件运行我的应用程序时,我想获得一些默认的 logback 配置。我在logback-spring.xml 中找不到有关具有某些默认配置的信息。

请注意,定义的配置文件超过 2 个,我不知道其他临时配置文件的名称。

谢谢, 巴甫洛

【问题讨论】:

    标签: spring logback spring-logback


    【解决方案1】:

    实际上,发现在配置文件标签之外定义配置可以完成这项工作:

    <configuration>
    
        <!-- appenders and profiles -->
    
        <root level="level here">
            <appender-ref ref="name here"/>
        </root>
    
        <logger name="name here" level="level here"/>
        <!-- other loggers -->
    
    </configuration>
    

    在这种情况下,它将被视为基本配置。您可以覆盖配置文件特定块中的 appender-ref 和记录器。

    【讨论】:

      【解决方案2】:

      你可以这样定义:

      <configuration>
      
      <!-- appender config -->
      
      <springProfile name=dev>
          <!-- dev specific config here -->
      </springProfile>
      
      <springProfile name=prod>
          <!-- prod specific config here -->
      </springProfile>
      
      <!-- other profiles -->
      
      <springProfile name="!(dev| prod)">
         <!-- other specific config here -->
      </springProfile>
      

      【讨论】:

      • 谢谢,这会起作用,但我想知道是否有任何解决方案不会让我在默认配置中列出所有配置文件。
      • 在 asnwers 示例中,您只需要定义不同的特定配置对于其他配置,您可以按 name=!(dev| prod) 列出它们。如果您想要一些通用配置,您可以使用 name=(开发|产品)
      • 只有在“默认”Logback 配置之后指定 springprofiles 时才为真,否则始终采用默认配置
      猜你喜欢
      • 1970-01-01
      • 2023-02-09
      • 2016-09-16
      • 2018-07-10
      • 2013-01-04
      • 2019-08-16
      • 1970-01-01
      • 2021-02-01
      • 2016-03-21
      相关资源
      最近更新 更多