Enable debug logging in maven jetty 7 plugin

守給你的承諾、 提交于 2019-11-30 08:14:38

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.

First add logback-classic as dependency in the plugin and set the logback.configurationFile system property to point on a configuration file:

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

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

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

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:

You can also do this "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):

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

For instance, log into a file /tmp/output.log with higher debug level (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>

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!