Configure logging for Jetty's maven plugin?
Asked Answered
P

2

21

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

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

Jetty refuses to log anything to slf4j in spite of the fact that my project declares slf4j as a dependency. If I pass "-Dorg.eclipse.jetty.util.log.DEBUG=true" to the JVM, Jetty outputs tons of logs but they seem to go to stderr instead of to slf4j. Any ideas?

Physician answered 23/8, 2011 at 22:16 Comment(0)
P
14

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>
Physician answered 30/8, 2011 at 21:45 Comment(3)
Note, to configure the logging just let maven know where your configuration file is: mvn verify -Dlogback.configurationFile=/path/to/logback.xmlAmourpropre
Is there an update for version 9? I'm not seeing any logs from Jetty when I use this configuration.Tabbitha
@Tabbitha I'm not aware of any changes. That said, I no longer use the Jetty maven plugin. I now invoke Jetty from a normal Java class.Physician
F
6

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>
Fetish answered 15/9, 2013 at 19:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.