为"log4j.properties"设置绝对路径.文件 [英] set an absolute path for a "log4j.properties" file

查看:806
本文介绍了为"log4j.properties"设置绝对路径.文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的Web应用程序使用apache commons + log4j.

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

通常,log4j在类路径中需要一个配置文件.但是我需要将日志记录配置委派给外部文件(我需要在环境中部署.war,但是日志配置(最大大小,位置等)取决于第二小组.

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.

我的类路径中有一个commons-logging.properties

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.

是否可以使用外部配置文件设置log4j?

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

推荐答案

您可以将其设置为系统属性log4j.configuration属性..例如在J2SE应用程序中

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

请注意,该属性值必须是URL.

Note, that property value must be a URL.

有关更多信息,请参见 Log4j手册中的默认初始化过程"一节.

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

还可以让ServletContextListener设置系统属性:

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<String> 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) {
    }
}

然后将其放入您的web.xml(context.xml也应该是这样)

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

<context-param>
        <param-name>customPrefix.property</param-name>
        <param-value>value</param-value>
        <param-type>java.lang.String</param-type>
</context-param>

<listener>
    <listener-class>servletUtils.SystemPropertiesHelper</listener-class>    
</listener>

我是从 answer .

我希望这可以对您有所帮助!

I hope this could help you!

这篇关于为"log4j.properties"设置绝对路径.文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆