What's Up with Logging in Java?

Loki picture Loki · Dec 10, 2008 · Viewed 12.5k times · Source

Why one would use one of the following packages instead of the other?

  • Java Logging
  • Commons Logging
  • Log4j
  • SLF4j
  • Logback

Answer

Stephen picture Stephen · Dec 10, 2008

In chronological order of api apperance (as far as I know):

  • Log4j because most everybody uses it (in my experience)
  • Commons Logging because open source projects use it (so they can integrate with whatever logging framework is used in the integrated solution); especially valid if you're an API/Framework/OSS and you rely on other packages that use Commons Logging.
  • Commons Logging because you don't want to "lock down" to a particular logging framework (so instead you lock down to what Commons Logging gives you instead) - I don't think it is sensible to decide using this point as the reason.
  • Java logging because you don't want to add in an extra jar.
  • SLF4j because it's newer than Commons Logging and provides parameterized logging:

logger.debug("The entry is {}.", entry);
//which expands effectively to
if (logger.isDebugEnabled()){
    // Note that it's actually *more* efficient than this - see Huxi's comment below...
    logger.debug("The entry is " + entry + "."); 
}
  • Logback because it's newer than log4j and again, supports parameterized logging, as it implements SLF4j directly
  • SLF4j/Logback because it's written by the same guy who did log4j, so he's made it better (according to Ken G - thanks. It seems to fit when looking at their earlier news posts)
  • SLF4j because they also publish a log4j adapter so you don't have to "switch out" log4j in older code - just make log4j.properties use SLF4j and it's configuration