Class JDBCAccessLogValve
- java.lang.Object
-
- org.apache.catalina.valves.ValveBase
-
- org.apache.catalina.valves.JDBCAccessLogValve
-
- All Implemented Interfaces:
Contained
,Lifecycle
,Valve
,GlassFishValve
public final class JDBCAccessLogValve extends ValveBase
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="AccessLogDBValve" driverName="your_jdbc_driver" connectionURL="your_jdbc_url" pattern="combined" resolveHosts="false" />
Many parameters can be configured, such as the database connection (with
driverName
andconnectionURL
), the table name (tableName
) and the field names (corresponding to the get/set method names). The same options as AccessLogValve are supported, such asresolveHosts
andpattern
("common" or "combined" only).When Tomcat is started, a database connection (with autoReconnect option) 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, ts TIMESTAMP NOT NULL, remoteHost CHAR(15) NOT NULL, user 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 (ts), INDEX (remoteHost), INDEX (virtualHost), INDEX (query), INDEX (userAgent) );
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.
- Version:
- 1.0
- Author:
- Andre de Jesus
-
-
Field Summary
-
Fields inherited from class org.apache.catalina.valves.ValveBase
container, controller, debug, domain, lifecycle, log, next, oname, rb, started
-
Fields inherited from interface org.glassfish.web.valve.GlassFishValve
END_PIPELINE, INVOKE_NEXT
-
Fields inherited from interface org.apache.catalina.Lifecycle
AFTER_START_EVENT, AFTER_STOP_EVENT, BEFORE_START_EVENT, BEFORE_STOP_EVENT, INIT_EVENT, START_EVENT, STOP_EVENT
-
-
Constructor Summary
Constructors Constructor Description JDBCAccessLogValve()
Class constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description long
getCurrentTimeMillis()
int
invoke(Request request, Response response)
This method is invoked by Tomcat on each query.void
postInvoke(Request request, Response response)
A post-request processing implementation that does nothing.void
setBytesField(String bytesField)
Sets the name of the field containing the number of bytes returned.void
setConnectionURL(String connectionURL)
Sets the JDBC URL for the database where the log is stored.void
setDriverName(String driverName)
Sets the database driver name.void
setMethodField(String methodField)
Sets the name of the field containing the HTTP request method.void
setPattern(String pattern)
Sets the logging pattern.void
setQueryField(String queryField)
Sets the name of the field containing the URL part of the HTTP query.void
setRefererField(String refererField)
Sets the name of the field containing the referer.void
setRemoteHostField(String remoteHostField)
Sets the name of the field containing the remote host.void
setResolveHosts(String resolveHosts)
Determines whether IP host name resolution is done.void
setStatusField(String statusField)
Sets the name of the field containing the HTTP response status code.void
setTableName(String tableName)
Sets the name of the table where the logs are stored.void
setTimestampField(String timestampField)
Sets the name of the field containing the server-determined timestamp.void
setUserAgentField(String userAgentField)
Sets the name of the field containing the user agent.void
setUserField(String userField)
Sets the name of the field containing the remote user name.void
setVirtualHostField(String virtualHostField)
Sets the name of the field containing the virtual host information (this is in fact the server name).void
start()
Invoked by Tomcat on startup.void
stop()
Invoked by tomcat on shutdown.-
Methods inherited from class org.apache.catalina.valves.ValveBase
addLifecycleListener, backgroundProcess, createObjectName, event, findLifecycleListeners, getContainer, getController, getDebug, getDomain, getInfo, getNext, getObjectName, getParentName, invoke, isStarted, removeLifecycleListener, setContainer, setController, setDebug, setNext, setObjectName
-
-
-
-
Constructor Detail
-
JDBCAccessLogValve
public JDBCAccessLogValve()
Class constructor. Initializes the fields with the default values. The defaults are:driverName = null; connectionURL = null; tableName = "access"; remoteHostField = "remoteHost"; userField = "user"; timestampField = "timestamp"; virtualHostField = "virtualHost"; methodField = "method"; queryField = "query"; statusField = "status"; bytesField = "bytes"; refererField = "referer"; userAgentField = "userAgent"; pattern = "common"; resolveHosts = false;
-
-
Method Detail
-
setDriverName
public void setDriverName(String driverName)
Sets the database driver name.- Parameters:
driverName
- The complete name of the database driver class.
-
setConnectionURL
public void setConnectionURL(String connectionURL)
Sets the JDBC URL for the database where the log is stored.- Parameters:
connectionURL
- The JDBC URL of the database.
-
setTableName
public void setTableName(String tableName)
Sets the name of the table where the logs are stored.- Parameters:
tableName
- The name of the table.
-
setRemoteHostField
public void setRemoteHostField(String remoteHostField)
Sets the name of the field containing the remote host.- Parameters:
remoteHostField
- The name of the remote host field.
-
setUserField
public void setUserField(String userField)
Sets the name of the field containing the remote user name.- Parameters:
userField
- The name of the remote user field.
-
setTimestampField
public void setTimestampField(String timestampField)
Sets the name of the field containing the server-determined timestamp.- Parameters:
timestampField
- The name of the server-determined timestamp field.
-
setVirtualHostField
public void setVirtualHostField(String virtualHostField)
Sets the name of the field containing the virtual host information (this is in fact the server name).- Parameters:
virtualHostField
- The name of the virtual host field.
-
setMethodField
public void setMethodField(String methodField)
Sets the name of the field containing the HTTP request method.- Parameters:
methodField
- The name of the HTTP request method field.
-
setQueryField
public void setQueryField(String queryField)
Sets the name of the field containing the URL part of the HTTP query.- Parameters:
queryField
- The name of the field containing the URL part of the HTTP query.
-
setStatusField
public void setStatusField(String statusField)
Sets the name of the field containing the HTTP response status code.- Parameters:
statusField
- The name of the HTTP response status code field.
-
setBytesField
public void setBytesField(String bytesField)
Sets the name of the field containing the number of bytes returned.- Parameters:
bytesField
- The name of the returned bytes field.
-
setRefererField
public void setRefererField(String refererField)
Sets the name of the field containing the referer.- Parameters:
refererField
- The referer field name.
-
setUserAgentField
public void setUserAgentField(String userAgentField)
Sets the name of the field containing the user agent.- Parameters:
userAgentField
- The name of the user agent field.
-
setPattern
public void setPattern(String pattern)
Sets the logging pattern. The patterns supported correspond to the file-based "common" and "combined". These are translated into the use of tables containing either set of fields.TO DO: more flexible field choices.
- Parameters:
pattern
- The name of the logging pattern.
-
setResolveHosts
public void setResolveHosts(String resolveHosts)
Determines whether IP host name resolution is done.- Parameters:
resolveHosts
- "true" or "false", if host IP resolution is desired or not.
-
invoke
public int invoke(Request request, Response response) throws IOException, jakarta.servlet.ServletException
This method is invoked by Tomcat on each query.- Specified by:
invoke
in interfaceGlassFishValve
- Specified by:
invoke
in classValveBase
- Parameters:
request
- The Request object.response
- The Response object.- Returns:
INVOKE_NEXT
orEND_PIPELINE
- Throws:
IOException
- Should not be thrown.jakarta.servlet.ServletException
- Database SQLException is wrapped in a ServletException.
-
postInvoke
public void postInvoke(Request request, Response response) throws IOException, jakarta.servlet.ServletException
Description copied from class:ValveBase
A post-request processing implementation that does nothing. Very few Valves override this behaviour as most Valve logic is used for request processing.- Specified by:
postInvoke
in interfaceGlassFishValve
- Overrides:
postInvoke
in classValveBase
- Parameters:
request
- The servlet request to be processedresponse
- The servlet response to be created- Throws:
IOException
- if an input/output error occursjakarta.servlet.ServletException
- if a servlet error occurs
-
start
public void start() throws LifecycleException
Invoked by Tomcat on startup. The database connection is set here.- Specified by:
start
in interfaceLifecycle
- Overrides:
start
in classValveBase
- Throws:
LifecycleException
- Can be thrown on lifecycle inconsistencies or on database errors (as a wrapped SQLException).
-
stop
public void stop() throws LifecycleException
Invoked by tomcat on shutdown. The database connection is closed here.- Specified by:
stop
in interfaceLifecycle
- Overrides:
stop
in classValveBase
- Throws:
LifecycleException
- Can be thrown on lifecycle inconsistencies or on database errors (as a wrapped SQLException).
-
getCurrentTimeMillis
public long getCurrentTimeMillis()
-
-