tomcat access log 的配置对应 server.xml 的 Valve节点属性值className为 org.apache.catalina.valves.AccessLogValve的配置。
几个属性中,className为对应处理类,directory为目录,pattern为日志输出模式,prefix为日志文件前缀,suffix为日志文件后缀。pattern是复杂的,其他很好理解。
配置如:
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" pattern="%h %l %u %t "%r" %s %b" prefix="localhost_access_log." suffix=".txt"/>
其中pattern的配置值(如:%h %l %u %t "%r" %s %b)的每个格式详细为关键,熟悉后可以根据实际需要灵活配置,了解其中含义也是理解access日志的前提。
以下,是pattern属性值各模式配置的详细描述(实际上注释在其直接抽象父类org.apache.catalina.valves.AbstractAccessLogValve):
Abstract implementation of the Valve interface that generates a web server access log with the detailed line contents matching a configurable pattern. The syntax of the available patterns is similar to that supported by the Apache HTTP Server
mod_log_config
module.Patterns for the logged message may include constant text or any of the following replacement strings, for which the corresponding information from the specified Response is substituted:
%a
- Remote IP address%A
- Local IP address%b
- Bytes sent, excluding HTTP headers, or '-' if no bytes were sent%B
- Bytes sent, excluding HTTP headers%h
- Remote host name (or IP address ifenableLookups
for the connector is false)%H
- Request protocol%l
- Remote logical username from identd (always returns '-')%m
- Request method%p
- Local port%q
- Query string (prepended with a '?' if it exists, otherwise an empty string%r
- First line of the request%s
- HTTP status code of the response%S
- User session ID%t
- Date and time, in Common Log Format format%u
- Remote user that was authenticated%U
- Requested URL path%v
- Local server name%D
- Time taken to process the request, in millis%T
- Time taken to process the request, in seconds%F
- Time taken to commit the response, in millis%I
- current Request thread name (can compare later with stacktraces)%X
- Connection status when response is completed:
X
= Connection aborted before the response completed.+
= Connection may be kept alive after the response is sent.-
= Connection will be closed after the response is sent.In addition, the caller can specify one of the following aliases for commonly utilized patterns:
- common -
%h %l %u %t "%r" %s %b
- combined -
%h %l %u %t "%r" %s %b "%{Referer}i" "%{User-Agent}i"
There is also support to write information from the cookie, incoming header, the Session or something else in the ServletRequest.
It is modeled after the Apache HTTP Server log configuration syntax:
%{xxx}i
for incoming headers%{xxx}o
for outgoing response headers%{xxx}c
for a specific cookie%{xxx}r
xxx is an attribute in the ServletRequest%{xxx}s
xxx is an attribute in the HttpSession%{xxx}t
xxx is an enhanced SimpleDateFormat pattern (see Configuration Reference document for details on supported time patterns)Conditional logging is also supported. This can be done with the
conditionUnless
andconditionIf
properties. If the value returned from ServletRequest.getAttribute(conditionUnless) yields a non-null value, the logging will be skipped. If the value returned from ServletRequest.getAttribute(conditionIf) yields the null value, the logging will be skipped. Thecondition
attribute is synonym forconditionUnless
and is provided for backwards compatibility.For extended attributes coming from a getAttribute() call, it is you responsibility to ensure there are no newline or control characters.