Tomcat Mongo Access Log

Tomcat Mongo Access Log can be used to save tomcat access logs to mongodb. Before start you should got a mongodb installed (MongoDB installation guide).

This project is inspired by two articles:

  1. MongoDB is fantastic for logging
  2. MongoDB use case - storing log data

Besides, you can also use Tomcat Mongo Access Log Console to query access logs.

Usage

  1. Have a Apache Tomcat 7.0.54(or above) installed.
  2. Clone and build it with maven. mvn clean package
  3. copy target/tomcat-mongo-access-log-${version}.jar to ${TOMCAT_HOME}/lib.
  4. Configure MongoAccessLogValve in ${tomcat}/conf/server.xml, ${tomcat}/conf/context.xml or in a context file. Example:
    <Valve 
    className="me.chanjar.tomcat.valves.MongoAccessLogValve" 
    uri="mongodb://localhost/tomcat"
    pattern="default" />

If you want to develop based on this project, you can add following maven dependency to pom.xml:

<dependency>
  <groupId>me.chanjar</groupId>
  <artifactId>tomcat-mongo-access-log</artifactId>
  <version>1.0.0</version>
</dependency>

Attributes

MongoAccessLogValve supports the following configuration attributes of AccessLogValve :

  1. condition
  2. resolveHosts
  3. conditionIf
  4. excludes
  5. pattern. A little different from AccessLogValve, see below
  6. requestAttributesEnabled

And some attributes differs from AccessLogValve:

Attribute Description
uri MongoDB's uri (See MongoClientURI API)
dbName Which DB to store logs, default is tomcat
collName Which collection to store logs, default is tomcat_access_logs
excludes Default is ".js,.css,jpg,.jpeg,.gif,.png,.bmp,.gif,.html,.htm". Don't log the URI request matches some pattern
rotatable If rotatable is on, MongoDBAccessLogValve will try to create a capped collection with the size of capSize(default is 1024, in megabytes) if the collection is not exist
capSize The size of capped collection, in megabytes
rotateCount Not supported
recordError Default is true. MongoAccessLogValve will store exception stack traces in error key when exception throws

Collection Indexes

While it's hard to determine which fields should be indexed, but MongoAccessLogVavle provide the following indexes when it creates the Collection, so the query performance is not guaranteed. May be it's better to determine how to create index by yourself.

{ sessionId : 1}
{ datetime : -1}
{ user : 1}
{ statusCode : 1}
{ elapsedSeconds : 1}
{ elapsedMilliseconds : 1}
{ bytesSent : 1}
{ url : 1}
{ method : 1}

{ url : 1, datetime : -1}

{ user : 1, statusCode : 1}
{ user : 1, datetime : -1}
{ user : 1, sessionId : 1, statusCode : 1, datetime : -1}
{ user : 1, sessionId : 1, url : 1, statusCode : 1, datetime : -1}

{ sessionId : 1, datetime : -1}
{ sessionId : 1, statusCode : 1}
{ sessionId : 1, statusCode : 1, datetime : -1}
{ sessionId : 1, url : 1, statusCode : 1, datetime : -1}

Patterns

All the pattern of AccessLogValve are supported while MongoAccessLogValve save them in a JSON form.

Pattern Key Description
%a remoteIP See AccessLogValve (doc)
%A localIP See AccessLogValve (doc)
%b bytesSent See AccessLogValve (doc)
%B bytesSent See AccessLogValve (doc)
%h remoteHost See AccessLogValve (doc)
%H protocol See AccessLogValve (doc)
%l user See AccessLogValve (doc)
%m method See AccessLogValve (doc)
%p localPort See AccessLogValve (doc)
%q queryString See AccessLogValve (doc)
%r line1st See AccessLogValve (doc)
%s statusCode See AccessLogValve (doc)
%S sessionId See AccessLogValve (doc)
%t datetime See AccessLogValve (doc)
%t{format} datetime Same effect as %t
%u remoteUser See AccessLogValve (doc)
%U url See AccessLogValve (doc)
%v serverName See AccessLogValve (doc)
%D elapsedMillis See AccessLogValve (doc)
%T elapsedSeconds See AccessLogValve (doc)
%I thread See AccessLogValve (doc)
%P params All the GET and POST parameters, value is in the form of { param1 : value1, param2 : value2, params3 : [value1, value2, value3] }
%{xxx}i requestHeaders Request headers { header1 : value1, header2 : value2}. See AccessLogValve (doc)
%{xxx}o responseHeaders Response headers { header1 : value1, header2 : value2}. See AccessLogValve (doc)
%{xxx}c cookies Cookies { cookie1 : value1, cookie2 : value2}. See AccessLogValve (doc)
%{xxx}r requestAttrs Attributes in the ServletRequest { attr1 : value1, attr2 : value2}. See AccessLogValve (doc)
%{xxx}s sessionAttrs Attributes in the HttpSession { attr1 : value1, attr2 : value2}. See AccessLogValve (doc)
%{xxx}t not supported

Shorthand pattern

The shorthand patterns common and combined are also supported.

And MongoAccessLogValve provides two more shorthand patterns:

  1. default equivalent to %a %b %l %m %s %S %t %U %T %P %{Referer}i %{User-Agent}i
  2. all equivalent to %a %A %b %B %h %H %l %m %p %q %r %s %S %t %u %U %v %D %T %I %P %{Referer}i %{User-Agent}i