views:

4302

answers:

2

I have some questions about logging, more specifically about setting it up and making sure it works.

The project I'm doing will use Wicket, Spring and Hibernate. I know that Wicket and Hibernate uses Simple Logging Facade for Java (SL4J) and that Spring is using the logging component from Apache Commons.

Will they co-exist happily? I thought I would use log4j together with both SL4J and the logging component from Apache commons, do you think that's a good idea?

Can I set up them all to output logging data into a common file? Or should I use separate files? Or should I store the logging messages in the database? (I'd rather not, as I find grepping etc on text files quite convenient.)

For Spring I guess I need some kind of configuration file for the Apache Commons logging component as well where I direct it to use log4j?

When I've set these up I guess to see that everything works I set the logging level to INFO as it's fairly certain that all three of the frameworks output some information in that mode? Or is there an even better way to make sure?

And my last question. In the project I'm starting, do you recommend that I use SL4J for my own logging purposes? (I thought I would use log4j directly, but that was before I learned a little bit more about logging and a lot of respectable libraries seem to choose the path of a bridge/facade for their logging needs. And if it gets us flexibility without added cost there's no reason not to do it that way.)

I'm looking forward to hearing more from you about how you are doing your logging. It's a new area for me which I'm eager to improve myself in.

+8  A: 

Well SLF4J is just a facade, like commons logging, which means they still need something else to work. They permit library authors to not force users into having multiple logging library and configuration. Log4j and logback are regular logging libs.

See here for more info.

SLF4J has a commons logging bridge that you can use to replace the commons logging library. I think the schema there explain the situation very well.

Now, you just need to use slf4j-logj12.jar to have commons logging and slf4j use log4j (or anything else you chose; btw, logback doesn't need an additional library to be used with slf4j) as a backing engine.

You application will thus have

  • jcl104-over-slf4j.jar (to bridge jakarta commons logging to slf4j)
  • slf4j.jar (for hibernate and others to use slf4j)
  • slf4j-logj12.jar (for slf4j to use log4j as a backend)
  • log4j.jar (for your application to use. all config will also be done here)
Loki
Remember to not confuse slf4j-xxxx.jar with xxxx-over-slf4j.jar. The former is for funneling to your desired logging framework (log4j, logback, etc), the latter is to replace the used logging framework.
Spencer K
And using both can create a breach in the space-time fabric!
Loki
Stop the madness. Does anybody else think that logging has become far too complex? I thought log4j was a terrific thing, worthy of being made a standard. I didn't follow the JCP history closely enough to know why Sun decided to go their own way. This is ridiculous. I use ACL + log4j.
duffymo
+7  A: 

Here is how to redirect everything to SLF4J:

  • remove commons-logging.jar from your classpath. If you are using Maven and have trouble getting rid of commons-logging, see this.

  • put jcl-over-slf4j.jar in your classpath (it comes in the SLF4J distribution). This is a drop-in replacement that mimics JCL's classes, but calls SLF4J internally. This will take care of Spring, and any other framework that uses JCL.

Connect SLF4J to your favorite backend (Log4J, Logback...) by putting slf4j-xxx.jar in the classpath. Configure the backend to log all categories to one file, and you're done.

As for using SLF4J in your application, it is not strictly necessary. Libraries like JCL and SLF4J were originally designed for people who write libraries and do no want to lock their clients into a particular logging framework.

PS: by the way, JCL = Jakarta Commons Logging

Olivier
Thanks! I changed to JCL in the OP.
DeletedAccount
What do you mean by "onfigure the backend to log all categories to one file"? Could you provide more details?
Glenn
In order to exclude commons-logging from your final build in maven use scope 'provided' instead of the 99.0-does-not-exist from Eric.
Gareth Davis