SLF4J / Log4J not initialized in jetty-maven-plugin
Asked Answered
L

3

5

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?

Latinist answered 3/4, 2012 at 14:28 Comment(0)
S
6

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.

Samurai answered 23/4, 2013 at 22:55 Comment(0)
Y
4

As indicated in this message board thread, you cannot configure log4j using system properties in the jetty-maven-plugin anymore. As of Jetty 7.5.0, Jetty classes now use static log initializers. These static log initializers cause the Log4j system to be initialized before system properties are loaded.

Two possible workarounds would be either to downgrade to Jetty 7.4.5, or to use a separate maven plugin such as the properties-maven-plugin to set the log4j system properties before the Jetty plugin is initialized.

Yarber answered 30/5, 2012 at 20:4 Comment(1)
Thank you. Thank you. Thank you. I've been struggling with system properties and the 8.x Jetty plugin for awhile. I've read so many posts, but this is the first that worked.Novia
L
1

The problem was that I have an aggregator with several modules each starting Jetty before it's tests and then stopping it. When starting Jetty, I have <systemProperties/> defined. After having a look at Jetty's sources, I found out that once system properties are set this way from one of the modules, they are never overriden later on (in your other modules) simply because there is a rule in the plugin which forbids this. Thus, the system properties for the logging were getting confused between executions, despite the fact that they were in different sub-modules.

I fixed this by writing my own Maven plugin that sets the System properties for you before the execution. I have put the project here in github. Explanations of how to use it can be found here.

Latinist answered 6/4, 2012 at 8:4 Comment(2)
Thank you for this solution; your plugin didn't work for me but I was able to use the set-system-properties goal of the properties maven plugin (mojo.codehaus.org/properties-maven-plugin) to accomplish the same thing.Yarber
I have actually filed a bug for this to the Jetty guys here: jira.codehaus.org/browse/JETTY-1507. Basically, the plugin will not override any system property which already exists. I had to find this the hard way. It's really not expected behavior. If you like, you can vote for the ticket.Latinist

© 2022 - 2024 — McMap. All rights reserved.