Pass variable/parameter to logback.xml

By xngo on February 21, 2019

The way to pass variables or parameters to the logback.xml configuration file is to use Java system properties.

Java system properties can be set on the command line as follows: java -DUSER_HOME="/home/sebastien" MyApp2

  • USER_HOME is the variable name that you want to pass to the logback.xml configuration file.

Here is how you use it within the logback.xml configuration file:

<configuration>
 
  <appender name="FILE" class="ch.qos.logback.core.FileAppender">
    <file>${USER_HOME}/myApp.log</file> <!-- USER_HOME = /home/sebastien -->
    <encoder>
      <pattern>%msg%n</pattern>
    </encoder>
  </appender>
 
  <root level="debug">
    <appender-ref ref="FILE" />
  </root>
</configuration>

Reference

  • http://logback.qos.ch/manual/configuration.html#variableSubstitution2

About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.