简体   繁体   English

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

[英]Enable debug logging in maven jetty 7 plugin

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). 我正在运行一个带有简单mvn jetty:run的java webapp mvn jetty:run ,使用最新的jetty插件,但我似乎找不到告诉jetty将DEBUG消息输出到控制台的方法(对于嵌入式jetty实例,而不是插件本身)。 It's currently outputting only WARN and INFO messages. 它目前只输出WARN和INFO消息。 I've tried setting -DDEBUG and -DVERBOSE , but they don't do anything. 我尝试过设置-DDEBUG-DVERBOSE ,但他们什么也没做。 I've already had a look at the documentation , but it doesn't seem to cover this. 我已经看过文档 ,但它似乎没有涵盖这一点。

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. 我的理解是Jetty 7对特定的日志框架没有任何依赖性,即使对于JSP引擎也是如此,因为Jetty 7使用JSP 2.1引擎。 So you can use any logging framework. 所以你可以使用任何日志框架。 Here I will use logback. 在这里,我将使用logback。

First add logback-classic as dependency in the plugin and set the logback.configurationFile system 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.xml configuration file. 然后添加一个src/etc/logback.xml配置文件。 Below a minimal configuration: 低于最小配置:

<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$1@ee21f5
00:31:33.216 [main] INFO  org.mortbay.log - Started SelectChannelConnector@0.0.0.0:8080
00:31:33.217 [main] DEBUG org.mortbay.log - started SelectChannelConnector@0.0.0.0:8080
00:31:33.217 [main] DEBUG org.mortbay.log - started org.mortbay.jetty.Server@a010ba
[INFO] Started Jetty Server

Resources: 资源:

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: 其他资源:

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

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添加一个依赖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. 可以直接从Maven pom配置SLF4J Logger。 Defaults are described in http://www.slf4j.org/apidocs/org/slf4j/impl/SimpleLogger.html ) 默认值在http://www.slf4j.org/apidocs/org/slf4j/impl/SimpleLogger.html中描述

For instance, log into a file /tmp/output.log with higher debug level ( TRACE ): 例如,登录到具有更高调试级别( TRACE )的文件/tmp/output.log

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

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 对于资源插件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM