扩展Logback配置

Sun*_*nny 5 java logback slf4j

有没有办法将多个回溯配置文件组合在一起?假设有一个包含多个项目的父项目我希望所有人都有相同的基本配置或基本logback.xml文件,但我想为每个项目添加或更改一些内容.可以这样做吗?

真的不太了解它,但是听说可以在属性的帮助下为log4j做这样的事情.

小智 6

我认为您正在寻找文件包含功能。在一个以元素为根的文件中创建通用基本功能<included>,然后将其从各种配置文件中包含到该<include>元素中。

本手册的“配置”一章中通过示例对此进行了描述:

Joran 支持包含另一个文件中的部分配置文件。这是通过声明一个<include>元素来完成的,如下所示:

示例:文件包含(logback-examples/src/main/resources/chapters/configuration/containingConfig.xml)

<configuration>
  <include file="src/main/java/chapters/configuration/includedConfig.xml"/>

  <root level="DEBUG">
    <appender-ref ref="includedConsole" />
  </root>

</configuration>
Run Code Online (Sandbox Code Playgroud)

目标文件必须将其元素嵌套在<included>元素内。例如,aConsoleAppender可以声明为:

示例:文件包含 (logback-examples/src/main/resources/chapters/configuration/includedConfig.xml)

<included>
  <appender name="includedConsole" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
      <pattern>"%d - %m%n"</pattern>
    </encoder>
  </appender>
</included>
Run Code Online (Sandbox Code Playgroud)

再次请注意强制性<included>要素。

要包含的内容可以作为文件、资源或 URL 来引用。

  • 作为文件: 要包含文件,请使用文件属性。您可以使用相对路径,但请注意,当前目录是由应用程序定义的,不一定与配置文件的路径相关。
  • 作为资源: 要包含资源,即在类路径上找到的文件,请使用资源属性。
    <include resource="includedConfig.xml"/>
    
    Run Code Online (Sandbox Code Playgroud)
  • 作为 URL: 要包含 URL 的内容,请使用url属性。
    <include url="http://some.host.com/includedConfig.xml"/>
    
    Run Code Online (Sandbox Code Playgroud)