Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

environment specific log4j configuration by spring

Tags:

spring

log4j

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.

like image 550
adeelmahmood Avatar asked Mar 11 '13 21:03

adeelmahmood


People also ask

Where is log4j configuration file?

The file is named log4j. properties and is located in the $DGRAPH_HOME/dgraph-hdfs-agent/lib directory. The file defines the ROLLINGFILE appenders for the root logger and also sets the log level for the file.

How load external log4j properties file in spring boot?

How I could do that using Spring-Boot? Adding -Dlog4j. configuration=file:/path/to/log4j. properties to the command line should also work in Spring Boot.


1 Answers

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

like image 56
Patrycja K Avatar answered Oct 27 '22 10:10

Patrycja K