2011-08-23 112 views
16

我调用“码头:运行”的目标有以下插件配置:为Jetty的maven插件配置日志记录?

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

码头拒绝记录任何尽管事实证明我的项目声明了SLF4J作为一个依赖于SLF4J。如果我将“-Dorg.eclipse.jetty.util.log.DEBUG = true”传递给JVM,则Jetty会输出大量日志,但它们似乎转到stderr而不是slf4j。有任何想法吗?

回答

12

回答我的问题:

  1. 插件没有看到项目的依赖。您需要在<plugin>内指定<dependencies>

  2. 您需要指定具体的slf4j实现,例如logback。指定slf4j是不够的。

最终的结果应该是这个样子:

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

注意,要配置日志记录只需让maven知道你的配置文件在哪里:mvn verify -Dlogback.configurationFile =/path/to/logback.xml – Mike

+0

版本9是​​否有更新?当我使用此配置时,我没有看到来自Jetty的任何日志。 – user64141

+0

@ user64141我不知道有任何更改。也就是说,我不再使用Jetty maven插件。我现在从一个普通的Java类中调用Jetty。 – Gili

5

延伸吉利的回答有点;使用properties-maven-plugin是设置系统属性的一种便捷方式,而不必在命令行上指定它们。我提供了logback和log4j的示例。除了Gili的答案中的jetty-maven-plugin配置之外,将此插件块添加到您的pom.xml中。

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

也为log4j的,自然地使用以下依赖于码头,Maven的插件而不是的logback经典:

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