Tomcat配置访问日志 日志支持的属性 格式字符 特殊说明

2016-11-06 19:55:00
admin
原创 2420
摘要:Tomcat配置访问日志 日志支持的属性 格式字符 特殊说明

一、Tomcat配置访问日志

配置路径:server.xml/Server/Service/Engine/Host/Valve
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" pattern="%h %l %u %t &quot;%r&quot; %s %b" prefix="localhost_access_log." suffix=".txt"/>
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" pattern="%{begin:yyyy-MM-dd HH:mm:ss.SSS}t %l %u %h &quot;%r&quot; %s %D %F %b" prefix="localhost_access_log." suffix=".txt" buffered="false"/>


日志支持的属性:
fileDateFormat,配置文件名的时间戳,默认yyyy-MM-dd,时间戳变化后文件将会滚动
rotatable,是否支持滚动,默认支持
renameOnRotate,当前文件是否包含时间戳
conditionIf,ServletRequest.getAttribute("attr") != null,时输出日志
conditionUnless,ServletRequest.getAttribute("attr") == null,时输出日志
pattern,日志格式
buffered,是否进行缓存,默认为true,如果false立即输出日志


格式字符:

  • %a - Remote IP address
  • %A - Local IP address
  • %b - Bytes sent, excluding HTTP headers, or '-' if zero
  • %B - Bytes sent, excluding HTTP headers
  • %h - Remote host name (or IP address if enableLookups for the connector is false)
  • %H - Request protocol
  • %l - Remote logical username from identd (always returns '-')
  • %m - Request method (GET, POST, etc.)
  • %p - Local port on which this request was received. See also %{xxx}p below.
  • %q - Query string (prepended with a '?' if it exists)
  • %r - First line of the request (method and request URI)
  • %s - HTTP status code of the response
  • %S - User session ID
  • %t - Date and time, in Common Log Format
  • %u - Remote user that was authenticated (if any), else '-'
  • %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)


数据来源于请求头、响应头、cookie、会话:

  • %{xxx}i write value of incoming header with name xxx
  • %{xxx}o write value of outgoing header with name xxx
  • %{xxx}c write value of cookie with name xxx
  • %{xxx}r write value of ServletRequest attribute with name xxx
  • %{xxx}s write value of HttpSession attribute with name xxx
  • %{xxx}p write local (server) port (xxx==local) or remote (client) port (xxx=remote)
  • %{xxx}t write timestamp at the end of the request formatted using the SimpleDateFormat pattern

特殊说明:

1、生产环境必须设置buffered为true;

2、%{xxx}r,如果xxx属性不存在则输出"-";

3、%D接收到第一字节到发送最后一字节,包含网络影响;

4、%F接受到第一字节到发送第一个字节,不包含网络影响;

发表评论
评论通过审核之后才会显示。