问题
I'm invoking the "jetty:run" goal with the following plugin configuration:
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>7.4.4.v20110707</version>
<configuration>
<scanIntervalSeconds>5</scanIntervalSeconds>
<connectors>
<connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
<port>80</port>
</connector>
</connectors>
</configuration>
</plugin>
Jetty refuses to log anything to slf4j in spite of the fact that my project declares slf4j as a dependency. If I pass "-Dorg.eclipse.jetty.util.log.DEBUG=true" to the JVM, Jetty outputs tons of logs but they seem to go to stderr instead of to slf4j. Any ideas?
回答1:
Answering my own question:
Plugins don't see the project dependencies. You need to specify
<dependencies>
inside the<plugin>
.You need to specify a concrete slf4j implementation, such as logback. Specifying slf4j is not enough.
The end-result should look something like this:
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>7.4.4.v20110707</version>
<configuration>
<scanIntervalSeconds>5</scanIntervalSeconds>
<connectors>
<connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
<port>80</port>
</connector>
</connectors>
</configuration>
<dependencies>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>0.9.29</version>
</dependency>
</dependencies>
</plugin>
回答2:
Extending Gili's answer a bit; using the properties-maven-plugin is a convenient way to set system properties instead of having to specify them on the command line. I provide examples for both logback and log4j. Add this plugin block to your pom.xml in addition to the jetty-maven-plugin configuration in Gili's answer.
Logback:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-2</version>
<executions>
<execution>
<goals>
<goal>set-system-properties</goal>
</goals>
<configuration>
<properties>
<!-- makes jetty log the exception if it fails to initialize slf4j -->
<property>
<name>org.eclipse.jetty.util.log.IGNORED</name>
<value>true</value>
</property>
<!-- Location of logback config -->
<property>
<name>logback.configurationFile</name>
<value>/path/to/logback.xml</value>
</property>
</properties>
</configuration>
</execution>
</executions>
</plugin>
Log4j:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-2</version>
<executions>
<execution>
<goals>
<goal>set-system-properties</goal>
</goals>
<configuration>
<properties>
<!-- makes jetty log the exception if it fails to initialize slf4j -->
<property>
<name>org.eclipse.jetty.util.log.IGNORED</name>
<value>true</value>
</property>
<!-- this tells where the log4j configuration is -->
<property>
<name>log4j.configuration</name>
<value>file:./src/main/resources/log4j.properties</value>
</property>
<!-- this can be uncommented to debug startup log4j itself,
e.g. how it locates log4j.properties etc -->
<!--
<property>
<name>log4j.debug</name>
<value></value>
</property>
-->
</properties>
</configuration>
</execution>
</executions>
</plugin>
Also for log4j, naturally use the following dependency for the jetty-maven-plugin instead of logback-classic:
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
...
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.4</version>
</dependency>
</dependencies>
</plugin>
来源:https://stackoverflow.com/questions/7168214/configure-logging-for-jettys-maven-plugin