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.xml
is to set a system property at startup and then reference that from your log4j.xml
.
At startup, you set your system property:
java -Dlog_dir=/var/logs/custom com.yourorg.yourapp.Main
Or set it programmatically at runtime (before initializing Log4j):
System.setProperty("log_dir", "/var/logs/custom")
Then you can reference it like this:
<appender name="MyAppender"class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="${log_dir}/my.log"/>
...
</appender>
Or in properties file, like this:
log4j.appender.MyAppender.File = ${log_dir}/my.log
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:
<appender name="MyAppender"class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="${catalina.home}/logs/my.log"/>
...
</appender>