Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SLF4J / Log4J not initialized in jetty-maven-plugin

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?

like image 287
carlspring Avatar asked Apr 03 '12 14:04

carlspring


3 Answers

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.

like image 117
asaed Avatar answered Nov 08 '22 11:11

asaed


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.

like image 36
piepera Avatar answered Nov 08 '22 11:11

piepera


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.

like image 1
carlspring Avatar answered Nov 08 '22 10:11

carlspring