I have log4j.xml configuration like this:
<appender name="MyAppender"class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="/logs/custom/my.log"/>
...
</appender>
However the root directory of my file are the same for a lot of appender. Is there a way to defined “/logs/custom/” as a variable and reused it in all of my appender.
Thanks,
Sean
UPDATE: The original answer applies to Log4j 1.x
Log4j 2.x has much richer support for properties in configuration file, see the Log4j manual about Configuration with properties.
Log4j 1.x (the original answer):
The only way to achieve something similar when you are using
log4j.xmlis to set a system property at startup and then reference that from yourlog4j.xml.At startup, you set your system property:
Or set it programmatically at runtime (before initializing Log4j):
Then you can reference it like this:
Or in properties file, like this:
Source: I got inspiration for this answer from Using system environment variables in log4j xml configuration.
Also, if you are running under Tomcat, you can use
${catalina.home}variable, like this: