environment specific log4j configuration by spring

前端 未结 2 1419
感情败类
感情败类 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:

    
        log4jConfigLocation
        classpath:conf/log4j-${ENV-NAME}.xml
    
    
        org.springframework.web.util.Log4jConfigListener
    
    

    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:

    
     spring.profiles.active
     ${ENV-NAME}
    
    

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

提交回复
热议问题