配置Jetty的maven插件的日志记录?

时间:2011-08-23 22:16:28

标签: jetty slf4j maven-jetty-plugin

我正在使用以下插件配置调用“jetty:run”目标:

<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拒绝将任何内容记录到slf4j,尽管我的项目将slf4j声明为依赖项。如果我将“-Dorg.eclipse.jetty.util.log.DEBUG = true”传递给JVM,Jetty会输出大量的日志,但它们似乎转到stderr而不是slf4j。有什么想法吗?

2 个答案:

答案 0 :(得分:14)

回答我自己的问题:

  1. 插件看不到项目依赖项。您需要在<dependencies>

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

  4. 最终结果应如下所示:

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

答案 1 :(得分:6)

延长吉利的答案;使用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,自然使用jetty-maven-plugin的以下依赖项而不是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>