|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.apache.catalina.valves.ValveBase org.apache.catalina.valves.JDBCAccessLogValve
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
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 (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.
Field Summary | |
protected static java.lang.String |
info
The descriptive information about this implementation. |
protected LifecycleSupport |
lifecycle
The lifecycle event support for this component. |
Fields inherited from class org.apache.catalina.valves.ValveBase |
container, debug |
Fields inherited from interface org.apache.catalina.Lifecycle |
AFTER_START_EVENT, AFTER_STOP_EVENT, BEFORE_START_EVENT, BEFORE_STOP_EVENT, START_EVENT, STOP_EVENT |
Constructor Summary | |
JDBCAccessLogValve()
Class constructor. |
Method Summary | |
void |
addLifecycleListener(LifecycleListener listener)
Adds a Lifecycle listener. |
LifecycleListener[] |
findLifecycleListeners()
Get the lifecycle listeners associated with this lifecycle. |
long |
getCurrentTimeMillis()
|
void |
invoke(Request request,
Response response,
ValveContext context)
This method is invoked by Tomcat on each query. |
void |
removeLifecycleListener(LifecycleListener listener)
Removes a Lifecycle listener. |
void |
setBytesField(java.lang.String bytesField)
Sets the name of the field containing the number of bytes returned. |
void |
setConnectionURL(java.lang.String connectionURL)
Sets the JDBC URL for the database where the log is stored. |
void |
setDriverName(java.lang.String driverName)
Sets the database driver name. |
void |
setMethodField(java.lang.String methodField)
Sets the name of the field containing the HTTP request method. |
void |
setPattern(java.lang.String pattern)
Sets the logging pattern. |
void |
setQueryField(java.lang.String queryField)
Sets the name of the field containing the URL part of the HTTP query. |
void |
setRefererField(java.lang.String refererField)
Sets the name of the field containing the referer. |
void |
setRemoteHostField(java.lang.String remoteHostField)
Sets the name of the field containing the remote host. |
void |
setResolveHosts(java.lang.String resolveHosts)
Determines whether IP host name resolution is done. |
void |
setStatusField(java.lang.String statusField)
Sets the name of the field containing the HTTP response status code. |
void |
setTableName(java.lang.String tableName)
Sets the name of the table where the logs are stored. |
void |
setTimestampField(java.lang.String timestampField)
Sets the name of the field containing the server-determined timestamp. |
void |
setUserAgentField(java.lang.String userAgentField)
Sets the name of the field containing the user agent. |
void |
setUserField(java.lang.String userField)
Sets the name of the field containing the remote user name. |
void |
setVirtualHostField(java.lang.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 |
getContainer, getDebug, getInfo, setContainer, setDebug |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
protected static java.lang.String info
protected LifecycleSupport lifecycle
Constructor Detail |
public JDBCAccessLogValve()
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 |
public void setDriverName(java.lang.String driverName)
driverName
- The complete name of the database driver class.public void setConnectionURL(java.lang.String connectionURL)
connectionURL
- The JDBC URL of the database.public void setTableName(java.lang.String tableName)
tableName
- The name of the table.public void setRemoteHostField(java.lang.String remoteHostField)
remoteHostField
- The name of the remote host field.public void setUserField(java.lang.String userField)
userField
- The name of the remote user field.public void setTimestampField(java.lang.String timestampField)
timestampField
- The name of the server-determined timestamp field.public void setVirtualHostField(java.lang.String virtualHostField)
virtualHostField
- The name of the virtual host field.public void setMethodField(java.lang.String methodField)
methodField
- The name of the HTTP request method field.public void setQueryField(java.lang.String queryField)
queryField
- The name of the field containing the URL part of
the HTTP query.public void setStatusField(java.lang.String statusField)
statusField
- The name of the HTTP response status code field.public void setBytesField(java.lang.String bytesField)
bytesField
- The name of the returned bytes field.public void setRefererField(java.lang.String refererField)
refererField
- The referer field name.public void setUserAgentField(java.lang.String userAgentField)
userAgentField
- The name of the user agent field.public void setPattern(java.lang.String pattern)
TO DO: more flexible field choices.
pattern
- The name of the logging pattern.public void setResolveHosts(java.lang.String resolveHosts)
resolveHosts
- "true" or "false", if host IP resolution
is desired or not.public void invoke(Request request, Response response, ValveContext context) throws java.io.IOException, javax.servlet.ServletException
invoke
in interface Valve
invoke
in class ValveBase
request
- The Request object.response
- The Response object.context
- The ValveContext object.
java.io.IOException
- Should not be thrown.
javax.servlet.ServletException
- Database SQLException is wrapped
in a ServletException.public void addLifecycleListener(LifecycleListener listener)
addLifecycleListener
in interface Lifecycle
listener
- The listener to add.public LifecycleListener[] findLifecycleListeners()
findLifecycleListeners
in interface Lifecycle
public void removeLifecycleListener(LifecycleListener listener)
removeLifecycleListener
in interface Lifecycle
listener
- The listener to remove.public void start() throws LifecycleException
start
in interface Lifecycle
LifecycleException
- Can be thrown on lifecycle
inconsistencies or on database errors (as a wrapped SQLException).public void stop() throws LifecycleException
stop
in interface Lifecycle
LifecycleException
- Can be thrown on lifecycle
inconsistencies or on database errors (as a wrapped SQLException).public long getCurrentTimeMillis()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |