spring 环境特定的 log4j 配置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15348550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 05:48:59  来源:igfitidea点击:

environment specific log4j configuration by spring

springlog4j

提问by adeelmahmood

I am loading log4j.xml using the traditional way

我正在使用传统方式加载 log4j.xml

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

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

这工作正常,但现在我需要根据我所在的环境加载不同的 log4j.xml 文件,其中由环境变量/jndi 条目定义..所以我希望通过新的 spring 3.1 属性管理我可以改变这个到

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

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

并且 spring 将在每个环境中加载正确的 log4j 文件,但这可能不起作用,因为 web.xml 在 spring 之前加载。我遇到了这个方法

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

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.

所以基本上将 log4j 的配置移动到 spring 上下文文件而不是 web.xml 中。但这也行不通,因为某种原因,以记录所有内容的方式启用日志记录。那么我如何基于环境变量使用不同的 log4j.xml 文件或在 servletcontextlistener 中以编程方式加载它。

采纳答案by Patrycja K

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

现在帮助 adeelmahmood 为时已晚,但希望其他人能从我的回答中受益。事情是 adeelmahmood 是对的,这个配置:

<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.
  • Log4jConfigListener 应该在 web.xml 中的 ContextLoaderListener 之前注册!!!
  • 您还必须记住 Log4jConfigListener 假定一个扩展的 WAR 文件。

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

然后,如果设置了 ENV-NAME 环境变量,它会按预期工作。

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:

顺便说一句,${ENV-NAME} 也可以在 spring 配置文件中使用!这还不是全部...您还可以使用我们的 env.property 来设置 spring 配置文件:

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

通过这种方式,您可以设置 log4jConfigLocation 和 spring 配置文件,两者都具有相同的 env。多变的。

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-practicesand remember: "Use profiles to manage build-time variables, not run-time variables and not (with RARE exceptions) alternative versions of your artifact".

顺便说一句,以防万一:使用 Maven 配置文件为开发、测试和生产单独构建并不是一个好习惯。阅读例如。http://java.dzone.com/articles/maven-profile-best-practices并记住:“使用配置文件来管理构建时变量,而不是运行时变量,而不是(除了罕见的例外)工件的替代版本” .

回答by maxxyme

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:

这可能对最近的读者也有帮助:在 Log4j 2.7(但它可能更旧)中,参数名称已更改:请参阅界面Log4jWebSupport

/**
 * The {@link javax.servlet.ServletContext} parameter name for the location of the configuration.
 */
String LOG4J_CONFIG_LOCATION = "log4jConfiguration";