Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging an Exception using commons.logging in Java?

I am using commons logging in a Java application, and I want to log the exception from the stack trace.

 catch( IOException exception ) {
    logger.error( "IOException Occured :", exception.fillInStackTrace() ); 

       //Print the entire stack trace to log file.
      throw new AsExceptionUtl( APPMessageHelper.getMessage( APPMessageConstants.ERROR_FailedLoadFile, documentPath ) );
   }

Is this the right way ? And will it print the stacktrace in the log ?

like image 863
Debajyoti Das Avatar asked Dec 14 '12 12:12

Debajyoti Das


People also ask

How do you do common logging in Java?

We need to have the commons-logging. properties in our src folder. The above property tells the Commons Library as to which Logger Class, the application intends to use. Then, we need to set up the corresponding library, Log4J in this case.

Is Commons Logging the same as log4j?

Apache Commons Logging is an abstraction for the concrete implementation. It uses log4j, if present and configured. I would use Commons logging in my code and log4j as logging implementation.

What is the use of Commons Logging?

Apache Commons Logging (previously known as Jakarta Commons Logging or JCL) is a Java-based logging utility and a programming model for logging and for other toolkits. It provides APIs, log implementations, and wrapper implementations over some other tools.

Does slf4j use Commons Logging?

The classes for commons-logging will be provided by jcl-over-slf4j.


2 Answers

If you want to log the exception, then just use

logger.error("IOException Occured :", exception); 

Whether the stack trace will be displayed or not depends on what the underlying logging implementation is, and how it's configured. AFAIK, most or all implementations do display the stack trace of the exceptions by default.

like image 85
JB Nizet Avatar answered Oct 22 '22 02:10

JB Nizet


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.

like image 23
The Dark Knight Avatar answered Oct 22 '22 04:10

The Dark Knight