【问题标题】:environment specific log4j configuration by springspring 的环境特定 log4j 配置
【发布时间】:2013-03-11 21:02:07
【问题描述】:

我正在使用传统方式加载 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 文件只需将其更改为

<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 之前加载。我遇到了这种方法

<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 中以编程方式加载它。

【问题讨论】:

  • 你也在用maven吗?
  • @Jintian DENG 是的,我是
  • 那我建议使用maven配置文件。看看这个:stackoverflow.com/questions/9543219/…
  • 我刚刚以编程方式配置它 public void contextInitialized(ServletContextEvent sce) { String env = ResourceUtils.getEnvironment(); //为此环境加载log4j配置文件 String log4jFile = "/WEB-INF/classes/conf/log4j-" + env.toLowerCase() + ".xml"; DOMConfigurator.configure(sce.getServletContext().getRealPath(log4jFile));

标签: spring log4j


【解决方案1】:

现在帮助 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>

正在工作,但是:

  • Lo​​g4jConfigListener 应该在 web.xml 中的 ContextLoaderListener 之前注册!!!
  • 您还必须记住,Log4jConfigListener 假定一个扩展的 WAR 文件。

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

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

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

通过这种方式,您可以设置 log4jConfigLocation 和 spring 配置文件,它们都具有一个相同的 env。变量。

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

【讨论】:

  • 感谢您回来做这件事,给了我在我的应用程序中工作所需的信息。
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

    默认情况下,Spring Boot 会从系统的默认位置获取本机配置(例如 classpath:logback.xml 用于 Logback),但您可以使用 logging.config 属性设置配置文件的位置。

    logging.config=classpath:log4j-&lt;ENV-NAME&gt;.xml

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

    【讨论】:

      猜你喜欢
      • 2013-08-16
      • 2014-09-07
      • 2015-09-13
      • 1970-01-01
      • 2014-04-25
      • 1970-01-01
      • 1970-01-01
      • 2017-12-08
      • 1970-01-01
      相关资源
      最近更新 更多