在tomcat的access中打印出请求的状况能够帮助咱们分析问题,一般比较关注的有访问IP、线程号、访问url、返回状态码、访问时间、持续时间。nginx
在Spring boot中使用了内嵌的tomcat,能够经过server.tomcat.accesslog
配置tomcat 的access日志,这里就以Spring boot 1.5.3为例。tomcat
server.tomcat.accesslog.buffered=true # Buffer output such that it is only flushed periodically.
server.tomcat.accesslog.directory=logs # Directory in which log files are created. Can be relative to the tomcat base dir or absolute.
server.tomcat.accesslog.enabled=false # Enable access log.
server.tomcat.accesslog.file-date-format=.yyyy-MM-dd # Date format to place in log file name.
server.tomcat.accesslog.pattern=common # Format pattern for access logs.
server.tomcat.accesslog.prefix=access_log # Log file name prefix.
server.tomcat.accesslog.rename-on-rotate=false # Defer inclusion of the date stamp in the file name until rotate time.
server.tomcat.accesslog.request-attributes-enabled=false # Set request attributes for IP address, Hostname, protocol and port used for the request.
server.tomcat.accesslog.rotate=true # Enable access log rotation.
server.tomcat.accesslog.suffix=.log # Log file name suffix.
比较经常使用的有(省略了前缀server.tomcat.accesslog.
):服务器
pattern的配置:cookie
enableLookups
for the connector is false),远程主机名称(若是resolveHosts为false则展现IP)Access log 也支持将cookie、header、session或者其余在ServletRequest中的对象信息打印到日志中,其配置遵循Apache配置的格式({xxx}指值的名称):session
%{xxx}i
for incoming headers,request header信息%{xxx}o
for outgoing response headers,response header信息%{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)Access log内置了两个日志格式模板,能够直接指定pattern为模板名称,如:ide
server.tomcat.accesslog.pattern=common
%h %l %u %t "%r" %s %b
,依次为:远程主机名称,远程用户名,被认证的远程用户,日期和时间,请求的第一行,response code,发送的字节数%h %l %u %t "%r" %s %b "%{Referer}i" "%{User-Agent}i"
,依次为:远程主机名称,远程用户名,被认证的远程用户,日期和时间,请求的第一行,response code,发送的字节数,request header的Referer信息,request header的User-Agent信息。除了内置的模板,咱们经常使用的配置有:url
%t %a "%r" %s (%D ms)
,日期和时间,请求来自的IP(不必定是原始IP),请求第一行,response code,响应时间(毫秒),样例:[21/Mar/2017:00:06:40 +0800] 127.0.0.1 POST /bgc/syncJudgeResult HTTP/1.0 200 63
,这里请求来自IP就是通过本机的nginx转发的。%t [%I] %{X-Forwarded-For}i %a %r %s (%D ms)
,日期和时间,线程名,原始IP,请求来自的IP(不必定是原始IP),请求第一行,response code,响应时间(毫秒),样例:[21/Apr/2017:00:24:40 +0800][http-nio-7001-exec-4] 10.125.15.1 127.0.0.1 POST /bgc/syncJudgeResult HTTP/1.0 200 5
,这里的第一个IP是Nginx配置了X-Forwarded-For
记录了原始IP。这里简要介绍下上面用到的HTTP请求头X-Forwarded-For
,它是一个 HTTP 扩展头部,用来表示 HTTP 请求端真实 IP,其格式为:X-Forwarded-For: client, proxy1, proxy2
,其中的值经过一个逗号+空格
把多个IP地址区分开,最左边(client)是最原始客户端的IP地址,代理服务器每成功收到一个请求,就把请求来源IP地址添加到右边。spa