set an absolute path for a “log4j.properties” file

天涯浪子 提交于 2019-12-23 04:09:13

问题


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?


回答1:


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

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>

I got this from this listener code from answer .

I hope this could help you!




回答2:


You can use a jvm parameter indicating the configuration file path:

-Dlog4j.configuration=absolute path

example with an absolute path:

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



回答3:


Set the system property log4j.configuration=/abslute/or/relative/path_to_file_name

commons logging only needs to know what logging implementation it's using, the logging implementation it self is configured in whatever way it is always configured in.

This is documented in the log4j manual.




回答4:


If you are using the Spring Framework, you can use the org.springframework.web.util.Log4jConfigListener.

You only need to specify the parameter log4jConfigLocation with the file location (using the URL for File). e.g.:

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

You can specify the log4jRefreshInterval, the interval between config file refresh checks, in milliseconds.


See more in the javadoc of org.springframework.web.util.Log4jWebConfigurer.



来源:https://stackoverflow.com/questions/27123578/set-an-absolute-path-for-a-log4j-properties-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!