【问题标题】:Automatic log4j configuration and bundleresource自动 log4j 配置和 bundleresource
【发布时间】:2011-10-20 18:30:51
【问题描述】:

两个问题/问题:

1)在许多log4j线程/论坛/等中,我看到定义log4j.debug的结果,其中包含以“file:”为前缀的log4j配置文件引用,如下所示:

log4j: Using URL [file:/data/app/conf/log4j.properties] for automatic log4j configuration.

在我正在更新的代码(由现在在其他地方工作的另一位开发人员编写)中,我看到:

log4j: Using URL [bundleresource://23/log4j.properties] for automatic log4j configuration

如上所示的“bundleresource”是什么/在哪里?我认为这是一个我无法识别的专用资源 Eclipse 插件。


2)这个问题源于一个更大的问题,即如果不将 log4j 配置文件显式插入 JVM 的命令行参数(例如,-Dlog4j.configuration),就无法加载它。我正在替换测试服务器上的基于 Eclipse 插件的应用程序,该应用程序没有在启动脚本中指定此命令行参数,并且能够在 log4j 正确初始化的情况下正确启动。当我用源存储库中的最新版本替换此应用程序时,应用程序无法找到 log4j 配置文件。为什么第一个应用程序可以启动并找到 log4j 配置文件,而第二个(较新的)应用程序却不能?

任何帮助将不胜感激。

谢谢!



更新/编辑

定义了 log4j.debug 的应用程序的输出:

log4j: Trying to find [log4j.xml] using context classloader sun.misc.Launcher$AppClassLoader@9fe666.
log4j: Trying to find [log4j.xml] using sun.misc.Launcher$AppClassLoader@9fe666 class loader.
log4j: Trying to find [log4j.xml] using ClassLoader.getSystemResource().
log4j: Trying to find [log4j.properties] using context classloader sun.misc.Launcher$AppClassLoader@9fe666.
log4j: Trying to find [log4j.properties] using sun.misc.Launcher$AppClassLoader@9fe666 class loader.
log4j: Trying to find [log4j.properties] using ClassLoader.getSystemResource().
log4j: Could not find resource: [null].

【问题讨论】:

    标签: eclipse logging configuration eclipse-plugin log4j


    【解决方案1】:

    您可以通过首先加载属性,然后将其扔到 log4j 的属性配置器中来加载 Log4j 的属性文件:

    Properties props = new Properties();
    FileInputStream fis = new FileInputStream("mylog4j.properties");
    props.load(fis);
    fis.close();
    PropertyConfigurator.configure(props);
    

    bundleresource 看起来像是一个特定于库的东西。我建议在源代码上使用 grep:

    grep -irn ./ -e "bundleresource"
    

    【讨论】:

    • 我会这样做,但是在对源进行 grep 之后,既没有显式调用 PropertyConfigurator 也没有调用 DocumentConfigurator。这一切似乎都是在以前的安装中自动启动时完成的。它似乎是在类路径中找到 log4j 配置,发现某个我不知道的地方。对此有何其他想法?
    • 这个问题看起来确实是由于在类路径中找不到 log4j 配置文件。我将在后续编辑中发布我的输出。
    • 我至少回答了问题 2。=D 我从不使用类路径 log4j 的东西,我只是使用我发布的答案。也许,在您的主钩子上,您可以从已知位置加载属性文件并将其添加到您的 jar 路径,然后从您的类加载器加载。
    猜你喜欢
    • 1970-01-01
    • 2012-02-20
    • 2012-03-09
    • 1970-01-01
    • 2017-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多