Search code examples
javaloggingconfigurationlogbacklog4j2

log4j2 and ch.qos.logback.core.PropertyDefinerBase


Logback's PropertyDefinerBase allows to use dynamic properties within logback.xml. I use this to configure a target directory for a FileAppender with values from my application config. In my case there's an enum instance that contains the value I want to use:

public String getPropertyValue() {return MyConfigEnum.LOGDIR.value();}

Relevant parts of logback.xml:

<fileNamePattern>${logdir}/test.log</fileNamePattern>

... and:

<define name="logdir" class="com.example.MyLogdirDefiner" />

This idiom is now needed for log4j 2. How can I apply the value of MyConfigEnum.LOGDIR in a log4j2.xml?

I don't want to set a property upon application start.


Solution

  • Log4j2 also lets you define new properties in the configuration file.

    Details are in the Configuration > Property Substitution section of the manual: http://logging.apache.org/log4j/2.x/manual/configuration.html#PropertySubstitution