通过spring配置特定环境的log4j[英] environment specific log4j configuration by spring

本文是小编为大家收集整理的关于通过spring配置特定环境的log4j的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在使用传统方式加载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>

这可以正常工作,但是现在我需要基于我所在的环境来加载其他log4j.xml文件,由环境变量/jndi条目定义.因此,我希望有了新的Spring 3.1 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>

spring会在每个环境中加载正确的log4j文件,但这可能没有起作用,这可能是因为web.xml在弹簧之前加载.我遇到了这种方法

<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的配置移至春季上下文文件而不是web.xml.但这是由于某种原因无法以某种方式启用日志记录的方式.因此,如何根据环境变量使用其他log4j.xml文件或在ServletContextListener中编程加载.

推荐答案

现在帮助Adeelmahmood为时已晚,但希望其他人能从我的答案中受益. 事情是Adelmahmood正确,此配置:

<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应该在web.xml中的上下文loaderListener之前注册!!!
  • 您还必须记住,log4jconfiglistener假定了一个扩展的战争文件.

然后,如果设置了Env-Name环境变量,则可以按预期工作.

顺便说一句,$ {env-name}也可以在Spring Config Files中使用!那不是一切...您还可以使用我们的Env.Property设置春季配置文件:

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

这样,您可以使用一个和同一env设置log4jconfiglocation和Spring配置文件.变量.

顺便说一句,以防万一:使用Maven配置文件为开发,测试和生产具有单独的构建不是很好的做法.阅读,例如. http://java.dzone.com/articles/articles/maven-profile-best-practices 记住:"使用配置文件来管理构建时间变量,而不是运行时变量,而不是(除了极少数例外)伪影的替代版本.

其他推荐答案

这对较新的读者可能也有帮助:在log4j 2.7中(但可能年龄较大),参数名称已更改: 请参阅接口Log4jWebSupport:

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

其他推荐答案

默认情况下,Spring Boot从其系统的默认位置拾取本机配置(例如classpath:logback.xml for logBack),但是您可以使用logging.config property设置配置文件的位置.

logging.config=classpath:log4j-<ENV-NAME>.xml

html/howto-logging.html" rel="nore="noflowl noreflowl norefloll/Docs.spring.io/spring-boot/docs/2.0.0.release/referene/html/howto-logging.html

本文地址:https://www.itbaoku.cn/post/1574864.html

问题描述

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>

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>

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>

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.

推荐答案

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

其他推荐答案

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

其他推荐答案

By default, Spring Boot picks up the native configuration from its default location for the system (such as classpath:logback.xml for Logback), but you can set the location of the config file by using the logging.config property.

logging.config=classpath:log4j-<ENV-NAME>.xml

https://docs.spring.io/spring-boot/docs/2.0.0.RELEASE/reference/html/howto-logging.html