2 Minutes To Code (β Version)

Apache Tomcat - 2 minute tutorial

How to customize local access log that records all incoming http hits

You can ask the tomcat server to log all http requests urls along with parameters that it receives. Apart from this a few other details like client ip, timestamp, response time, response content length etc.

The file $TOMCAT_DIR/conf/server.xml has a Valve element with the className attribute org.apache.catalina.valves.AccessLogValve. This is used to configure access logs.

By default, it is commented out (). To enable access logs, uncomment it. The directory attribute specifies the folder name inside $TOMCAT_DIR where the logs file will be created. Attributes prefix and suffix are the prefix and suffix of log file name respectively.

The 'resolveHost' attribute specifies whether tomcayt should try to get the host name of the client (via dns look up) or just log ip. Unless unavoidable, set it to 'false' to avoid dns look ups which can introduce latency.

The most interesting attribute is pattern. This specifies the tokens that will be stored in access log file for each request. Its default value common stands for '%h %l %u %t "%r" %s %b', which can be used directly as attribute value instead of shorthand 'common'. Other supported tokens are :

Token What it stands for
%aRemote IP address
%ALocal IP address
%bBytes sent, excluding HTTP headers, or '-' if zero
%BBytes sent, excluding HTTP headers
%hRemote host name (or IP address if resolveHosts is false)
%HRequest protocol
%lRemote logical username from identd (always returns '-')
%mRequest method (GET, POST, etc.)
%pLocal port on which this request was received
%qQuery string (prepended with a '?' if it exists)
%rFirst line of the request (method and request URI)
%sHTTP status code of the response
%SUser session ID
%tDate and time, in Common Log Format
%uRemote user that was authenticated (if any), else '-'
%URequested URL path
%vLocal server name
%DTime taken to process the request, in millis
%TTime taken to process the request, in seconds
%Icurrent request thread name (can compare later with stacktraces)