RSS Feed for This PostCurrent Article

Java: Log Exception Correctly with Log4j

This is simple but often overlooked.

There are numerous code like this

   1: try {
   2:   ….
   3: }
   4: catch (Exception e) {
   5:    e.printStackTrace();
   6: }

printStatckTrace prints out to “stderr”. If you are using log4j and you should do this

   1: try {
   2:
   3: }
   4: catch (Exception e) {
   5:   log.error(“Exception details “, e);
   6: }

For every debug, info, warn, error and fatal methods there is a corresponding method that accepts a Throwable as the second parameter.


Trackback URL


RSS Feed for This PostPost a Comment

CAPTCHA Image
Refresh Image
*