简体   繁体   English

log4j.xml配置的公共变量

[英]common variable for log4j.xml configuration

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. 但是我的文件的根目录对于很多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 肖恩

It is possible in XML as well to define a variable and reuse it in the rest of the doc: 在XML中也可以定义一个变量并在doc的其余部分重用它:

<?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

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手册,了解有关配置属性的信息

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>

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM