I attempted to solve this issue in this question previously, but attempts at using a plain regex didn’t quite work.
In a project I’m working on, log methods provide the following
public static void info(Object source, String message)
public static void info(Object source, String message, Throwable t)
for various logging levels.
However, the methods have been used incorrectly, in a way that doesn’t log all the information.
// Bad example 1
try {
// something
} catch (Exception e) {
Log.error(this, "oops");
}
// Bad example 2
try {
// something
} catch (Exception e) {
Log.error(this, "oops " + e);
}
// Bad example 3
try {
// something
} catch (Exception e) {
Log.error(this, "oops " + e.getMessage());
}
// Correct usage
try {
// something
} catch (Exception e) {
Log.error(this, "oops", e);
}
// Technically correct, but rarely seen and should probably be changed anyway
try {
// something
} catch (Exception e) {
Log.error(this, "oops");
throw new OtherException(e);
}
The bad examples have been a cause for headaches when debugging, as the full exception isn’t included in the log.
My question is: how can I find all cases where a log line is used incorrectly within a catch block? Namely, where the exception is used neither as a parameter to a log method, nor as a cause to the rethrown exception. The question linked above attempted to find these using regex but most solutions suggested had a fairly high failure rate (either matching too many correct usages or not matching all incorrect usages).
I’m using Intellij as my IDE, so if it has any tools that would be useful in achieving this that would also be good to know.