Class JDBCAccessLogValve
- All Implemented Interfaces:
- MBeanRegistration,- AccessLog,- Contained,- JmxEnabled,- Lifecycle,- Valve
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, query VARCHAR(255) NOT NULL, status SMALLINT UNSIGNED NOT NULL, bytes INT UNSIGNED NOT NULL, virtualHost VARCHAR(64) NOT NULL, method VARCHAR(8) 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
 
- Author:
- Andre de Jesus, Peter Rossbach
- 
Nested Class SummaryNested classes/interfaces inherited from interface org.apache.catalina.LifecycleLifecycle.SingleUse
- 
Field SummaryFieldsModifier and TypeFieldDescriptionprotected StringDeprecated.protected ConnectionDeprecated.protected StringDeprecated.The connection username to use when trying to connect to the database.protected StringDeprecated.The connection URL to use when trying to connect to the database.protected StringDeprecated.protected longDeprecated.protected DriverDeprecated.Instance of the JDBC Driver class we use as a connection factory.protected StringDeprecated.protected StringDeprecated.protected StringDeprecated.protected PreparedStatementDeprecated.protected StringDeprecated.protected StringDeprecated.protected StringDeprecated.protected booleanDeprecated.Should this valve set request attributes for IP address, hostname, protocol and port used for the request.protected booleanDeprecated.protected StringDeprecated.protected StringDeprecated.protected StringDeprecated.protected booleanDeprecated.Use long contentLength as you have more 4 GB output.protected StringDeprecated.protected StringDeprecated.protected StringDeprecated.Fields inherited from class org.apache.catalina.valves.ValveBaseasyncSupported, container, containerLog, next, smFields inherited from class org.apache.catalina.util.LifecycleMBeanBasemserverFields inherited from interface org.apache.catalina.AccessLogPROTOCOL_ATTRIBUTE, REMOTE_ADDR_ATTRIBUTE, REMOTE_HOST_ATTRIBUTE, SERVER_NAME_ATTRIBUTE, SERVER_PORT_ATTRIBUTEFields 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
- 
Method SummaryModifier and TypeMethodDescriptionprotected voidclose()Deprecated.Close the specified database connection.Deprecated.Deprecated.longDeprecated.booleanDeprecated.booleanDeprecated.voidDeprecated.Perform request processing as required by this Valve.voidDeprecated.Add the request/response to the access log using the specified processing time.protected voidopen()Deprecated.Open (if necessary) and return a database connection for use by this AccessLogValve.protected voidprepare()Deprecated.Prepare tables for processing.voidsetBytesField(String bytesField) Deprecated.Sets the name of the field containing the number of bytes returned.voidsetConnectionName(String connectionName) Deprecated.Set the username to use to connect to the database.voidsetConnectionPassword(String connectionPassword) Deprecated.Set the password to use to connect to the database.voidsetConnectionURL(String connectionURL) Deprecated.Sets the JDBC URL for the database where the log is stored.voidsetDriverName(String driverName) Deprecated.Sets the database driver name.voidsetMethodField(String methodField) Deprecated.Sets the name of the field containing the HTTP request method.voidsetPattern(String pattern) Deprecated.Sets the logging pattern.voidsetQueryField(String queryField) Deprecated.Sets the name of the field containing the URL part of the HTTP query.voidsetRefererField(String refererField) Deprecated.Sets the name of the field containing the referer.voidsetRemoteHostField(String remoteHostField) Deprecated.Sets the name of the field containing the remote host.voidsetRequestAttributesEnabled(boolean requestAttributesEnabled) Deprecated.Should this valve use request attributes for IP address, hostname, protocol and port used for the request?voidsetResolveHosts(String resolveHosts) Deprecated.Determines whether IP host name resolution is done.voidsetStatusField(String statusField) Deprecated.Sets the name of the field containing the HTTP response status code.voidsetTableName(String tableName) Deprecated.Sets the name of the table where the logs are stored.voidsetTimestampField(String timestampField) Deprecated.Sets the name of the field containing the server-determined timestamp.voidsetUseLongContentLength(boolean useLongContentLength) Deprecated.voidsetUserAgentField(String userAgentField) Deprecated.Sets the name of the field containing the user agent.voidsetUserField(String userField) Deprecated.Sets the name of the field containing the remote user name.voidsetVirtualHostField(String virtualHostField) Deprecated.Sets the name of the field containing the virtual host information (this is in fact the server name).protected voidDeprecated.Start this component and implement the requirements ofLifecycleBase.startInternal().protected voidDeprecated.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, toStringMethods inherited from class org.apache.catalina.util.LifecycleMBeanBasedestroyInternal, getDomain, getObjectName, postDeregister, postRegister, preDeregister, preRegister, register, setDomain, unregister, unregisterMethods inherited from class org.apache.catalina.util.LifecycleBaseaddLifecycleListener, destroy, findLifecycleListeners, fireLifecycleEvent, getState, getStateName, getThrowOnFailure, init, removeLifecycleListener, setState, setState, setThrowOnFailure, start, stop
- 
Field Details- 
useLongContentLengthprotected boolean useLongContentLengthDeprecated.Use long contentLength as you have more 4 GB output.- Since:
- 6.0.15
 
- 
connectionNameDeprecated.The connection username to use when trying to connect to the database.
- 
connectionPasswordDeprecated.The connection URL to use when trying to connect to the database.
- 
driverDeprecated.Instance of the JDBC Driver class we use as a connection factory.
- 
driverNameDeprecated.
- 
connectionURLDeprecated.
- 
tableNameDeprecated.
- 
remoteHostFieldDeprecated.
- 
userFieldDeprecated.
- 
timestampFieldDeprecated.
- 
virtualHostFieldDeprecated.
- 
methodFieldDeprecated.
- 
queryFieldDeprecated.
- 
statusFieldDeprecated.
- 
bytesFieldDeprecated.
- 
refererFieldDeprecated.
- 
userAgentFieldDeprecated.
- 
patternDeprecated.
- 
resolveHostsprotected boolean resolveHostsDeprecated.
- 
connDeprecated.
- 
psDeprecated.
- 
currentTimeMillisprotected long currentTimeMillisDeprecated.
- 
requestAttributesEnabledprotected boolean requestAttributesEnabledDeprecated.Should this valve set request attributes for IP address, hostname, protocol and port used for the request. Default istrue.- See Also:
 
 
- 
- 
Constructor Details- 
JDBCAccessLogValvepublic JDBCAccessLogValve()Deprecated.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 Details- 
setRequestAttributesEnabledpublic void setRequestAttributesEnabled(boolean requestAttributesEnabled) Deprecated.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()Deprecated.- Specified by:
- getRequestAttributesEnabledin interface- AccessLog
- Returns:
- trueif the attributes will be logged, otherwise- false
- See Also:
 
- 
getConnectionNameDeprecated.- Returns:
- the username to use to connect to the database.
 
- 
setConnectionNameDeprecated.Set the username to use to connect to the database.- Parameters:
- connectionName- Username
 
- 
setDriverNameDeprecated.Sets the database driver name.- Parameters:
- driverName- The complete name of the database driver class.
 
- 
getConnectionPasswordDeprecated.- Returns:
- the password to use to connect to the database.
 
- 
setConnectionPasswordDeprecated.Set the password to use to connect to the database.- Parameters:
- connectionPassword- User password
 
- 
setConnectionURLDeprecated.Sets the JDBC URL for the database where the log is stored.- Parameters:
- connectionURL- The JDBC URL of the database.
 
- 
setTableNameDeprecated.Sets the name of the table where the logs are stored.- Parameters:
- tableName- The name of the table.
 
- 
setRemoteHostFieldDeprecated.Sets the name of the field containing the remote host.- Parameters:
- remoteHostField- The name of the remote host field.
 
- 
setUserFieldDeprecated.Sets the name of the field containing the remote user name.- Parameters:
- userField- The name of the remote user field.
 
- 
setTimestampFieldDeprecated.Sets the name of the field containing the server-determined timestamp.- Parameters:
- timestampField- The name of the server-determined timestamp field.
 
- 
setVirtualHostFieldDeprecated.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.
 
- 
setMethodFieldDeprecated.Sets the name of the field containing the HTTP request method.- Parameters:
- methodField- The name of the HTTP request method field.
 
- 
setQueryFieldDeprecated.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.
 
- 
setStatusFieldDeprecated.Sets the name of the field containing the HTTP response status code.- Parameters:
- statusField- The name of the HTTP response status code field.
 
- 
setBytesFieldDeprecated.Sets the name of the field containing the number of bytes returned.- Parameters:
- bytesField- The name of the returned bytes field.
 
- 
setRefererFieldDeprecated.Sets the name of the field containing the referer.- Parameters:
- refererField- The referer field name.
 
- 
setUserAgentFieldDeprecated.Sets the name of the field containing the user agent.- Parameters:
- userAgentField- The name of the user agent field.
 
- 
setPatternDeprecated.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.
 
- 
setResolveHostsDeprecated.Determines whether IP host name resolution is done.- Parameters:
- resolveHosts- "true" or "false", if host IP resolution is desired or not.
 
- 
getUseLongContentLengthpublic boolean getUseLongContentLength()Deprecated.- Returns:
- trueif content length should be considered a long rather than an int, defaults to- false
 
- 
setUseLongContentLengthpublic void setUseLongContentLength(boolean useLongContentLength) Deprecated.- Parameters:
- useLongContentLength- the useLongContentLength to set
 
- 
invokeDeprecated.Description copied from interface:ValvePerform request processing as required by this Valve. An individual Valve MAY perform the following actions, in the specified order: - Examine and/or modify the properties of the specified Request and Response.
- Examine the properties of the specified Request, completely generate the corresponding Response, and return control to the caller.
- Examine the properties of the specified Request and Response, wrap either or both of these objects to supplement their functionality, and pass them on.
- If the corresponding Response was not generated (and control was not returned, call the next Valve in the
 pipeline (if there is one) by executing getNext().invoke().
- Examine, but not modify, the properties of the resulting Response (which was created by a subsequently invoked Valve or Container).
 A Valve MUST NOT do any of the following things: - Change request properties that have already been used to direct the flow of processing control for this request (for instance, trying to change the virtual host to which a Request should be sent from a pipeline attached to a Host or Context in the standard implementation).
- Create a completed Response AND pass this Request and Response on to the next Valve in the pipeline.
- Consume bytes from the input stream associated with the Request, unless it is completely generating the response, or wrapping the request before passing it on.
- Modify the HTTP headers included with the Response after the getNext().invoke()method has returned.
- Perform any actions on the output stream associated with the specified Response after the
 getNext().invoke()method has returned.
 - Specified by:
- invokein interface- Valve
- Parameters:
- request- The servlet request to be processed
- response- The servlet response to be created
- Throws:
- IOException- if an input/output error occurs, or is thrown by a subsequently invoked Valve, Filter, or Servlet
- ServletException- if a servlet error occurs, or is thrown by a subsequently invoked Valve, Filter, or Servlet
 
- 
logDeprecated.Description copied from interface:AccessLogAdd the request/response to the access log using the specified processing time.
- 
openDeprecated.Open (if necessary) and return a database connection for use by this AccessLogValve.- Throws:
- SQLException- if a database error occurs
 
- 
prepareDeprecated.Prepare tables for processing. Used by subclasses for testing.- Throws:
- SQLException- if an exception occurs
 
- 
closeprotected void close()Deprecated.Close the specified database connection.
- 
startInternalDeprecated.Start 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
 
- 
stopInternalDeprecated.Stop 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()Deprecated.
 
-