Thursday, August 9, 2012

Customizing Tomcat access log

By default Tomcat logs every web request it receives in $TOMCAT_HOME/logs folder in a file called  "localhost_access_log<Date>.txt" and it does creates one log file for every day.
By default it captures the request IP address, time of the request, bytes sent, status of request etc.
If you want to change it, for example let say you are using an reverse proxy server and wants to log the actual client IP address present in "X-Forwarded-For" then you need to specify the same in $TOMCAT_HOME/config/server.xml file.

Look for the Valve element and additional parameters you want to capture to the pattern element. You can as well configure log file name, location over here.


        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log." suffix=".txt"
               pattern="%a %{X-Forwarded-For}i %T %t  %s %b" />


For more configuration options refer to Tomcat Documentation. http://tomcat.apache.org/tomcat-7.0-doc/config/valve.html

For your quick reference(From Apache Documentation mentioned above).



Values for the pattern attribute are made up of literal text strings, combined with pattern identifiers prefixed by the "%" character to cause replacement by the corresponding variable value from the current request and response. The following pattern codes are supported:
  • %a - Remote IP address
  • %A - Local IP address
  • %b - Bytes sent, excluding HTTP headers, or '-' if zero
  • %B - Bytes sent, excluding HTTP headers
  • %h - Remote host name (or IP address if enableLookups for the connector is false)
  • %H - Request protocol
  • %l - Remote logical username from identd (always returns '-')
  • %m - Request method (GET, POST, etc.)
  • %p - Local port on which this request was received
  • %q - Query string (prepended with a '?' if it exists)
  • %r - First line of the request (method and request URI)
  • %s - HTTP status code of the response
  • %S - User session ID
  • %t - Date and time, in Common Log Format
  • %u - Remote user that was authenticated (if any), else '-'
  • %U - Requested URL path
  • %v - Local server name
  • %D - Time taken to process the request, in millis
  • %T - Time taken to process the request, in seconds
  • %I - Current request thread name (can compare later with stacktraces)
There is also support to write information incoming or outgoing headers, cookies, session or request attributes and special timestamp formats. It is modeled after the Apache HTTP Serverlog configuration syntax:
  • %{xxx}i for incoming headers
  • %{xxx}o for outgoing response headers
  • %{xxx}c for a specific cookie
  • %{xxx}r xxx is an attribute in the ServletRequest
  • %{xxx}s xxx is an attribute in the HttpSession
  • %{xxx}t xxx is an enhanced SimpleDateFormat pattern

No comments:

Post a Comment