In addition to Jatin's answer:
Spring uses Jakarta Commons Logging as a logging API. In order to log to slf4j, you need to make sure commons-logging
is not on the classpath. jcl-over-slf4j
is a replacement jar for commons-logging.
If you're using maven, you can detect where commons-logging comes from using mvn dependency:tree
and exclude it from all dependencies that require it using dependency exclusions. You might need to run mvn dependency:tree
several times though, because it only shows the first occurence of a transitive dependency.
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework.version}</version>
<exclusions>
<exclusion>
<artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId>
</exclusion>
</exclusions>
</dependency>
Those messages are something tricky, enough so that people created this to make it clearer:
https://issues.apache.org/bugzilla/show_bug.cgi?id=25747
What's tricky about them is that the warnings are written if Log4j can't find its log4j.properties
(or log4j.xml
) file, but also if the file is fine and dandy but its content is not complete from a configuration point of view.
The following paragraph is taken from here:
http://svn.apache.org/repos/asf/logging/log4j/tags/v1_2_9/docs/TROUBLESHOOT.html
Logging output is written to a target by using an appender. If no appenders are attached to a category nor to any of its ancestors, you will get the following message when trying to log:
log4j: No appenders could be found for category (some.category.name).
log4j: Please initialize the log4j system properly.
Log4j does not have a default logging target. It is the user's responsibility to ensure that all categories can inherit an appender. This can be easily achieved by attaching an appender to the root category.
You can find info on how to configure the root logger (log4j.rootLogger
) in the log4j documentation, basically adding something as simple as this at the beginning of the file:
log4j.rootLogger=debug, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
This should clear those WARN messages you get on startup (make sure you don't already have an appender named stdout
; also be carefull of what level you give the root logger, debug
will be very verbose and every library in your app will start writing stuff to the console).
As about the log4j.properties
/log4j.xml
, I suggest you place this file in /WEB-INF/classes
as it is important to have it exposed for different tweaks (activating/deactivating logs, changing log levels etc). You can have it inside a JAR in the classpath also (as you specified in your comment), but it will be enclosed in the archive (hopefully in the right place inside the archive) and won't be as easy to handle as if it were in /WEB-INF/classes
.
Best Answer
Log4j
by default looks for a file calledlog4j.properties
orlog4j.xml
on the classpath.You can control which file it uses to initialize itself by setting system properties as described here (Look for the "Default Initialization Procedure" section).
For example:
Will cause
log4j
to look for a file called customName on the classpath.If you are having problems I find it helpful to turn on the log4j.debug:
It will print to System.out lots of helpful information about which file it used to initialize itself, which loggers / appenders got configured and how etc.
The configuration file can be a java properties file or an xml file. Here is a sample of the properties file format taken from the log4j intro documentation page: