Java 在 maven jetty 7 插件中启用调试日志记录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2372670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 06:40:00  来源:igfitidea点击:

Enable debug logging in maven jetty 7 plugin

javaloggingmaven-2jettymaven-jetty-plugin

提问by wds

I'm running a java webapp with a simple mvn jetty:run, using the latest jetty plugin, but I can't seem to find a way to tell jetty to output DEBUG messages to console (for the embedded jetty instance, not the plugin itself). It's currently outputting only WARN and INFO messages. I've tried setting -DDEBUGand -DVERBOSE, but they don't do anything. I've already had a look at the documentation, but it doesn't seem to cover this.

我正在mvn jetty:run使用最新的 jetty 插件运行一个带有 simple 的 java webapp ,但我似乎无法找到一种方法来告诉 jetty 将 DEBUG 消息输出到控制台(对于嵌入式码头实例,而不是插件本身)。它目前只输出 WARN 和 INFO 消息。我试过设置-DDEBUG-DVERBOSE,但他们什么也没做。我已经看过文档,但它似乎没有涵盖这一点。

采纳答案by Pascal Thivent

Update:OK, I finally got things working and here is what I did.

更新:好的,我终于开始工作了,这就是我所做的。

My understanding is that Jetty 7 doesn't have any dependencies on a particular logging framework, even for the JSP engine since Jetty 7 uses the JSP 2.1 engine. So you can use any logging framework. Here I will use logback.

我的理解是 Jetty 7 不依赖于特定的日志记录框架,即使对于 JSP 引擎也是如此,因为 Jetty 7 使​​用 JSP 2.1 引擎。所以你可以使用任何日志框架。这里我将使用 logback。

First add logback-classicas dependency in the plugin and set the logback.configurationFilesystem property to point on a configuration file:

首先logback-classic在插件中添加依赖项并将logback.configurationFile系统属性设置为指向配置文件:

<project>
  ...
  <build>
    ...
    <plugins>
      <plugin>
        <groupId>org.mortbay.jetty</groupId>
        <artifactId>jetty-maven-plugin</artifactId>
        <version>7.0.0.pre5</version>
        <configuration>
          <systemProperties>
            <systemProperty>
              <name>logback.configurationFile</name>
              <value>./src/etc/logback.xml</value>
            </systemProperty>
          </systemProperties>
        </configuration>
        <dependencies>
          <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>0.9.15</version>
          </dependency>
        </dependencies>
      </plugin>
      ...
    </plugins>
  </build>
  ...
</project>

Then add a src/etc/logback.xmlconfiguration file. Below a minimal configuration:

然后添加一个src/etc/logback.xml配置文件。下面是一个最小配置:

<configuration>
  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <layout class="ch.qos.logback.classic.PatternLayout">
      <Pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</Pattern>
    </layout>
  </appender>

  <root level="debug">
    <appender-ref ref="STDOUT"/>
  </root>
</configuration>

With this setup, jetty will output DEBUG messages:

使用此设置,jetty 将输出 DEBUG 消息:

$ mvn jetty:run
...
00:31:33.089 [main] DEBUG org.mortbay.log - starting DefaultHandler@145e5a6
00:31:33.089 [main] DEBUG org.mortbay.log - started DefaultHandler@145e5a6
00:31:33.105 [main] DEBUG org.mortbay.log - starting RequestLogHandler@1e80761
00:31:33.106 [main] DEBUG org.mortbay.log - started RequestLogHandler@1e80761
00:31:33.106 [main] DEBUG org.mortbay.log - starting HandlerCollection@1485542
00:31:33.106 [main] DEBUG org.mortbay.log - started HandlerCollection@1485542
00:31:33.106 [main] DEBUG org.mortbay.log - starting org.mortbay.jetty.Server@a010ba
00:31:33.174 [main] DEBUG org.mortbay.log - started org.mortbay.jetty.nio.SelectChannelConnector@ee21f5
00:31:33.216 [main] INFO  org.mortbay.log - Started [email protected]:8080
00:31:33.217 [main] DEBUG org.mortbay.log - started [email protected]:8080
00:31:33.217 [main] DEBUG org.mortbay.log - started org.mortbay.jetty.Server@a010ba
[INFO] Started Jetty Server

Resources:

资源:

回答by yegor256

To extend Pascal's answer, this is how it works with log4j:

为了扩展 Pascal 的答案,这就是它与 log4j 的工作方式:

<project>
  ...
  <build>
    ...
    <plugins>
      <plugin>
        <groupId>org.mortbay.jetty</groupId>
        <artifactId>jetty-maven-plugin</artifactId>
        <version>7.0.0.pre5</version>
        <configuration>
          <systemProperties>
            <systemProperty>
              <name>log4j.configurationFile</name>
              <value>file:${project.basedir}/src/test/resources/log4j.properties</value>
            </systemProperty>
          </systemProperties>
        </configuration>
        <dependencies>
           <dependency>
             <groupId>log4j</groupId>
             <artifactId>log4j</artifactId>
             <version>1.2.16</version>
           </dependency>
           <dependency>
             <groupId>org.slf4j</groupId>
             <artifactId>slf4j-api</artifactId>
             <version>1.6.1</version>
           </dependency>
           <dependency>
             <groupId>org.slf4j</groupId>
             <artifactId>slf4j-log4j12</artifactId>
             <version>1.6.1</version>
           </dependency>
        </dependencies>
      </plugin>
      ...
    </plugins>
  </build>
  ...
</project>

This is your ${project.basedir}/src/test/resources/log4j.properties:

这是你的${project.basedir}/src/test/resources/log4j.properties

log4j.rootLogger=INFO, CONSOLE
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern = [%-5p] %c: %m\n
log4j.logger.org.eclipse.jetty.util.log=INFO

Additional resources:

其他资源:

回答by somebodywhocares

You can also do this "mvn -X jetty:run"

你也可以这样做“mvn -X jetty:run”

回答by vogdb

I find this solution more convenient

我觉得这个解决方案更方便

    <resources>
        <resource>
            <directory>${project.basedir}/src/main/resources</directory>
            <targetPath>${project.build.outputDirectory}</targetPath>
            <includes>
                <include>log4j.properties</include>
            </includes>
        </resource>
    </resources>

also don't forget paste

也不要忘记粘贴

    <overwrite>true</overwrite>

for resources plugin

对于资源插件

回答by Stepan Vavra

To extend Pascal's and yegor256's answer, this is how it works with SLF4J Simple logger(which is the most easiest option since you just need to add a dependency to slf4j-simple):

要扩展 Pascal 和 yegor256 的答案,这就是它的工作方式SLF4J Simple logger(这是最简单的选项,因为您只需要向 中添加依赖项slf4j-simple):

<project>
  ...
  <build>
    ...
    <plugins>
      <plugin>
        <groupId>org.mortbay.jetty</groupId>
        <artifactId>jetty-maven-plugin</artifactId>
        <version>7.0.0.pre5</version>

        <dependencies>
           <dependency>
              <groupId>org.slf4j</groupId>
              <artifactId>slf4j-simple</artifactId>
              <version>1.7.5</version>
           </dependency>
        </dependencies>
      </plugin>
      ...
    </plugins>
  </build>
  ...
</project>

It is possible to configure the SLF4J Logger directly from Maven pom. Defaults are described in http://www.slf4j.org/apidocs/org/slf4j/impl/SimpleLogger.html)

可以直接从 Maven pom 配置 SLF4J Logger。默认值在http://www.slf4j.org/apidocs/org/slf4j/impl/SimpleLogger.html中描述)

For instance, log into a file /tmp/output.logwith higher debug level (TRACE):

例如,登录到/tmp/output.log具有更高调试级别 ( TRACE)的文件:

<configuration>
   <systemProperties>
      <systemProperty>
         <name>org.slf4j.simpleLogger.logFile</name>
         <value>/tmp/output.log</value>
      </systemProperty>
      <systemProperty>
         <name>org.slf4j.simpleLogger.defaultLogLevel</name>
         <value>trace</value>
      </systemProperty>
   </systemProperties>
</configuration>