将 Tomcat access log 记录到 MongoDB 的插件:Tomcat Mongo Access Log

jopen 10年前

Tomcat Mongo Access Log 是一个将 Tomcat access log 记录到 MongoDB 的插件。

使用方式和Tomcat自带的AccessLogValve基本一致,只需要简单配置就能够实现将日志存入到mongodb的功能。

例子(替换掉server.xml中原来的AccessLogValve配置):

<!--  <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"         prefix="localhost_access_log." suffix=".txt"        pattern="%h %l %u %t &quot;%r&quot; %s %b" />  -->  <Valve       className="chanjarster.tomcat.valves.MongoAccessLogValve"       uri="mongodb://url/to/mongodb"      pattern="all" />

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

 

项目主页:http://www.open-open.com/lib/view/home/1405735009328