I've got a maven & spring app that I want logging in. I'm keen to use SLF4J.
I want to put all my config files into a directory {classpath}/config including log4j.xml and then init using a spring bean.
e.g.
<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:config/log4j.xml</value>
</list>
</property>
</bean>
However I get this warning and no logging.
log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
I've googled around and can't find a simple example on setting this up. Any ideas?
Just for the sake of completeness, a
logback-classic
variant:Do not forget however to disable
commons-logging
dependency which sprouts from Spring dependency like in the accepted (Stijn's) answer.Use blow configuration for implementation of the
JCL API
on theclasspath
:for More information check here
You'll find an example at https://github.com/mbogoevici/spring-samples/tree/master/mvc-basic/trunk. You need to include some dependencies in your POM file to enable logging.
Just add
lazy-init="false"
to eagerly load the bean for log4j configuration in your root context. That should solve the WARN messagelog4j:WARN No appenders could be found for logger
Example:
A more better approach would be to have the configuration in web.xml or as a JVM parameter (
-Dlog4j.configuration=.../conf/log4j.xml
or with 'file:' prefix as-Dlog4j.configuration=file:conf/log4j.properties
for some cases)keep log4j file in default package
I like the logback way, and for slf4j, we do the similar config:
slf4j-log4j12 will automatically introduce slf4j-api and log4j, so don't need to put so many dependencies