In one of our projects we are using SLF4J + logback for logging. Is it possible to configure spring-framework to use SLF4J instead of its default log4j?
-
duplicated http://stackoverflow.com/questions/5681218/slf4j-java-util-logging-how-to-configure – Shams May 06 '13 at 14:49
-
@shams - The thread you are referring helps me to configure SLF4J for an application that I am developing. However, my question is more towards configuring SLF4J "for spring-framework logs" rather than "for using in spring-based applications". Anyway, thanks for your help! – Shyam Kumar Sundarakumar May 07 '13 at 05:09
1 Answers
Spring uses apache commons logging by default (not log4J), but its just a wrapper. You need to provide a logging package for it to connect to. Most people seem to recommend replacing apache commons with a different wrapper called SLF4J, and connecting it to the Log4J logger. If you feel that is the one true way to log in spring, the instructions here should work for you: http://spring.io/blog/2009/12/04/logging-dependencies-in-spring
There's an easier way. I just add a logging.properties file to WEB-INF classes, and configure some spring loggers and handlers. A basic logging.properties file is here: https://tomcat.apache.org/tomcat-6.0-doc/logging.html#Using_java.util.logging_(default)
Replace every occurrence of the word "manager" with "spring", add these two lines anywhere in the logging.properties example: org.springframework.level=FINE org.springframework.handlers=3spring.org.apache.juli.FileHandler
And delete these ones: org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager].level = INFO org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager].handlers = \ 3manager.org.apache.juli.FileHandler
Apache commons will find the java.util.logging package in your JDK, and you'll have the spring framework logs under $CATALINA_BASE/conf (or if you're not deploying in tomcat, replace the path in the .properties file).

- 850
- 1
- 9
- 21