Thursday, March 22, 2012

When to use Apache Commons-Logging?

I recently had a requirement to introduce Logging to a couple of Java projects. I chose Apache Commons Logging and I want to share my rationale for selecting it. 

I will talk about a couple of scenarios where I found commons-logging as the Best framework for the job. I hope the examples described in the forthcoming paragraphs will help you decide when you should use commons-logging.


Background:
Apache Commons Logging is NOT a logging framework, but it is 'a bridge between different logging implementations'.  It offers a clean, uniform interface for Logging within your application. You don't have to write framework specific code for logging while developing your application.

So in Java analogy, it as an Interface, not a Class. You can choose your own log implementation (i.e. the logging framework of your choice). Your code will not need to be aware of it. Very minimal configuration is required to instruct Commons Logging to use the underlying Logging framework.

Coming to the scenarios that I want to discuss: There are two applications. Let us call them App1 and App2. Here is a brief introduction of them:

App1:
It is a command line ETL tool to fetch a file from FTP and extract, transform and load data into a Database. It reads the configuration from a properties file (using Apache commons-configuration).

App2:
It is an API that will be distributed as a JAR file to the clients, so that they can integrate the encapsulated functionality in their existing applications/products.

Now, Here comes the important question
Why commons-logging?
Here are my reasons/answers:
  1. App1 already uses Apache Commons-configuration which depends on commons-logging. So, it became my first choice.
  2. App2 being an API will co-exist with external applications which might be using their own logging frameworks. So, by using commons-logging I would let the API consumer decide the logging framework of their choice and put just the configuration file.




So, my next job was to decide the underlying log implementation. I needed to make a choice only for App1, since for App2 I am leaving that for the API user to decide.

Out of various implementations that commons-logging supports (like log4j, Avalon LogKit, SimpleLog, JDK log etc.), I chose JDK logging (aka JavaSE logging). 

The next  important question
Why Java SE Logging?
Answer:
It satisfies my requirements without adding any new Dependency to this small application where logging scope is very limited.

In my Next post I will explain:
  1. How to use 'Apache Commons Logging' with 'Java SE logging'? and
  2. Why NOT Java SE Logging?
HTH!

4 comments:

Unknown said...

Nice post. Waiting for your next one.

Rushikesh S. Thakkar said...

Thanks for visiting Maaz! I am glad you liked it. I will surely post the next one. Keep watching!

Utsav Jimulia said...

Good one Rushi...keep going

Rushikesh S. Thakkar said...

Thanks Utsav! Keep visiting..