Log4J exception example - How to print the stack trace of an exception using Log4J (or Commons Logging)
Di Muso (del 22/06/2011 @ 21:11:46, in Informatica, linkato 4713 volte)

Log4J exception FAQ: "How do I print the stack trace of an exception using Log4J or Commons Logging?"

Printing the stack trace of a Log4J exception seems to be something of a trick question. In reviewing Java code from different developers at different organizations I see a lot of people working very hard to print a stack trace using Log4J, including a lot of variations of calling e.printStackTrace() method.

Log4J exception stack trace - short answer

The short answer is that all you have to do to print the stack trace of an exception using Java and Log4J (or the Apache Commons Logging project) is this:

log.error("Your description here", exception); 

where exception is your Java Exception object. The Log4j error method that takes a description followed by a Throwable will print the stack trace of the Java Throwable object.

For more information on how this works with Log4J I recommend looking at the API documentation for the Logger class.