方法来自动检测错误log4j的静态初始化 [英] Way to automatically detect wrong log4j static initialization

查看:170
本文介绍了方法来自动检测错误log4j的静态初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(请注意,它比一个Java问题Bash的问题,参见下面的注释)

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

在每个班级配置log4j的,我们做到以下几点:

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

public class Example {

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

问题是,我们现在有一个包含大量的Java类的中型codeBase类(200K LOC)和......颇有些错误配置的log4j记录器。

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.

这是因为人们(包括我在内,我承认),的确在这有时会导致傻cut'n'paste:

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 );

和繁荣,而不必的 Another.class 的,它是旧的 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).

我觉得有点奇怪,这种类型的错误配置都可以发生,但它可以和我们的主要问题现在还不是,它可以发生,但我们必须解决misconfured记录器。

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.

我们如何去自动检测这些? (修复可手动,但我想办法找到所有的地方log4j的配置错误的类)。

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).

一个bash shell脚本,例如,将是非常欢迎的。

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


  1. 对于每个java文件

  2. 找到每一个类XXX

  3. 解析下一个'X'线(比如20)

  4. 有一个Logger.getLogger(...)行?

  5. 如果是,这是否符合阶级XXX?

  6. 如果没有报告

假阳性是不是一个问题,所以它不是如果一些虚假的阶级XXX进行解析等的问题。

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

注意:这个问题确实是,我们现在有200万行code和我们想自动检测冲突(修复可手动),这样的问题是不是类似于:

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:

[有没有更好的方式来获得Java当前类变量<?一href=\"http://stackoverflow.com/questions/2151167/is-there-a-better-way-to-get-the-current-class-variable-in-java\">1

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

其实这很可能有不止一个Java问题Bash的问题:)

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

在这个最欢迎的任何帮助。

Any help on this most welcome.

推荐答案

我想,如果你正在寻找一个班轮,一个班轮

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' \
    {} \;

我会尝试此之前备份您的code。它可能打破了几个地方,但有一些修正将得到你,你找什么。如果您使用SVN或东西,你必须调整找到排除的.svn 目录,否则你的提交将真的搞砸了。

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.

要点:甚至不打扰试图捕捉类名。结合亚历山大间接与解决方案。但随着工厂调用替换您的初始记录仪的声明。你需要捕获的唯一事情是局部变量的名称。然后,你需要找到你的进口,这我假设你可以做pretty正是因为你要导入的log4j(或的java.util.logging ) 。找到导入语句和导入正确的下方你的工厂。

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.

顺便说一句,所有你得到关于这个自动化的警告是正确的,也同样适用于这一解决方案。您的需求的是ppared到 $ P $的javac 一切至少是尝试此之后。真的,你应该有怪物code覆盖一些测试套件在这一点上自动运行。

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.

这篇关于方法来自动检测错误log4j的静态初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆