2011-08-23 72 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>