Ever wondered, how to integrate logstash and Tomcat 7 the easy way? Here we go!
You need the following components to begin:
- A Tomcat server (I took Apache Tomcat 7, but it should work the same way for Tomcat 5 and 6)
- Logstash (currently version 1.4.2)
- 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
- 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)
- 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
- 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
- Start Tomcat and enjoy logging!
- Gist containing the diffs: https://gist.github.com/mp911de/90792c304fcc5bf0737c
- Gist containing the complete files: https://gist.github.com/mp911de/69385b908d129a74b205