【问题标题】:set an absolute path for a "log4j.properties" file为“log4j.properties”文件设置绝对路径
【发布时间】:2014-11-25 09:56:49
【问题描述】:

我正在为我的网络应用程序使用 apache commons + log4j。

通常 log4j 需要类路径中的配置文件;但我需要将日志配置委托给外部文件(我需要在环境中部署 .war,但日志配置(最大大小、位置等)取决于第二个团队。

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

org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger
# log4j.configuration=/absolute/path/where/external/logs/are/log4j.properties

很遗憾,注释行不起作用。

有没有办法使用外部配置文件设置 log4j?

【问题讨论】:

    标签: java logging log4j apache-commons-logging


    【解决方案1】:

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

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

    注意,该属性值必须是 URL。

    更多信息请阅读Log4j manual.中的“默认初始化程序”部分

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

    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 也应该可以)

    <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 的监听器代码中得到了这个。

    希望对你有帮助!

    【讨论】:

      【解决方案2】:

      可以使用jvm参数表示配置文件路径:

      -Dlog4j.configuration=absolute path
      

      绝对路径示例:

      java -Dlog4j.configuration="file:/dir1/log4j.properties"
      

      【讨论】:

      • AFAIK 这种方式来设置配置文件,它仍然使用相对路径;你能指出我使用绝对路径使用它的文档吗?
      • 精彩的答案。 file 前缀似乎是强制性的,至少在 unix 操作系统上是这样。
      【解决方案3】:

      设置系统属性log4j.configuration=/abslute/or/relative/path_to_file_name

      commons logging 只需要知道它正在使用什么日志实现,它自己的日志实现以它总是配置的任何方式配置。

      这在log4j manual.中有记录

      【讨论】:

        【解决方案4】:

        如果您使用的是Spring Framework,则可以使用org.springframework.web.util.Log4jConfigListener

        您只需要使用文件位置指定参数log4jConfigLocation(使用URL for File)。例如:

        <context-param>
            <param-name>log4jConfigLocation</param-name>
            <param-value>file:/absolute/path/where/external/logs/are/log4j.properties</param-value>
        </context-param>
        <context-param>
            <param-name>log4jRefreshInterval</param-name>
            <param-value>1000</param-value>
        </context-param>
        <listener>
            <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
        </listener>
        

        您可以指定log4jRefreshInterval,即配置文件刷新检查之间的间隔,以毫秒为单位。


        org.springframework.web.util.Log4jWebConfigurerjavadoc 中查看更多信息。

        【讨论】:

        • 不幸的是,在这个项目中我没有使用弹簧:\
        • 嗯... 只需添加所需的jar文件即可,无需配置Spring。 :)
        猜你喜欢
        • 2020-06-04
        • 2013-03-07
        • 1970-01-01
        • 1970-01-01
        • 2011-12-17
        • 2016-12-11
        • 2011-07-09
        • 2016-06-29
        • 1970-01-01
        相关资源
        最近更新 更多