为 Jetty 的 Maven 插件配置日志记录?

发布于 2024-11-30 23:27:15 字数 721 浏览 2 评论 0原文

我使用以下插件配置调用“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>

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鱼忆七猫命九 2024-12-07 23:27:15

回答我自己的问题:

  1. 插件看不到项目依赖项。您需要在 内指定

  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>

Answering my own question:

  1. Plugins don't see the project dependencies. You need to specify <dependencies> inside the <plugin>.

  2. 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>
才能让你更想念 2024-12-07 23:27:15

稍微扩展一下吉利的答案;使用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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文