Class JDBCAccessLogValve

java.lang.Object
All Implemented Interfaces:
MBeanRegistration, AccessLog, Contained, JmxEnabled, Lifecycle, Valve

public final class JDBCAccessLogValve extends ValveBase implements AccessLog

This Tomcat extension logs server access directly to a database, and can be used instead of the regular file-based access log implemented in AccessLogValve. To use, copy into the server/classes directory of the Tomcat installation and configure in server.xml as:

      <Valve className="org.apache.catalina.valves.JDBCAccessLogValve"
          driverName="your_jdbc_driver"
          connectionURL="your_jdbc_url"
          pattern="combined" resolveHosts="false"
      />
 

Many parameters can be configured, such as the database connection (with driverName and connectionURL), the table name (tableName) and the field names (corresponding to the get/set method names). The same options as AccessLogValve are supported, such as resolveHosts and pattern ("common" or "combined" only).

When Tomcat is started, a database connection is created and used for all the log activity. When Tomcat is shutdown, the database connection is closed. This logger can be used at the level of the Engine context (being shared by all the defined hosts) or the Host context (one instance of the logger per host, possibly using different databases).

The database table can be created with the following command:

 CREATE TABLE access (
 id INT UNSIGNED AUTO_INCREMENT NOT NULL,
 remoteHost CHAR(15) NOT NULL,
 userName CHAR(15),
 timestamp TIMESTAMP NOT NULL,
 virtualHost VARCHAR(64) NOT NULL,
 method VARCHAR(8) NOT NULL,
 query VARCHAR(255) NOT NULL,
 status SMALLINT UNSIGNED NOT NULL,
 bytes INT UNSIGNED NOT NULL,
 referer VARCHAR(128),
 userAgent VARCHAR(128),
 PRIMARY KEY (id),
 INDEX (timestamp),
 INDEX (remoteHost),
 INDEX (virtualHost),
 INDEX (query),
 INDEX (userAgent)
 );
 

Set JDBCAccessLogValve attribute useLongContentLength="true" as you have more then 4GB outputs. Please, use long SQL datatype at access.bytes attribute. The datatype of bytes at oracle is number and other databases use bytes BIGINT NOT NULL.

If the table is created as above, its name and the field names don't need to be defined.

If the request method is "common", only these fields are used: remoteHost, user, timeStamp, query, status, bytes

TO DO: provide option for excluding logging of certain MIME types.

Author:
Andre de Jesus, Peter Rossbach