views:

2371

answers:

9

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

  • Java Logging
  • Commons Logging
  • Log4j
  • SLF4j
  • Logback
+37  A: 

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
Stephen
As far as I can tell the idea behind commons logging is that it should be used in libraries. This way the library can always use the same logging framework (via commons logging) that the hosting application uses.
Joachim Sauer
Thanks heaps to Loki for the question. I now know that I'm not going to be using log4j as my default framework any more. SLF4j FTW!Also thanks to Ken G for pointing out that SLF4j is written by the same guy as log4j
Stephen
The comment in your code example isn't 100% correct.The actual formatting of the message is performed lazily by Logback so it will only happen if the event is really handled by an appender *and* the appender requires the formatted message - which wouldn't happen in case of e.g. an SocketAppender since the event is serialized using the unchanged message pattern + the arguments as Strings.I guess it just depends how you define "effectively" though. It would certainly emit the same message (at least if entry and object are the same ;)) so please forgive my nitpicking.
Huxi
SLF4J actually is just an API that sits on top of other logging frameworks. Similar to the goal of Commons Logging, but more intuitive from my experience.
James McMahon
A: 

Generally I would default to using Log4J.

I would use Java Logging if I didn't mind a dependency on Java 1.4 but I would still use Log4J in preference.

I would use Commons Logging if I was enhancing something that already used it.

Michael Rutherfurd
Didn't mind a dependency on 1.4?? Even 1.4 has hit its end of service life.
Tom Hawtin - tackline
@TomHe means jdk1.4+ dont be silly.
mP
Actually I've recently done some work in a system that was still running under jdk 1.3 :-(, and it was less than two years ago I was last maintaining a **jdk 1.2** system. Too many places don't upgrade unless they absolutely have to, they just refuse to install the upgrades.
Michael Rutherfurd
+1  A: 

I would suggest creating a thin logging facade that can write to any of the logging frameworks, at which point the choice of backing engine become pretty much a moot point.

Travis
That is what commons logging does so why reinvent the wheel? Also there are some issues that your facade would have to handle that commons logging already does.
James A. N. Stauffer
+1 To counter commons logging argument, Some enterprise apps use custom logger. It always a good idea to hide underlying implementation. The 'thin' wrapper eliminates the need for another jar being packaged and is not as much as reinventing.
questzen
This approach is valid in many cases, especially when your code must run on an arbitrary platform. The underlying logging API tends to be different on application servers compared to Eclipse, for example.
McDowell
About hiding the underlying implementation: All the logging APIs mentioned can do just that: They are interfaces to pluggable implementations.
Thilo
This saved me recently when we ported our framework to the Compact Framework (in .Net). If we had hardcoded dependencies on nLog, we would have been screwed. Because we used this approach, we were able to drop in a null logger and be happy.Someone vote me back up to 0 Please :-)
Travis
One already exists - take a look at slf4j. It's an accepted thin wrapper that allows you to switch logging frameworks on application startup by switching jars on the runtime classpath.
deterb
clogging has its own way of discovering what framework it will use - its not nice and rather convuluted. How many logging frameworks has Ceki created. One should always strive to slot a level of interdirection and hide an implementation even if it is clogging with your own logging. Behind the scenes you can then plugin whatever f/w you desire as mentioned by Travis.
mP
+1  A: 

In our company project we use LOG4j and it is very easy to use like Stephen showed in his example. We also have written our own pattern classes for LOG4j so you can create your own output file schemas. You can describe how your log file should look like. It is possible to enhance the original log4j classes.

All LOG4j properties you can change in a log4j.properties file, so you can use different files for different projects.

Java logging is not my favorit, but this could be because i use log4j from the beginning.

Markus Lausberg
+8  A: 

See also answers to the question What are the best practices to log an error?, especially:

  • There are some potential classloading issues with Commons Logging.

  • Log4J and SLF4J were developed by the same person, learning from issues found in practice with Log4J.

Ken Gentle
+2  A: 

The Commons Logging overview gives the reason for its existence: logging from library code, when you have no control over the underlying logging framework. Very important for the various Apache projects, which will be linked into outside applications. Perhaps not so important for internal IT projects, where you have complete control.

That said, I write to Commons Logging, as do many of the other developers I know. The reason is to minimize mental baggage: you can change projects or jobs, and not have to learn a new framework (provided the new job/project also uses CL, and/or you can convince them to move to it).

Also, there is some value to creating your own wrappers around whatever framework you use. As described here, I like to use a LogWrapper object to provide custom stringification (important), and minimize the visual clutter of logging statements (less important).

kdgregory
There's also a commons.logging=>SLF4J bridge which can be used to route all CL logging over SLF4J. SLF4J supports bridging of commons.logging, LOG4J and (a bit cumbersome, but as good as possible) java.util.logging so all logs will end up in whatever SLF4J backend you'll use. See http://slf4j.org/legacy.html I'd use Logback, btw, but you could argue that I'm biased.
Huxi
+2  A: 

I find logging in Java to be confusing, inconsistent, poorly documented, and especially haphazard. Moreover, there is a huge amount of similarity between these logging frameworks resulting in duplication of effort, and confusion as to what logging environment you are actually in. In particular, if you are working in a serious Java web application stack, you are often in multiple logging environments at one time; (e.g hibernate may use log4j, and tomcat java.util.logging). Apache commons is meant to bridge different logging frameworks, but really just adds more complexity. If you do not know this ahead of time, it is utterly bewildering. Why are my log messages not printing out to the console, etc.? Ohh because I am looking at the Tomcat logs, and not log4j. Adding yet another layer of complexity, the application server may have global logging configurations that may not recognize local configurations for a particular web application. Lastly, all these logging frameworks are WAY TOO COMPLICATED. Logging in Java has been a disorganized mess leaving developers like me frustrated and confused.

Early versions of Java did not have a built-in logging framework leading to this scenario.

Julien Chastang
Is this an answer? It looks more like a rant.
Michael Myers
I am sorry. It *is* a little bit of a rant. But it is also a cogent response to "What’s Up with Logging in Java?". The answer, in short, is it is deeply broken.
Julien Chastang
well it's not worth a -1 vote ;P But something to ponder about.
The problems started when Sun actually added java.util.logging to Java 1.4. Before that LOG4J was well established and in wide use. After that there was the necessity of wrappers to support both LOG4J and java.util.logging. Also, since jul is contained in the java.* package, it can't be replaced by swapping a JAR - which is the way SLF4J bridges the other frameworks. This was probably the worst Sun idea ever... and it ultimately led to the wrong assumption that a "good Java citizen" should use jul.
Huxi
A: 

Yes, logging in larger applications can be a pain sometimes. Especially when it comes to multi-JVM and multi-host applications. Sure, log4j is great with its appenders and layouts. But still, you end up developing some logging framework on top of those API's and configurations in order to fit you application. We've developed a product to solve this problem. Direct all log traffic to a standalone log server and it will take care of aggregation, filtering and dispatching to multiple viewers. There is no need to manage log files - you can create them any time in any form and containing any slice of you system. Please have a look here: http://www.moonlit-software.com

Dima
+9  A: 

There's one important point that wasn't mentioned before:

SLF4J (and both Logback and LOG4J as the logging backend) have support for a so called Mapped Diagnostic Context (MDC, see javadoc and documentation).

This is essentially a thread-local Map<String,Object> which you can use to add additional context information to your logging event. The current state of the MDC is attached to every event.

This can be incredibly useful if you put stuff like the username and the URL of the request (in case of a webapp) into it. This can be done automatically using a filter, for example.

Huxi