Logging with log4j in jetty-maven-plugin

The jetty-maven-plugin is a great way to run a webapp during development. But the main problem is that it doesn’t print debug messages when you are using commons-logging. The reason is that it by default uses org.apache.commons.logging.impl.SimpleLog. This logger can’t print debug messages. Here is the solution on how to change this default behaviour so that it use Log4J instead.

Add this to your pom.xml inside the <plugins> tag:

<plugin>
  <groupid>org.mortbay.jetty</groupid>
  <artifactid>maven-jetty-plugin</artifactid>
  <configuration>
    <jettyenvxml>jetty/jetty-env.xml</jettyenvxml>
    <scanintervalseconds>3</scanintervalseconds>
    <connectors>
    <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
      <port>8180</port>
    </connector>
    </connectors>
    <systemproperties>
      <systemproperty>
        <name>org.apache.commons.logging.Log</name>
        <value>
        org.apache.commons.logging.impl.Log4JLogger
        </value>
      </systemproperty>
      <systemproperty>
        <name>slf4j</name>
        <value>false</value>
      </systemproperty>
      <systemproperty>
        <name>log4j.configuration</name>
        <value>file:./jetty/log4j.properties</value>
      </systemproperty>
    </systemproperties>
  </configuration>
  <dependencies>
    <dependency>
      <groupid>commons-logging</groupid>
      <artifactid>commons-logging</artifactid>
      <version>1.1</version>
      <type>jar</type>
    </dependency>
    <dependency>
      <groupid>log4j</groupid>
      <artifactid>log4j</artifactid>
      <version>1.2.13</version>
      <type>jar</type>
    </dependency>
  </dependencies>
</plugin>

Note that I point to a ./jetty/log4j.properties file. This could look something like:

log4j.rootCategory=ERROR, A1
log4j.logger.no.api=DEBUG
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%d %-4r [%t] %-5p %c %x - %m%n

This will print all log messages to your console.

2 thoughts on “Logging with log4j in jetty-maven-plugin

  1. Does it work after jetty detects a change and restarts? I’ve got problems with log4j complaining about missing appenders, resulting in zero logging after jetty restarts. 🙁

  2. Hmmm, I’m really not sure about that. I haven’t noticed this problem. I will do some testing, and come back to you if I am able to reconstruct this behaviour.

Leave a Reply

Your email address will not be published. Required fields are marked *