Tuesday, September 4, 2007

Switching from LOG4J to SLF4J

I have already mentioned that I was going to give SLF4J a try. Now, I am going to summ up what it took for me to switch from LOG4J to SLF4J logging library in a few items for maven based web project:
  1. discarded dependencies: commons-logging (1.0.4) and log4j (1.2.8)
  2. added new dependencies: slf4j-api (1.4.3), logback-core (0.9.8) and logback-classic (0.9.8)
  3. added one more dependency: jcl104-over-slf4j (1.4.3)
  4. created a logging configuration file logback.xml in the root of classpath (replacement for log4j.properties)
That's it!
Of course, I had to modify java code so that it uses Logger instead of Log now.

As for the item #3 it deserves a little bit more clarification. Since my final war is going to be deployed in Tomcat bundled with several other jar libraries (mostly apache commons, Spring and great Stripes framework) then dependency on JCL still resists. The solution to this problem is jcl104-over-slf4j.jar which implements the public API of JCL but using SLF4J underneath [see slf4j manual].

4 comments:

3rojka said...

What is the reason that I wanna swith from LOG4J to SLF4J, any advantages or so?

Tomáš Hubálek said...

If you use NetBeans platform then Jackpot module can do all the refactoring for you (just write two simple rules according tutorial) and change in the project is piece of cake ;-)

Lukas said...

3rojka, regarding your question about the reason I recommend you to look at the SLF4J web pages and dig for yourself. For short I can recommend two points from FAQ section:
http://www.slf4j.org/faq.html#yet_another_facade and http://www.slf4j.org/faq.html#why_new_project By far these are not the only reasons...
Anyway, what is the reason for not switching if it is so easy?

Alno said...

You don't really need to modify your existing code, you may simpli use log4j-over-slf4j module, as described at http://www.slf4j.org/log4j-over-slf4j.html