February 4, 2009

Add session identifier to request logging in Tomcat

In a previous article we learned how to enable request logging in Tomcat. The format of the log is the same as for standard web servers. If we look at conf/server.xml and the Valve element for the request logging we see the attribute pattern. This is set to common, which is the default format.

We can change the format by changing the pattern attribute. For example if we want to add the session identifier we must add %S to the pattern. For example we can use the following pattern: %h %u %S %t '%r' %s %b %D. The complete element now looks like this:

<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
  prefix="localhost_access_log." suffix=".log"
  pattern="%h %u %S %t '%r' %s %b %D" resolveHosts="false"/>

The Tomcat documentation contains more information about request logging and the different patterns.