Friday, 15 February 2008

Logging A StackTrace Exception

Logging an error seems to be a very easy job to do, but one of the common mistakes that Java programmers make is using the wrong method. Consider this piece of code:
try {
..
}
catch (Exception e) {
LOG.error(e);
}

Can you see what is really going on? If you have a look at commons logging's API, there are 2 error methods in the Log interface. All the above code does is it logs e.toString() to the error log, which means the error stack trace will be lost! Here is what you need to do instead:
try {
..
}
catch (Exception e) {
LOG.error("problem...", e);
}

1 comment:

  1. Nice tips, thx ! Hope to read another soon. Anyway, I kind of hoping you can write some materials on stand-alone application. Thx, Uby-One ! :))

    ReplyDelete