Class JDBCAccessLogValve
- java.lang.Object
- 
- org.apache.catalina.util.LifecycleBase
- 
- org.apache.catalina.util.LifecycleMBeanBase
- 
- org.apache.catalina.valves.ValveBase
- 
- org.apache.catalina.valves.JDBCAccessLogValve
 
 
 
 
- 
- All Implemented Interfaces:
- javax.management.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 driverNameandconnectionURL), the table name (tableName) and the field names (corresponding to the get/set method names). The same options as AccessLogValve are supported, such asresolveHostsandpattern("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, bytesTO DO: provide option for excluding logging of certain MIME types. - Author:
- Andre de Jesus, Peter Rossbach
 
- 
- 
Nested Class Summary- 
Nested classes/interfaces inherited from interface org.apache.catalina.LifecycleLifecycle.SingleUse
 
- 
 - 
Field Summary- 
Fields inherited from class org.apache.catalina.valves.ValveBaseasyncSupported, container, containerLog, next, sm
 - 
Fields inherited from class org.apache.catalina.util.LifecycleMBeanBasemserver
 - 
Fields inherited from interface org.apache.catalina.AccessLogPROTOCOL_ATTRIBUTE, REMOTE_ADDR_ATTRIBUTE, REMOTE_HOST_ATTRIBUTE, SERVER_NAME_ATTRIBUTE, SERVER_PORT_ATTRIBUTE
 - 
Fields inherited from interface org.apache.catalina.LifecycleAFTER_DESTROY_EVENT, AFTER_INIT_EVENT, AFTER_START_EVENT, AFTER_STOP_EVENT, BEFORE_DESTROY_EVENT, BEFORE_INIT_EVENT, BEFORE_START_EVENT, BEFORE_STOP_EVENT, CONFIGURE_START_EVENT, CONFIGURE_STOP_EVENT, PERIODIC_EVENT, START_EVENT, STOP_EVENT
 
- 
 - 
Constructor SummaryConstructors Constructor Description JDBCAccessLogValve()Class constructor.
 - 
Method SummaryAll Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidclose()Close the specified database connection.java.lang.StringgetConnectionName()java.lang.StringgetConnectionPassword()longgetCurrentTimeMillis()booleangetRequestAttributesEnabled()booleangetUseLongContentLength()voidinvoke(Request request, Response response)This method is invoked by Tomcat on each query.voidlog(Request request, Response response, long time)Add the request/response to the access log using the specified processing time.protected voidopen()Open (if necessary) and return a database connection for use by this AccessLogValve.voidsetBytesField(java.lang.String bytesField)Sets the name of the field containing the number of bytes returned.voidsetConnectionName(java.lang.String connectionName)Set the username to use to connect to the database.voidsetConnectionPassword(java.lang.String connectionPassword)Set the password to use to connect to the database.voidsetConnectionURL(java.lang.String connectionURL)Sets the JDBC URL for the database where the log is stored.voidsetDriverName(java.lang.String driverName)Sets the database driver name.voidsetMethodField(java.lang.String methodField)Sets the name of the field containing the HTTP request method.voidsetPattern(java.lang.String pattern)Sets the logging pattern.voidsetQueryField(java.lang.String queryField)Sets the name of the field containing the URL part of the HTTP query.voidsetRefererField(java.lang.String refererField)Sets the name of the field containing the referer.voidsetRemoteHostField(java.lang.String remoteHostField)Sets the name of the field containing the remote host.voidsetRequestAttributesEnabled(boolean requestAttributesEnabled)Should this valve use request attributes for IP address, hostname, protocol and port used for the request?voidsetResolveHosts(java.lang.String resolveHosts)Determines whether IP host name resolution is done.voidsetStatusField(java.lang.String statusField)Sets the name of the field containing the HTTP response status code.voidsetTableName(java.lang.String tableName)Sets the name of the table where the logs are stored.voidsetTimestampField(java.lang.String timestampField)Sets the name of the field containing the server-determined timestamp.voidsetUseLongContentLength(boolean useLongContentLength)voidsetUserAgentField(java.lang.String userAgentField)Sets the name of the field containing the user agent.voidsetUserField(java.lang.String userField)Sets the name of the field containing the remote user name.voidsetVirtualHostField(java.lang.String virtualHostField)Sets the name of the field containing the virtual host information (this is in fact the server name).protected voidstartInternal()Start this component and implement the requirements ofLifecycleBase.startInternal().protected voidstopInternal()Stop this component and implement the requirements ofLifecycleBase.stopInternal().- 
Methods inherited from class org.apache.catalina.valves.ValveBasebackgroundProcess, getContainer, getDomainInternal, getNext, getObjectNameKeyProperties, initInternal, isAsyncSupported, setAsyncSupported, setContainer, setNext, toString
 - 
Methods inherited from class org.apache.catalina.util.LifecycleMBeanBasedestroyInternal, getDomain, getObjectName, postDeregister, postRegister, preDeregister, preRegister, register, setDomain, unregister, unregister
 - 
Methods inherited from class org.apache.catalina.util.LifecycleBaseaddLifecycleListener, destroy, findLifecycleListeners, fireLifecycleEvent, getState, getStateName, getThrowOnFailure, init, removeLifecycleListener, setState, setState, setThrowOnFailure, start, stop
 
- 
 
- 
- 
- 
Constructor Detail- 
JDBCAccessLogValvepublic JDBCAccessLogValve() Class constructor. Initializes the fields with the default values. The defaults are:driverName = null; connectionURL = null; tableName = "access"; remoteHostField = "remoteHost"; userField = "userName"; timestampField = "timestamp"; virtualHostField = "virtualHost"; methodField = "method"; queryField = "query"; statusField = "status"; bytesField = "bytes"; refererField = "referer"; userAgentField = "userAgent"; pattern = "common"; resolveHosts = false; 
 
- 
 - 
Method Detail- 
setRequestAttributesEnabledpublic void setRequestAttributesEnabled(boolean requestAttributesEnabled) Should this valve use request attributes for IP address, hostname, protocol and port used for the request? The attributes used are:- org.apache.catalina.RemoteAddr
- org.apache.catalina.RemoteHost
- org.apache.catalina.Protocol
- org.apache.catalina.ServerName
- org.apache.catalina.ServerPost
 true.- Specified by:
- setRequestAttributesEnabledin interface- AccessLog
- Parameters:
- requestAttributesEnabled-- truecauses the attributes to be used,- falsecauses the original values to be used.
 
 - 
getRequestAttributesEnabledpublic boolean getRequestAttributesEnabled() - Specified by:
- getRequestAttributesEnabledin interface- AccessLog
- Returns:
- trueif the attributes will be logged, otherwise- false
- See Also:
- AccessLog.setRequestAttributesEnabled(boolean)
 
 - 
getConnectionNamepublic java.lang.String getConnectionName() - Returns:
- the username to use to connect to the database.
 
 - 
setConnectionNamepublic void setConnectionName(java.lang.String connectionName) Set the username to use to connect to the database.- Parameters:
- connectionName- Username
 
 - 
setDriverNamepublic void setDriverName(java.lang.String driverName) Sets the database driver name.- Parameters:
- driverName- The complete name of the database driver class.
 
 - 
getConnectionPasswordpublic java.lang.String getConnectionPassword() - Returns:
- the password to use to connect to the database.
 
 - 
setConnectionPasswordpublic void setConnectionPassword(java.lang.String connectionPassword) Set the password to use to connect to the database.- Parameters:
- connectionPassword- User password
 
 - 
setConnectionURLpublic void setConnectionURL(java.lang.String connectionURL) Sets the JDBC URL for the database where the log is stored.- Parameters:
- connectionURL- The JDBC URL of the database.
 
 - 
setTableNamepublic void setTableName(java.lang.String tableName) Sets the name of the table where the logs are stored.- Parameters:
- tableName- The name of the table.
 
 - 
setRemoteHostFieldpublic void setRemoteHostField(java.lang.String remoteHostField) Sets the name of the field containing the remote host.- Parameters:
- remoteHostField- The name of the remote host field.
 
 - 
setUserFieldpublic void setUserField(java.lang.String userField) Sets the name of the field containing the remote user name.- Parameters:
- userField- The name of the remote user field.
 
 - 
setTimestampFieldpublic void setTimestampField(java.lang.String timestampField) Sets the name of the field containing the server-determined timestamp.- Parameters:
- timestampField- The name of the server-determined timestamp field.
 
 - 
setVirtualHostFieldpublic void setVirtualHostField(java.lang.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.
 
 - 
setMethodFieldpublic void setMethodField(java.lang.String methodField) Sets the name of the field containing the HTTP request method.- Parameters:
- methodField- The name of the HTTP request method field.
 
 - 
setQueryFieldpublic void setQueryField(java.lang.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.
 
 - 
setStatusFieldpublic void setStatusField(java.lang.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.
 
 - 
setBytesFieldpublic void setBytesField(java.lang.String bytesField) Sets the name of the field containing the number of bytes returned.- Parameters:
- bytesField- The name of the returned bytes field.
 
 - 
setRefererFieldpublic void setRefererField(java.lang.String refererField) Sets the name of the field containing the referer.- Parameters:
- refererField- The referer field name.
 
 - 
setUserAgentFieldpublic void setUserAgentField(java.lang.String userAgentField) Sets the name of the field containing the user agent.- Parameters:
- userAgentField- The name of the user agent field.
 
 - 
setPatternpublic void setPattern(java.lang.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.
 
 - 
setResolveHostspublic void setResolveHosts(java.lang.String resolveHosts) Determines whether IP host name resolution is done.- Parameters:
- resolveHosts- "true" or "false", if host IP resolution is desired or not.
 
 - 
getUseLongContentLengthpublic boolean getUseLongContentLength() - Returns:
- trueif content length should be considered a long rather than an int, defaults to- false
 
 - 
setUseLongContentLengthpublic void setUseLongContentLength(boolean useLongContentLength) - Parameters:
- useLongContentLength- the useLongContentLength to set
 
 - 
invokepublic void invoke(Request request, Response response) throws java.io.IOException, ServletException This method is invoked by Tomcat on each query.- Specified by:
- invokein interface- Valve
- Parameters:
- request- The Request object.
- response- The Response object.
- Throws:
- java.io.IOException- Should not be thrown.
- ServletException- Database SQLException is wrapped in a ServletException.
 
 - 
logpublic void log(Request request, Response response, long time) Description copied from interface:AccessLogAdd the request/response to the access log using the specified processing time.
 - 
openprotected void open() throws java.sql.SQLExceptionOpen (if necessary) and return a database connection for use by this AccessLogValve.- Throws:
- java.sql.SQLException- if a database error occurs
 
 - 
closeprotected void close() Close the specified database connection.
 - 
startInternalprotected void startInternal() throws LifecycleExceptionStart this component and implement the requirements ofLifecycleBase.startInternal().- Overrides:
- startInternalin class- ValveBase
- Throws:
- LifecycleException- if this component detects a fatal error that prevents this component from being used
 
 - 
stopInternalprotected void stopInternal() throws LifecycleExceptionStop this component and implement the requirements ofLifecycleBase.stopInternal().- Overrides:
- stopInternalin class- ValveBase
- Throws:
- LifecycleException- if this component detects a fatal error that prevents this component from being used
 
 - 
getCurrentTimeMillispublic long getCurrentTimeMillis() 
 
- 
 
-