Tomcat – How to configure Tomcat to log requests before they are executed

loggingtomcat

We have Tomcat instance running on Ubuntu server. It runs a web service, open to the internet.

Sometimes it has a sudden spike of traffic and goes down. There is nothing unusual in Tomcat access logs. I guess it is because some of the requests are so 'heavy' that they never finish and hence are not recorded to Tomcat access logs.

Is there a way to configure Tomcat to log incoming requests before they are executed?
Each request should be logged in the following format

Date, Time, URL (with query string params), IP address (of client)

There should be one line per request. Only incoming requests to ports 80 and 443 should be logged.

Best Answer

Tomcat's AccessLogValve has the buffered configuration option which defaults to true, which means that if Tomcat crashes, you might lose the buffer's worth of logs. See http://tomcat.apache.org/tomcat-7.0-doc/config/valve.html#Access_Log_Valve.

You might want to try and set buffered to false, which would then make the valve flush the log on every request, bearing in mind that this will probably affect overall performance.

Another option is to place e.g. an Apache reverse proxy in front of Tomcat (using mod_proxy, mod_jk, mod_ajp or mod_cluster) and have it do the request logging.