title: environment specific log4j configuration by spring
body: <p>I am loading log4j.xml using the traditional way</p>
  
  <pre><code>&lt;context-param&gt;
      &lt;param-name&gt;log4jConfigLocation&lt;/param-name&gt;
      &lt;param-value&gt;classpath:conf/log4j.xml&lt;/param-value&gt;
  &lt;/context-param&gt;
  &lt;listener&gt;
      &lt;listener-class&gt;org.springframework.web.util.Log4jConfigListener&lt;/listener-class&gt;
  &lt;/listener&gt;
  </code></pre>
  
  <p>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</p>
  
  <pre><code>&lt;context-param&gt;
      &lt;param-name&gt;log4jConfigLocation&lt;/param-name&gt;
      &lt;param-value&gt;classpath:conf/log4j-${ENV-NAME}.xml&lt;/param-value&gt;
  &lt;/context-param&gt;
  &lt;listener&gt;
      &lt;listener-class&gt;org.springframework.web.util.Log4jConfigListener&lt;/listener-class&gt;
  &lt;/listener&gt;
  </code></pre>
  
  <p>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</p>
  
  <pre><code>&lt;bean id="log4jInitialization" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"&gt;
  &lt;property name="targetClass" value="org.springframework.util.Log4jConfigurer" /&gt;
  &lt;property name="targetMethod" value="initLogging" /&gt;
  &lt;property name="arguments"&gt;
   &lt;list&gt;
    &lt;value&gt;log4j-${ENV-NAME}.xml&lt;/value&gt;
   &lt;/list&gt;
  &lt;/property&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>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.</p>