views:

1796

answers:

4

Hi,

We are developing a web-based application in Java using the Spring framework. We are wondering which Logging system would be the most appropriate for it, whether Log4j or JUL (java.util.Logging), which is integrated with jdk. As far as I'm concerned, the former is more popular among developers and offers higher customization options, but I'm not sure which is simpler to adapt with spring.

any help will be appreciated.

thanks!

+3  A: 

Before you start with log4j, look at logback. Log4j shouldn't be used for new projects anymore. If you have legacy code that needs any logging framework, use slf4j to make the old code talk to logback (or log4j if you must).

The main reasons you should not use JUL is that it implements the bare minimum that you need for logging, the configuration is hard at best and, if you use any component that doesn't use it, you'll have to deal with two logging frameworks.

Aaron Digulla
Switching logging frameworks is highly overrated! There is nothing wrong with Log4j, it supports a wonderful range of file writing options and does the job it needs to.
Paul Keeble
"Please turn on Javascript to view this menu" - sorry logback, you fail.
Michael Borgwardt
He's not switching, he's looking at his first logging framework. In this case, he shouldn't start with an oldtimer.
Aaron Digulla
@Michael: I suggest to tell that to the author. I can't really do anything about that :) And since I'm using Maven, I don't really care, either.
Aaron Digulla
thanks guys. for now and due do the fact that some people here already worked with log4j we are gonna try with it. I've suggested to use logback but you know how reluctant developers are to try new things when time runs out. however, it isn't completely discarded yet. just testing the options for now
markitus82
+3  A: 

Regardless of which logging framework you use in your own code, as far as I can remember, Spring has a hard dependency on commons-logging. You can still use whatever you like for your own logging (I'd recommend SLF4J as your facade, with logback as your implementation), but Spring internally needs commons-logging. Whether you want to depend on multiple frameworks is your choice; it shouldn't prove problematic.

GaryF
To make it clear: slf4j comes with an adapter for commons-logging, so you don't need the commons-logging.jar in your project. See http://www.slf4j.org/legacy.html
Aaron Digulla
+2  A: 

Spring uses commons logging, which is a log abstraction facility but has issues that sl4j doesn't have. Hibernate moved to slf4j - I would like to see spring do the same, but I don't think they have any plans in this regard.

So as long as your log abstraction facility (use slf4j) logs to the same logging framework as you've configured commons logging in spring, then you're good. One log configuration for both. You might find an adapter for logback for commons and slf4j.

Michael Wiles
Commons Logging is easy to mock, so SLF4J provides a Commons Logging facade that sits on top of the usual SLF4J API. In this way you can use Spring with SLF4J (and Logback).
hbunny
+1  A: 

Since the other responses didn't actually answer your question, I thought I'd have a stab at it.

Firstly, java.util.logging is horrible. It's a pain to use, and a pain to configure. Log4J is nicer on both counts. However, you should pick whichever one you're most comfortable with. Spring uses commons-logging, which in turn will use either log4j (if found on the classpath) or java.util.logging otherwise. In otherwords, if your application already has log4j present, then Spring will effectively use that, and so should you. If log4j is not already present, then either use java.util.logging (if you choose to), or add log4j to your classpath and use that.

skaffman
thanks skaffman, for now we are trying with log4j, JUL really looks a bit tricky.
markitus82