【问题标题】:SLF4J / Log4J not initialized in jetty-maven-pluginSLF4J / Log4J 未在 jetty-maven-plugin 中初始化
【发布时间】:2012-04-17 05:31:18
【问题描述】:

运行 jetty-maven-plugin 时出现此错误:

[INFO] --- jetty-maven-plugin:7.6.1.v20120215:start (start-jetty) @ rest ---
log4j:WARN No appenders could be found for logger (org.eclipse.jetty.util.log).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

该项目是一个包含 WEB-INF/classes 中的 log4j.properties 的战争。

我还将以下属性传递给插件,只是为了查看发生了什么(该特定 log4j.properties 文件也存在于以下位置):

<!-- Log4J settings -->
<systemProperty>
    <name>log4j.configuration</name>
    <value>file://${project.build.testOutputDirectory}/log4j.properties</value>
</systemProperty>
<systemProperty>
    <name>log4j.debug</name>
</systemProperty>

webapp 中的日志记录工作正常。但是,我对这个错误感到困惑。

我在项目中有这些依赖:

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>jcl-over-slf4j</artifactId>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-core</artifactId>
</dependency>

此外,当测试(需要 Jetty)开始运行时,我确实看到了以下输出:

log4j: Using URL [file:/project/foo/rest/target/test-classes/log4j.properties] for automatic log4j configuration.
log4j: Reading configuration from URL file:/project/foo/rest/target/test-classes/log4j.properties
log4j: Parsing for [root] with value=[ERROR, console].
log4j: Level token is [ERROR].
log4j: Category root set to ERROR
log4j: Parsing appender named "console".
log4j: Parsing layout options for "console".
log4j: Setting property [conversionPattern] to [%d %p %c - %m%n].
log4j: End of parsing for "console".
log4j: Parsed "console" options.
log4j: Parsing for [project.foo] with value=[DEBUG].
log4j: Level token is [DEBUG].
log4j: Category project.foo set to DEBUG
log4j: Handling log4j.additivity.project.foo=[null]
log4j: Finished configuring.

有人能告诉我为什么 Jetty 不开心吗?

【问题讨论】:

    标签: maven log4j slf4j maven-jetty-plugin


    【解决方案1】:

    另一种选择是为 log4j.properties 使用“file:///”样式的 url,如下所示:

     <plugin>
      <groupId>org.mortbay.jetty</groupId>
      <artifactId>jetty-maven-plugin</artifactId>
      <version>8.1.10.v20130312</version>
      <configuration>
        <systemProperties>
                <systemProperty>
                    <name>log4j.configuration</name>
                    <!-- have to use file:/// url since -->
                        <!-- Jetty is using classloader --> 
                        <!-- before the webapp classloader is ready -->
                   <value>file:///${basedir}/src/main/resources/log4j.properties</value>
                </systemProperty>
         <configuration>
         <dependencies>
            <dependency>  
                <groupId>org.slf4j</groupId>  
                <artifactId>slf4j-log4j12</artifactId>  
                <version>1.6.6</version>  
            </dependency>
         </dependencies>
     </plugin>
    

    Jetty 使用不包含我项目源代码的类加载器查找 log4j.properties 文件时遇到了同样的问题。所以它一直在抱怨“log4j:WARN No appenders could be found for logger (org.eclipse.jetty.util.log)。”。但是这个解决方法解决了它,我可以看到日志消息并通过 log4j 控制它们。

    【讨论】:

      【解决方案2】:

      this message board thread 所示,您不能再使用 jetty-maven-plugin 中的系统属性配置 log4j。从 Jetty 7.5.0 开始,Jetty 类现在使用静态 日志初始化器。这些静态日志初始化器会在加载系统属性之前初始化 Log4j 系统。

      两种可能的解决方法是降级到 Jetty 7.4.5,或者使用单独的 maven 插件(例如 properties-maven-plugin)在 Jetty 插件初始化之前设置 log4j 系统属性。

      【讨论】:

      • 谢谢。谢谢你。谢谢你。我一直在为系统属性和 8.x Jetty 插件苦苦挣扎。我已经阅读了很多帖子,但这是第一个有效的。
      【解决方案3】:

      问题是我有一个聚合器,其中有几个模块,每个模块在测试之前启动 Jetty,然后停止它。启动 Jetty 时,我定义了 &lt;systemProperties/&gt;。在查看了 Jetty 的源代码后,我发现一旦从其中一个模块以这种方式设置系统属性,它们就永远不会在以后(在您的其他模块中)被覆盖,因为插件中有一条规则禁止这样做。因此,日志的系统属性在执行之间变得混乱,尽管它们位于不同的子模块中。

      我通过编写自己的 Maven 插件来解决此问题,该插件会在执行之前为您设置系统属性。我已将项目here 放在github 中。使用方法说明见here

      【讨论】:

      • 感谢您的解决方案;你的插件对我不起作用,但我能够使用属性 maven 插件 (mojo.codehaus.org/properties-maven-plugin) 的 set-system-properties 目标来完成同样的事情。
      • 我实际上已经向这里的 Jetty 人员提交了一个错误:jira.codehaus.org/browse/JETTY-1507。基本上,插件不会覆盖任何已经存在的系统属性。我不得不以艰难的方式找到这一点。这真的不是预期的行为。如果你愿意,你可以投票给票。
      猜你喜欢
      • 2013-04-24
      • 2013-12-11
      • 2016-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-30
      • 1970-01-01
      相关资源
      最近更新 更多