environment specific log4j configuration by spring

前端 未结 2 1373
感情败类
感情败类 2021-02-05 09:24

I am loading log4j.xml using the traditional way


    log4jConfigLocation
    classp         


        
相关标签:
2条回答
  • 2021-02-05 09:56

    It's too late to help adeelmahmood, but hope others will benefit from my answer. The thing is adeelmahmood was right, this configuration:

    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>classpath:conf/log4j-${ENV-NAME}.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>
    

    is working, but:

    • Log4jConfigListener should be registered before ContextLoaderListener in web.xml !!!
    • You must also remember that Log4jConfigListener assumes an expanded WAR file.

    Then, if ENV-NAME environment variable is set, it works as expected.

    By the way, ${ENV-NAME} can be used in spring config files as well! That's not everything... You can also use our env.property to set spring profile:

    <context-param>
     <param-name>spring.profiles.active</param-name>
     <param-value>${ENV-NAME}</param-value>
    </context-param>
    

    This way you can set log4jConfigLocation and spring profile, both with one and the same env. variable.

    BTW and just in case: using Maven profiles to have separate builds for dev, test and production isn't good practice. Read eg. http://java.dzone.com/articles/maven-profile-best-practices and remember: "Use profiles to manage build-time variables, not run-time variables and not (with RARE exceptions) alternative versions of your artifact".

    0 讨论(0)
  • 2021-02-05 10:03

    This might help too for more recent readers: in Log4j 2.7 (but it's probably older) the parameter name has been altered: see interface Log4jWebSupport :

    /**
     * The {@link javax.servlet.ServletContext} parameter name for the location of the configuration.
     */
    String LOG4J_CONFIG_LOCATION = "log4jConfiguration";
    
    0 讨论(0)
提交回复
热议问题