Spring特定于环境的log4j配置 [英] environment specific log4j configuration by spring
问题描述
我正在使用传统方式加载log4j.xml
I am loading log4j.xml using the traditional way
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:conf/log4j.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
这可以正常工作,但是现在我需要根据环境变量/jndi条目所定义的环境来加载另一个log4j.xml文件.因此,我希望借助新的spring 3.1属性管理,我可以只需将其更改为
This works fine but now I need to load a different log4j.xml file based on which environment I am in which is defined by a environment variable/jndi entry .. so I was hoping that with new spring 3.1 property management I could just change this to
<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>
并且spring将在每个环境中加载正确的log4j文件,但这可能不起作用,因为web.xml是在spring之前加载的.我碰到了这种方法
and spring will load the correct log4j file in each environment but this doesnt works probably because web.xml is loaded before spring. I came across this method
<bean id="log4jInitialization" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetClass" value="org.springframework.util.Log4jConfigurer" />
<property name="targetMethod" value="initLogging" />
<property name="arguments">
<list>
<value>log4j-${ENV-NAME}.xml</value>
</list>
</property>
</bean>
因此从本质上将log4j的配置移动到spring上下文文件而不是web.xml中.但是,由于某种原因,日志以某种方式被记录下来而无法启用,因此这都不起作用.因此,如何使用基于环境变量的其他log4j.xml文件或以编程方式将其加载到servletcontextlistener中.
so essentially moving configuration of log4j into spring context file instead of web.xml. But this doesnt works either for some reason logging gets enabled in a way that everything is logged. So how can I use a different log4j.xml file based on an environment variable or loading it programmatically in the servletcontextlistener.
推荐答案
帮助adeelmahmood为时已晚,但希望其他人将从我的回答中受益. 事情是adeelmahmood是正确的,此配置:
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>
正在工作,但是:
- Log4jConfigListener应该在ContextLoaderListener之前在web.xml中注册!!!
- 您还必须记住Log4jConfigListener假定了扩展的WAR文件.
然后,如果设置了ENV-NAME环境变量,它将按预期工作.
Then, if ENV-NAME environment variable is set, it works as expected.
顺便说一句,$ {ENV-NAME}也可以在spring配置文件中使用!这还不是全部...您还可以使用我们的env.property设置弹簧轮廓:
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>
这样,您可以使用一个和相同的env来设置log4jConfigLocation和spring配置文件.变量.
This way you can set log4jConfigLocation and spring profile, both with one and the same env. variable.
顺便说一句,以防万一:使用Maven配置文件为开发,测试和生产创建单独的版本不是一个好习惯.阅读例如. http://java.dzone.com/articles/maven-profile-best-practices 并记住:使用配置文件来管理构建时变量,而不是运行时变量,并且不能(除了RARE例外)管理工件的替代版本."
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".
这篇关于Spring特定于环境的log4j配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!