Logging in Java – which one to use?

Recently I got into a situation where I needed to choose a logging API or library to be used in a Java/J2ee based enterprise s/w platform layer.

There are a number of choices available for this, all of which pretty much do the same thing.

  • Apache Commons Logging (ACL)
  • Java Util Logging (JUL)
  • Simple Logging Facade for Java (SL4J)
  • Log4J

Firstly these all logging APIs basically provide

  • Ability to define multiple loggers for an application identifed by some namespace
  • Ability to define different logging levels for different loggers from configuration
  • Ability to change the logging destination without effecting code.

Here is a little detail on why there are so many logging options, and when to chose which.

Log4J was the first widely adopted logging API. It became very popular and was widely used. http://logging.apache.org/log4j/1.2/

Java Util Logging then came along and thus logging became a core Java feature. This kind of made Log4J unnecessary. However the API is slightly different in terms of the log levels, and input arguments – so a lot of people continue to use Log4J. A lot of new applications use Java Util Logging since it does not need any additional libraries etc. Neat. http://docs.oracle.com/javase/1.4.2/docs/api/java/util/logging/package-summary.html

Apache Commons Logging provided a abstract logging interface over the logging APIs. So applications can write to ACL APIs which in turn could be configured to use either Java Util or Log4J at deployment/run time. Applications (such as libraries) which cared about the need for such flexibility chose Apache Commons Logging. However ACL relied on some rather difficult understand classloader hacks which caused some applications to crash for hard to debug reasons! http://commons.apache.org/logging/

SL4J then came along and seems to fix some of these issues. It provides a lightweight facade over the logging APIs in a straight-forward manner (without any classpath magic). It also offers bridges to route logs written by components directly to other logging APIs into the SL4J logs. http://www.slf4j.org/

I feel moving forward applications should chose between Java Util Logging and SL4J. JUL is good enough for most use cases. If you are writing libraries etc and really care a lot about keeping your code abstractes from implementation APIs – then you may want to use SL4J.




About saratnathb

Building SOA solutions using Oracle Fusion Middleware technology stack.
This entry was posted in Uncategorized and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s