Configure logging for Jetty's maven plugin?

后端 未结 2 923
感动是毒
感动是毒 2021-01-03 23:49

I\'m invoking the \"jetty:run\" goal with the following plugin configuration:


  org.mortbay.jetty
  

        
相关标签:
2条回答
  • 2021-01-04 00:06

    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>
    
    0 讨论(0)
  • 2021-01-04 00:19

    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>
    
    0 讨论(0)
提交回复
热议问题