5

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 ?

1
  • I know this is extremely old, but... Where do these logs get written to? I am trying to debug an issue and I can't find these stupid logs anywhere. Commented Feb 6, 2019 at 1:42

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

3 Comments

+1 the stack trace is filled in as required and I can't think of a good reason to call it explicitly unless you want to change the stack trace (which I have done in one of my libraries but that does make it a good idea generally :)
@peter-lawrey @jb-nizet So what will exception.fillInStackTrace() do in my case.
Nothing, since the constructor of Throwable calls fillInStackTrace() already. The stack trace is there by default. Don't worry about it.
2

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.

1 Comment

Logging an exception this way does not necessarily log the exception's stack trace, it can just put the message in the log, e.g. "IOException Occured :Socket closed" and that's all you get.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.