You need the following components to begin:

  1. A Tomcat server (I took Apache Tomcat 7, but it should work the same way for Tomcat 5 and 6)
  2. Logstash (currently version 1.4.2)
  3. Logstash-Gelf (currently version 1.4.2)

Having all dependencies allows you to proceed with the integration - hassle-free, multi-lined messages and stack traces are working out of the box.

One word at java util logging

Tomcat uses the java util logging in order to produce log output for the server itself. The log system in the Tomcat server is initialized as one of the first, even before utilizing the library classpath. Therefore you have to have any logging libraries on the JVM's starting class path (java -cp ...). To achieve this, you've got to modify the startup scripts. I've added a full listing and a diff.

Now adjust the Tomcat server

  1. Extract the logstash-gelf module and drop all the jars (commons-pool2-2.0.jar, jedis-2.5.1.jar, json-simple-1.1.jar, logstash-gelf-1.4.2.jar) into the bin-directory of your Tomcat server (or you can use every other path outside the lib-directories. This example relies on the bin-directory)
  2. Add the jars to the Tomcat class path modifying the bin/catalina.sh or bin/catalina.bat file (see Gist for diff)
    if [ -r "$CATALINA_BASE/bin/logstash-gelf-1.4.2.jar" ] ; then
      CLASSPATH=$CLASSPATH:$CATALINA_BASE/bin/logstash-gelf-1.4.2.jar:$CATALINA_BASE/bin/json-simple-1.1.jar:$CATALINA_BASE/bin/jedis-2.5.1.jar:$CATALINA_BASE/bin/commons-pool2-2.0.jar
    else
      CLASSPATH=$CLASSPATH:$CATALINA_HOME/bin/logstash-gelf-1.4.2.jar:$CATALINA_HOME/bin/json-simple-1.1.jar:$CATALINA_HOME/bin/jedis-2.5.1.jar:$CATALINA_HOME/bin/commons-pool2-2.0.jar
    fi
  3. Add the log handler to conf/logging.properties (see Gist for the full logging.properties file)
    
    handlers = 1catalina.org.apache.juli.FileHandler, 2localhost.org.apache.juli.FileHandler, 3manager.org.apache.juli.FileHandler, 4host-manager.org.apache.juli.FileHandler, java.util.logging.ConsoleHandler, biz.paluch.logging.gelf.jul.GelfLogHandler
     
    .handlers = 1catalina.org.apache.juli.FileHandler, java.util.logging.ConsoleHandler, biz.paluch.logging.gelf.jul.GelfLogHandler
     
    biz.paluch.logging.gelf.jul.GelfLogHandler.host=udp:localhost
    biz.paluch.logging.gelf.jul.GelfLogHandler.port=12201
    biz.paluch.logging.gelf.jul.GelfLogHandler.level=INFO
    
  4. Start Tomcat and enjoy logging!

Resources