Buscar este blog

domingo, 8 de noviembre de 2015

Externalize log4j configuration file in webapp with spring config property

In a previous post (Externalize log4j configuration file in webapp) I explained how to externalize log4j.xml file by using org.springframework.util.Log4jConfigurer inside web.xml. This solution has a drawback, and it's that you need to use a system property wich points to the external file.

In most cases it is more suitable to have a property inside a config file, just like the rest o properties used in the app. This is accomplished with org.springframework.beans.factory.config.MethodInvokingFactoryBean. This class is able to invoke a method of another class, i.e, Log4jConfigurer and you can do it after your config file would be loaded.

The complete solution would be as follow:
<context:property-placeholder properties-ref="properties" order="1" />
<bean id="properties"  class="org.springframework.beans.factory.config.PropertiesFactoryBean">
 <property name="locations">
  <list>
   <value>classpath:conf.properties</value>
  </list>
 </property>
</bean>



<bean id="log4jInitializer" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean" depends-on="properties">
 <property name="targetClass" value="org.springframework.util.Log4jConfigurer" />
 <property name="targetMethod" value="initLogging" />
 <property name="arguments">
  <list>
   <value>${customProperty.log4j.location:classpath:log4j.xml}</value>   
  </list>
 </property>
</bean>

No hay comentarios:

Publicar un comentario