|
Apache Tomcat 7.0.39 | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.apache.catalina.util.LifecycleBase org.apache.catalina.util.LifecycleMBeanBase org.apache.catalina.valves.ValveBase org.apache.catalina.valves.JDBCAccessLogValve
public final class 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="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 (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, 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.
Field Summary | |
---|---|
protected String |
connectionName
The connection username to use when trying to connect to the database. |
protected String |
connectionPassword
The connection URL to use when trying to connect to the database. |
protected Driver |
driver
Instance of the JDBC Driver class we use as a connection factory. |
protected static String |
info
The descriptive information about this implementation. |
protected boolean |
requestAttributesEnabled
|
protected boolean |
useLongContentLength
Use long contentLength as you have more 4 GB output. |
Fields inherited from class org.apache.catalina.valves.ValveBase |
---|
asyncSupported, container, containerLog, next, sm |
Fields inherited from class org.apache.catalina.util.LifecycleMBeanBase |
---|
mserver |
Fields inherited from interface org.apache.catalina.AccessLog |
---|
PROTOCOL_ATTRIBUTE, REMOTE_ADDR_ATTRIBUTE, REMOTE_HOST_ATTRIBUTE, SERVER_PORT_ATTRIBUTE |
Fields inherited from interface org.apache.catalina.Lifecycle |
---|
AFTER_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 Summary | |
---|---|
JDBCAccessLogValve()
Class constructor. |
Method Summary | |
---|---|
protected void |
close()
Close the specified database connection. |
String |
getConnectionName()
Return the username to use to connect to the database. |
String |
getConnectionPassword()
Return the password to use to connect to the database. |
long |
getCurrentTimeMillis()
|
boolean |
getRequestAttributesEnabled()
|
boolean |
getUseLongContentLength()
get useLongContentLength |
void |
invoke(Request request,
Response response)
This method is invoked by Tomcat on each query. |
void |
log(Request request,
Response response,
long time)
Add the request/response to the access log using the specified processing time. |
protected void |
open()
Open (if necessary) and return a database connection for use by this AccessLogValve. |
void |
setBytesField(String bytesField)
Sets the name of the field containing the number of bytes returned. |
void |
setConnectionName(String connectionName)
Set the username to use to connect to the database. |
void |
setConnectionPassword(String connectionPassword)
Set the password to use to connect to the database. |
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 |
setRequestAttributesEnabled(boolean requestAttributesEnabled)
Should this valve set request attributes for IP address, Hostname, protocol and port used for the request? |
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 |
setUseLongContentLength(boolean useLongContentLength)
|
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). |
protected void |
startInternal()
Start this component and implement the requirements of LifecycleBase.startInternal() . |
protected void |
stopInternal()
Stop this component and implement the requirements of LifecycleBase.stopInternal() . |
Methods inherited from class org.apache.catalina.valves.ValveBase |
---|
backgroundProcess, event, getContainer, getDomainInternal, getInfo, getNext, getObjectNameKeyProperties, initInternal, isAsyncSupported, setAsyncSupported, setContainer, setNext, toString |
Methods inherited from class org.apache.catalina.util.LifecycleMBeanBase |
---|
destroyInternal, getDomain, getObjectName, postDeregister, postRegister, preDeregister, preRegister, register, setDomain, unregister |
Methods inherited from class org.apache.catalina.util.LifecycleBase |
---|
addLifecycleListener, destroy, findLifecycleListeners, fireLifecycleEvent, getState, getStateName, init, removeLifecycleListener, setState, setState, start, stop |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
protected boolean useLongContentLength
protected String connectionName
protected String connectionPassword
protected Driver driver
protected boolean requestAttributesEnabled
setRequestAttributesEnabled(boolean)
protected static final String info
Constructor Detail |
---|
public JDBCAccessLogValve()
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 |
---|
public void setRequestAttributesEnabled(boolean requestAttributesEnabled)
AccessLogValve
which will otherwise log the original values.
Default is true
.
The attributes set are:
setRequestAttributesEnabled
in interface AccessLog
requestAttributesEnabled
- true
causes the attributes
to be set, false
disables
the setting of the attributes.public boolean getRequestAttributesEnabled()
getRequestAttributesEnabled
in interface AccessLog
true
if the attributes will be logged, otherwise
false
AccessLog.setRequestAttributesEnabled(boolean)
public String getConnectionName()
public void setConnectionName(String connectionName)
connectionName
- Usernamepublic void setDriverName(String driverName)
driverName
- The complete name of the database driver class.public String getConnectionPassword()
public void setConnectionPassword(String connectionPassword)
connectionPassword
- User passwordpublic void setConnectionURL(String connectionURL)
connectionURL
- The JDBC URL of the database.public void setTableName(String tableName)
tableName
- The name of the table.public void setRemoteHostField(String remoteHostField)
remoteHostField
- The name of the remote host field.public void setUserField(String userField)
userField
- The name of the remote user field.public void setTimestampField(String timestampField)
timestampField
- The name of the server-determined timestamp field.public void setVirtualHostField(String virtualHostField)
virtualHostField
- The name of the virtual host field.public void setMethodField(String methodField)
methodField
- The name of the HTTP request method field.public void setQueryField(String queryField)
queryField
- The name of the field containing the URL part of
the HTTP query.public void setStatusField(String statusField)
statusField
- The name of the HTTP response status code field.public void setBytesField(String bytesField)
bytesField
- The name of the returned bytes field.public void setRefererField(String refererField)
refererField
- The referer field name.public void setUserAgentField(String userAgentField)
userAgentField
- The name of the user agent field.public void setPattern(String pattern)
TO DO: more flexible field choices.
pattern
- The name of the logging pattern.public void setResolveHosts(String resolveHosts)
resolveHosts
- "true" or "false", if host IP resolution
is desired or not.public boolean getUseLongContentLength()
public void setUseLongContentLength(boolean useLongContentLength)
useLongContentLength
- the useLongContentLength to setpublic void invoke(Request request, Response response) throws IOException, ServletException
invoke
in interface Valve
invoke
in class ValveBase
request
- The Request object.response
- The Response object.
IOException
- Should not be thrown.
ServletException
- Database SQLException is wrapped
in a ServletException.public void log(Request request, Response response, long time)
AccessLog
log
in interface AccessLog
request
- Request (associated with the response) to logresponse
- Response (associated with the request) to logtime
- Time taken to process the request/response in
milliseconds (use 0 if not known)protected void open() throws SQLException
SQLException
- if a database error occursprotected void close()
protected void startInternal() throws LifecycleException
LifecycleBase.startInternal()
.
startInternal
in class ValveBase
LifecycleException
- if this component detects a fatal error
that prevents this component from being usedprotected void stopInternal() throws LifecycleException
LifecycleBase.stopInternal()
.
stopInternal
in class ValveBase
LifecycleException
- if this component detects a fatal error
that prevents this component from being usedpublic long getCurrentTimeMillis()
|
Apache Tomcat 7.0.39 | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |