Sunny
Sunny

Reputation: 615

Extend logback configuration

Is there a way to combine multiple logback configuration files together? Say if there is a parent project with multiple projects inside I'd want the same base configuration or base logback.xml file for all, but I'd like to add or change a few things for each. Can this be done?

Don't really know much about it, but heard that something like this can be done for log4j with the help of properties.

Upvotes: 7

Views: 2842

Answers (1)

user65839
user65839

Reputation:

I think you're looking for the file inclusion feature. Create the common base functionality in one file that has as its root an <included> element, and then include it from your various configuration files with the <include> element.

This is described, with an example, in the Configuration chapter of the manual:

Joran supports including parts of a configuration file from another file. This is done by declaring a <include> element, as shown below:

Example: File 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>

The target file MUST have its elements nested inside an <included> element. For example, a ConsoleAppender could be declared as:

Example: File include (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>

Again, please note the mandatory <included> element.

The contents to include can be referenced as a file, as a resource, or as a URL.

  • As a file: To include a file use the file attribute. You can use relative paths but note that the current directory is defined by the application and is not necessarily related to the path of the configuration file.
  • As a resource: To include a resource, i.e a file found on the class path, use the resource attribute.
    <include resource="includedConfig.xml"/>
    
  • As a URL: To include the contents of a URL use the url attribute.
    <include url="http://some.host.com/includedConfig.xml"/>
    

Upvotes: 6

Related Questions