Apache commons logging explained : There is not much documentation on this available on internet. This is a small article for your benefit :
In order to log messages in your java code, you will need to import two classes into your source code.
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
Now, to create your log, create an attribute in your class in one of two ways:
private Log m_log = LogFactory.getLog(MyClass.class);
or
private Log m_log = LogFactory.getLog("MyClassLogger");
The first option is just creating a generic logger for your class, which will be controlled by the default logging options.
The second option is creating a specific logger which you have named ‘MyClassLogger’, that can be controlled individually to the defaults. You may want to do this if you use other third party source code that uses logging but you do not want to see the debugs or other information from that source code.
Using the logger is pretty straight forward. You can send log messages by calling a method corresponding to priority:
m_log.fatal(Object message);
m_log.fatal(Object message, Throwable t);
m_log.error(Object message);
m_log.error(Object message, Throwable t);
m_log.warn(Object message);
m_log.warn(Object message, Throwable t);
m_log.info(Object message);
m_log.info(Object message, Throwable t);
m_log.debug(Object message);
m_log.debug(Object message, Throwable t);
m_log.trace(Object message);
m_log.trace(Object message, Throwable t);
These methods are listed in order of priority from highest to lowest. Commons logging, by default, is set to display all messages from INFO and higher. As you can see, each method is overloaded with a method where you can send a Throwable type, such as an Exception – very handy!
That’s all you have to do to log the messages.
In your case, you just need to use :
logger.error("IOException Occured :", exception);
There is no need to call stackTrace as the exception object here will be logged with the exception thrown.