Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logback configuration: factoring out reusable parts

Tags:

logback

Is there a way to factor out and parameterize repeating parts of Logback XML configuration? I have many different rolling file appenders configured basically the same except for the file names. I use that in conjunction with a bunch of loggers with their 'additivity' turned off so I can redirect different parts of the stack to different files. This adds up to a cumbersome and long configuration file composed of many almost identical segments.

I've used Logback's <include> feature before, but it doesn't address this reuse issue since I can't parameterize the included configuration. I'd expect such a feature to look something akin to:

<include resource="file-appender.xml">
    <property name="filePath" value="/where/logs/go" />
    <property name="baseLogger" value="com.mycompany.thatpartofthestack" />
</include>

But as far as I understand that's wishful thinking. Is there another way of factoring out Logback's configuration via templates, macros, functions or whatnot?

like image 762
Ophir Radnitz Avatar asked Nov 28 '25 22:11

Ophir Radnitz


1 Answers

Try using variable substitution in local and/or context scope.

Perhaps the easiest way is to define variables in some resource file, say logback.properties bundled with each each application. Moreover, each application would carry a logback.xml file importing logback.properties.

<configuration debug="true">
  <property resource="logback.properties" />
  <!-- set root level as given by the value of the root.level variable -->
  <!-- if root.level is undefined default to DEBUG -->
  <root level="${root.level:-DEBUG}"/>
</configuration>

If you wish to set the root level to WARN in webapp-A, simply add the following line in logback.properties file bundled with webapp-A.

root.level=WARN

You can bundle logback.xml as a resource in a artifact common to your various applications.

like image 200
Ceki Avatar answered Dec 01 '25 17:12

Ceki



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!