Log4J Exceptions – Stack Trace Output and Handling

March 8, 2010

Overview

During my professional career I have become accustomed to the development tools that make the engineer’s job much easier. On the other hand I’ve come to the realization that little time and attention is being spent on optimization and proper use of those tools. While there is plenty of information out there on framework comparisons, guides, APIs, and the like very few products seem to adopt the best practices and the tier two functionality slips through the cracks of the Q&A process. This is usually the case with logging.
Log4J is the status quo when it comes to logging in Java. With the expanded mainstream popularity the framework which has been branched in C++ (Log4cxx) ,  C#/.NET (Log4Net) and even PHP (Log4php) has made it easy to add logging to multi tier applications. The advantage of having a very granular approach with different log levels (DEBUG, INFO, ERROR, etc.) and the Logger package inheritance make it a flexible tool. Without getting into details on the configuration and how to get started I want to spend time on the proper Logger initialization and use for exception handling.

Log4J Logger

As described in the Logger API we can get our Logger initialized inside the class in one of several ways the most common of which are:

- using the Class object

Logger log = Logger.getLogger(getClass());

- using a string

Logger log = Logger.getLogger(getClass().getName());

or

Logger log = Logger.getLogger("com.java.foo.MyClass");

Once we have a logger instance we can easily log messages with different threshold:

Foo foo = new Foo();
log.info("Created object foo: " + foo.toString());

or

if(log.isDebugEnabled())
{
	//do something
	log.debug("Result: " + foo.getResult());
}

Exception handling

It gets more interesting when we get to exception handling. In the regular try catch scenario we usually deal with some exception where we want to collect some output about what happened and where. A lot of times developers are tempted to use exception.getMessage() which is often empty or yet worse call exception.printStackTrace() which actually prints to the standard output rather than our log file. In order to successfully print the exception message and the stack trace we can add the exception to the logger output by doing this instead:

try
{
	computeInterestRates();
}
catch(Exception e)
{
	//something went wrong - log an error
	log.error("Error during RateCalc: ", e);
}

Conclusion

It is easy to get lost in requirements and miss on some of the essential product functionality. The proper use of logging helps reduce and eliminate defects in the lifecycle of the software and be a life saver in the long run. Make sure that you properly handle all logging. Keep in mind that the overuse of logging has a detrimental effect on performance so use it sparingly. For more information see the Log4j Official Documentation.

  • Share/Bookmark

Leave a Reply

You must be logged in to post a comment.