SLF4J / Log4J not initialized in jetty-maven-plugin

carlspring picture carlspring · Apr 3, 2012 · Viewed 7.4k times · Source

I am getting this error when running the 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.

The project is a war which contains log4j.properties in WEB-INF/classes.

I am also passing in the following properties to the plugin, just for the sake of seeing what's going on (that particular log4j.properties file exists in the location below as well):

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

The logging in the webapp works fine. However, I am baffled by the error.

I have these dependencies in the project:

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

In addition, when the tests (which need Jetty) start running, I do see the following output:

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.

Could somebody tell me why Jetty is unhappy?

Answer

asaed picture asaed · Apr 24, 2013

Another alternative is to use "file:///" style url for log4j.properties as follows :

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

I had the same problem where Jetty was looking for the log4j.properties file using a classloader that didn't include my project's source code. SO it kept complaining "log4j:WARN No appenders could be found for logger (org.eclipse.jetty.util.log).". But this workaround solved it and I'm able to see the log message and control them through log4j.