Proper use of LOG4J in Spring Framework via DI

I am trying to use Log4j as part of the Spring Framework, as I understand it, using a suitable bean, the system should display a singleton instance available in the code when automatically comparing the logging depth with the class

Similar to the normal use of Log4J, as in

Logger log = Logger.getLogger(getClass());

I used the following Spring bean definition

 <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>conf\log4j.xml</value> </list> </property> </bean> 

But I cannot match this bean with a specific member in this class and I cannot use it through @autowired

Please let me know if there are any more efficient ways to integrate Log4j and Spring

Best wishes

Mark

+6
java spring log4j
source share
1 answer

The short answer to your question is that log4j is not welcomed by DI.

The Log4jConfigurer.initLogging() method has a void return value, so nothing needs to be entered. The idea is that you call this method, which loads log4j, and then you use the Log4j API as usual (using Logger.getLogger(getClass()) ).

Usually you did not configure Log4jConfigurer as a Spring bean, but most often you called it directly from your own code at application startup time.

If this is a webapp, then Spring provides Log4jConfigurer alternatives that are better suited for this environment ( Log4jWebConfigurer , Log4jConfigListener ).

By the way, 2 years ago I submitted a request to allow startups of logs, and finally it was flagged as a fix for Spring 3.1. Horray

+6
source share

All Articles