java log4j 路径配置,为“ log4j.properties”设置绝对路径。文件

I am using apache commons + log4j for my web app.

normally log4j needs a configuration file inside the classpath; but I need to delegate the logging configuration to an external file (I need to deploy a .war in an environment, but the log configurations (max size, position, etc) it's up to a second team.

I have a commons-logging.properties in my classpath

org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger

# log4j.configuration=/absolute/path/where/external/logs/are/log4j.properties

unfortunately, the commented line doesn't work.

Is there a way to set up log4j with an external configuration file?

解决方案

You can set it as a system property log4j.configuration property .. for example in J2SE app

java -Dlog4j.configuration=file:/path/to/log4j.properties myApp

Note, that property value must be a URL.

For more read section 'Default Initialization Procedure' in Log4j manual.

It's also possible letting a ServletContextListener set the System properties:

import java.util.Enumeration;

import javax.servlet.*;

public class SystemPropertiesHelper implements

javax.servlet.ServletContextListener {

private ServletContext context = null;

public void contextInitialized(ServletContextEvent event) {

context = event.getServletContext();

Enumeration params = context.getInitParameterNames();

while (params.hasMoreElements()) {

String param = (String) params.nextElement();

String value =

context.getInitParameter(param);

if (param.startsWith("customPrefix.")) {

System.setProperty(param, value);

}

}

}

public void contextDestroyed(ServletContextEvent event) {

}

}

And then put this into your web.xml (should be possible for context.xml too)

customPrefix.property

value

java.lang.String

servletUtils.SystemPropertiesHelper

I got this from this listener code from answer .

I hope this could help you!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值