log4j.xml配置的公共变量 [英] common variable for log4j.xml configuration
问题描述
我有这样的log4j.xml配置:
I have log4j.xml configuration like this:
<appender name="MyAppender"class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="/logs/custom/my.log"/>
...
</appender>
然而,对于很多appender,我的文件的根目录是相同的。有没有办法将/ logs / custom /定义为变量并在我的所有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.
谢谢,
Sean
推荐答案
更新:原始答案适用于Log4j 1。 x
UPDATE: The original answer applies to Log4j 1.x
Log4j 2.x对配置文件中的属性有更丰富的支持,请参阅Log4j手册关于具有属性的配置。
Log4j 2.x has much richer support for properties in configuration file, see the Log4j manual about Configuration with properties.
Log4j 1。 x(原始答案):
使用 log4j.xml $ c时实现类似功能的唯一方法$ c>是在启动时设置系统属性,然后从
log4j.xml
中引用它。
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启动时,设置系统属性:
At startup, you set your system property:
java -Dlog_dir=/var/logs/custom com.yourorg.yourapp.Main
或者设置它在运行时以编程方式(在初始化Log4j之前):
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
资料来源:我从使用中得到了这个答案的灵感log4j xml配置中的系统环境变量。
此外,如果您在Tomcat下运行,则可以使用 $ {catalina.home }
变量,如下所示:
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>
这篇关于log4j.xml配置的公共变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!