Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Log Coverage tool

Are there any tools or strategies for generating a "Log coverage" report on (Java, log4j)? Like code coverage, but ensuring that there aren't large methods, classes, or packages which don't log anything.

When coding web services, me team doesn't write many log statements. When debugging a real time problem with running, production code, we always wish we had. Inevitably we try to reproduce the bug in our test environment with either a debugger attached or additional log statements added, which can be very difficult depending on the structures and inter-operation involved.

Does anyone use this as a code-quality metric?

like image 337
Cory Kendall Avatar asked Mar 06 '13 00:03

Cory Kendall


1 Answers

Code coverage takes special instrumentation because you're trying to find out whether a piece of production code is exercised by any test. What you're asking is a little more vague and could be either much easier ("is any logging done for this large class?") or much harder to the point of impossible ("did we log the method that's going to break in production?").

For the first question, you could whip up a shell script pretty quickly to do the job. Here's a skeleton in Perl, for example. Here, I assume that we're using SLF4J and that seeing the import of "LoggerFactory" is enough evidence to assume there's a logger.

while ($filename = shift) {
    open my $in, "<$filename";
    my $loc = 0;
    my $log = "NO LOGGER";
    while (<$in>) {
        $loc++;
        if (m/import org.slf4j.LoggerFactory/) {
            $log = "has logger";
        }
    }
    print "$filename : $loc LOC $log\n";
    $total{$log} += $loc;
}
print "\n\nTOTAL LOGGED: $total{'has logger'}\nTOTAL UNLOGGED: $total{'NO LOGGER'}\n";

and I can run this from my shell to run over all the Java files in a little project with

$ find . -name \*.java -exec perl haslog.pm {} \+

This only works for small-sized projects, and it's fairly brittle but it wouldn't be a ton of work to make a more robust version of this.

like image 97
Nathaniel Waisbrot Avatar answered Oct 02 '22 16:10

Nathaniel Waisbrot