Getting Spring To Read Your Log4j Config
I’m using The Spring Framework to build a new application. Spring is cool — it can do a lot and there is a lot to learn about it. One cool thing I just figured out it logging injection. One thing that took me a while to figure out is to get Spring (and my Spring-configured classes) to use my log4j.xml file instead of the log4j.properties that is hidden somewhere in the Spring jar.
I googled and googled and finally put together a couple of answers and got it to work. So now I’m putting it here so you, gentle reader, don’t have to go through that much work:
<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>classpath:log4j.xml</value>
</list>
</property>
</bean>
Note that this is pretty much a boiler-plate — just copy it into your context configuration and make sure that your log4j.xml file is somewhere on your classpath. Then things will start working wonderfully