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

时间:2010-03-03 15:47:18

标签: java logging maven-2 jetty maven-jetty-plugin

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

5 个答案:

答案 0 :(得分:18)

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

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

首先在插件中添加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>

然后添加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>

通过此设置,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

资源:

答案 1 :(得分:11)

为了扩展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>

这是您的${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

其他资源:

答案 2 :(得分:5)

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

答案 3 :(得分:2)

要扩展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>

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

中描述

例如,登录到具有更高调试级别(/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>

答案 4 :(得分:0)

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

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

也不要忘记粘贴

    <overwrite>true</overwrite>

for resources plugin