java log4j xml配置详解,log4j.xml配置的公共变量

I have log4j.xml configuration like this:

...

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.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:

...

Or in properties file, like this:

log4j.appender.MyAppender.File = ${log_dir}/my.log

Also, if you are running under Tomcat, you can use ${catalina.home} variable, like this:

...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值