SLF4J/Log4J未在jetty-maven-plugin中初始化

car*_*ing 4 log4j slf4j maven maven-jetty-plugin

运行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.
Run Code Online (Sandbox Code Playgroud)

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

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

<!-- Log4J settings -->
<systemProperty>
    <name>log4j.configuration</name>
    <value>file://${project.build.testOutputDirectory}/log4j.properties</value>
</systemProperty>
<systemProperty>
    <name>log4j.debug</name>
</systemProperty>
Run Code Online (Sandbox Code Playgroud)

登录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>
Run Code Online (Sandbox Code Playgroud)

另外,当测试(需要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.
Run Code Online (Sandbox Code Playgroud)

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

小智 5

另一种方法是使用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>
Run Code Online (Sandbox Code Playgroud)

我遇到了同样的问题,Jetty正在使用不包含我项目源代码的类加载器来查找log4j.properties文件.所以它一直在抱怨"log4j:WARN没有为logger找到appender(org.eclipse.jetty.util.log)." 但是这个解决方法解决了它,我能够看到日志消息并通过log4j控制它们.