Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initializing Log4J with Spring?

I have a web app that uses Spring's Log4jConfigurer class to initialize my Log4J log factory. Basically it initializes Log4J with a config file that is off the class path.

Here is the config:

<bean id="log4jInitializer" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean" depends-on="sbeHome">     <property name="targetClass" value="org.springframework.util.Log4jConfigurer" />     <property name="targetMethod" value="initLogging" />     <property name="arguments">         <list>             <value>#{ MyAppHome + '/conf/log4j.xml'}</value>         </list>     </property> </bean> 

However I get this error at application startup:

log4j:WARN No appenders could be found for logger

and tons of Spring application context initialization messages are printed to the console. I think this is because Spring is doing work to initialize my application before it has a chance to initialize my logger. In case it matters, I am using SLF4J on top of Log4J.

Is there some way I can get my Log4jConfigurer to be the first bean initialized? or is there some other way to solve this?

like image 713
HDave Avatar asked Dec 09 '10 16:12

HDave


People also ask

Does spring boot support log4j?

Spring Boot also supports either Log4j or Log4j 2 for logging configuration, but only if one of them is on the classpath. If you are using the starter poms for assembling dependencies that means you have to exclude Logback and then include your chosen version of Log4j instead.


1 Answers

You could configure your Log4j listener in the web.xml instead of the spring-context.xml

<context-param>     <param-name>log4jConfigLocation</param-name>     <param-value>/WEB-INF/classes/log4j.web.properties</param-value> </context-param>  <listener>     <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener> 

So it is up before Spring starts.

like image 98
Ralph Avatar answered Sep 26 '22 08:09

Ralph