2010-02-13

Catchlogger

Post seriousness: 70%.

Catchlogger [jar] [git src] checks that exceptions are being logged properly.

It's entirely fallible, but, if your codebase is prone to catching and ignoring exception, and you use log4j, it's Very Helpful.

For example:

try {
  foo();
} catch (IOException e) {
  logger.error("bar: failed to foo", e);
}

This is fine; an error has occurred and it's full stacktrace will be placed in the log4j configured log.

try {
  foo();
} catch (IOException e) {
  logger.error(e);
  logger.info("oh noes", e);
  e.printStackTrace();
}

None of these, however, will do anything useful. The first logs just the toString() to the error log, info is too low-level to log exceptions at, and printStackTrace() doesn't necessarily go anywhere at all.

For this block, catchlogger will issue:

IOException e unused at (Test.java:15) in public main(String[] args) in path.

The JAR is huge as it pulls in the entire Eclipse compiler to parse the source. BSD/MIT licensed.


Commenting is disabled for this post.

Read more of Faux' blog