java log4j.xml 配置的公共变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7613943/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
common variable for log4j.xml configuration
提问by Sean Nguyen
I have log4j.xml configuration like this:
我有这样的 log4j.xml 配置:
<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.
但是,对于很多附加程序,我的文件的根目录是相同的。有没有办法将“/logs/custom/”定义为变量并在我的所有 appender 中重用它。
Thanks,
谢谢,
Sean
肖恩
回答by Neeme Praks
UPDATE: The original answer applies to Log4j 1.x
更新:原始答案适用于 Log4j 1.x
Log4j 2.x has much richer support for properties in configuration file, see the Log4j manual about Configuration with properties.
Log4j 2.x 对配置文件中的属性有更丰富的支持,请参阅 Log4j 手册关于Configuration with properties。
Log4j 1.x (the original answer):
Log4j 1.x(原始答案):
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
.
使用时实现类似功能的唯一方法log4j.xml
是在启动时设置系统属性,然后从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):
或者在运行时以编程方式设置它(在初始化 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.
来源:我从在 log4j xml 配置中使用系统环境变量得到了这个答案的灵感。
Also, if you are running under Tomcat, you can use ${catalina.home}
variable, like this:
此外,如果您在 Tomcat 下运行,则可以使用${catalina.home}
变量,如下所示:
<appender name="MyAppender"class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="${catalina.home}/logs/my.log"/>
...
</appender>
回答by user185624
It is possible in XML as well to define a variable and reuse it in the rest of the doc:
也可以在 XML 中定义一个变量并在文档的其余部分重用它:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" [
<!ENTITY logHome "/logs/folder1/folder2">
]
>
Then, refer to this variable just defined, as &logHome;
然后,引用刚刚定义的这个变量,作为 &logHome;
<param name="File" value="&logHome;/folder3/my.log"/>
How about that?
那个怎么样?
(I believe I learnt about XML entity references some time ago at this link: http://www.ibm.com/developerworks/xml/library/x-tipentref/index.html)
(我相信我前段时间在此链接中了解了 XML 实体引用:http: //www.ibm.com/developerworks/xml/library/x-tipentref/index.html)
回答by matt b
I don't believe this is possible using XML configuration, but it is in a .properties file configuration:
我不相信使用 XML 配置可以做到这一点,但它位于 .properties 文件配置中:
mysubdir = /logs/custom
...
log4j.appender.MyAppender.File = ${mysubdir}/my.log