SLF4J/Log4J未在jetty maven插件中初始化



我在运行jetty maven插件时收到这个错误:

[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>

登录网络应用程序运行良好。然而,我对这个错误感到困惑。

我在项目中有以下依赖项:

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

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

另一种选择是对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找不到logger(org.eclipse.jjetty.util.log)的附加程序。"。但这个解决方法解决了这个问题,我可以通过log4j看到日志消息并控制它们。

如此消息板线程所示,您无法再使用jetty-maven插件中的系统属性配置log4j。从Jetty 7.5.0开始,Jetty类现在使用静态日志初始化程序。这些静态日志初始化程序导致在加载系统属性之前对Log4j系统进行初始化。

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

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

我通过编写自己的Maven插件来解决这个问题,该插件在执行之前为您设置System属性。我已经把这个项目放在github里了。关于如何使用它的解释可以在这里找到。

最新更新