Way to automatically detect wrong log4j static ini

2019-05-29 01:19发布

问题:

(Note that it's more of a Bash question than a Java question, see note below)

When configuring log4j in each class, we do the following:

public class Example {

  private static final Logger log = Logger.getLogger( Example.class );

The problem is that we now have a medium-sized codebase (200K LOC) containing a lot of Java classes and... Quite some misconfigured log4j loggers.

It's because people (including me, I admit), did silly cut'n'paste resulting sometimes in this:

public class Another {

  private static final Logger log = Logger.getLogger( Example.class );

And boom, instead of having Another.class, it's the old Example.class that is left and hence wrongly appearing in the logs (hence causing quite a few headaches).

I find it kind of weird that such kind of misconfiguration can happen but it can and our main issue now is not that it can happen but that we have to fix the misconfured loggers.

How can we go about automatically detecting these? (the fix can be manual but I'd like a way to find all the classes where log4j is misconfigured).

A Bash shell script, for example, would be very welcome.

  1. for every .java file
  2. find every "class XXX"
  3. parse the next 'x' lines (say 20)
  4. is there a Logger.getLogger(...) line?
  5. if yes, does it match the "class XXX"?
  6. if no report

False positive ain't an issue so it's not a problem if a few bogus "class XXX" are parsed etc.

NOTE: the problem is really that we now have 200 000 lines of code and we'd like to detect the violation automatically (the fix can be manual) so the question is not similar to:

[Is there a better way to get the current class variable in java?1

Actually it's probably more of a Bash question than a Java question :)

Any help on this most welcome.

回答1:

I guess if you're looking for a one-liner, one-liner

find -name "*.java" -exec sed -i \
    -e 's/private static final Logger \([a-zA-Z_][a-zA-Z0-9_]*).*$/private static final Logger \1 = LoggerFactory.make()/g' \
    -e 's/import org\.apache\.log4j\.Logger;/&\nimport path.to.LoggerFactory;/g' \
    {} \;

I would back up your code before trying this. It's probably broken in a couple places, but with a few corrections will get you what you're looking for. If you're using svn or something, you'll have to tweak find to exclude the .svn directories, otherwise your commits will be really messed up.

The gist: don't even bother trying to capture class names. Incorporate the solution indirectly linked to by Alexander. But replace your initial Logger declarations with factory calls. The only thing you need to capture is the name of the local variable. Then you need to find where your imports are, which I'm assuming you can do pretty exactly because you're importing log4j (or java.util.logging). Find that import statement and import your factory right below it.

BTW, all the warnings you're getting about automating this are correct and equally apply to this solution. You need to be prepared to javac everything right after trying this at the very least. Really, you should have some test suites with monster code coverage to automatically run at this point.



回答2:

Look for solutions in this posting: Is there a better way to get the current class variable in java?



回答3:

you could try weaving Logger.getLogger with AspectJ, in order to determine if the parameter, Example.class in your case, equals the "current class" name.

Hint: you can get the "current class" name programmatically using something like:

String className = new Exception().getStackTrace()[0].getClassName();


回答4:

Untested:

find *.java | while read file
    do
        lines=$(grep -A 20 "public class .* {" "$file")
        class=$(echo "$lines" | sed -n '1 s/public class \(.*\) {/\1/p'
        log=$(echo "$lines" | grep "Logger.getLogger"
        log=$(echo "$log" | sed -n 's/.*( *\(.*\).class *).*')
        if [[ "$log" != "$class" ]]
        then
            echo "There's a mis-match in file $file, class $class, for logger $log"
        fi
    done


回答5:

There may be a detector in FindBugs - if there isn't, it's definitely one to write...



回答6:

Look into checkstyle. You can write a checkstyle custom rule that does this. It will be an interesting exercise in XPath.

However, if the code is very predictably structured, I'd offer that it could be done in sed. If you want to structure the computation in bash, then ...

  1. use exec to open a file descriptor to the file
  2. loop with while reading lines
  3. when you see the first 'class' statement, grab the class name.
  4. when you see the Logger construction, grab and check.


标签: java bash log4j