昨天遇到的一个问题是,我有一个接口,这个接口会接收一个100M的视频,然后我想知道,这个接口的处理时间。以前spring项目是扔到tomcat里面去部署的,我们可以直接去tomcat放access日志的地方查看,现在springboot项目,tomcat是内嵌到框架里面的,这时候我们就需要在项目的配置文件application.yml里面来配置tomcat的日志了。以下是基本的配置
server:
tomcat:
accesslog:
enabled: true
directory: D:/logs/tw-weiting
rotate: true
pattern: '%t %a %A %m %U%q %s %D %I %B'
buffered: false
enabled:是否打印accesslog,默认是false
directory:放access日志的路径
rotate:指定是否启用日志轮转。默认为true。这个参数决定是否需要切换切换日志文件,如果被设置为false,则日志文件不会切换,即所有文件打到同一个日志文件中,并且file-date-format参数也会被忽略
pattern:定义access日志的格式,可以自定义,也可以用Access log内置了两个日志格式模板,
- 【pattern:common】 -
%h %l %u %t "%r" %s %b
,依次为:远程主机名称,远程用户名,被认证的远程用户,日期和时间,请求的第一行,response code,发送的字节数 - 【pattern:combined 】-
%h %l %u %t "%r" %s %b "%{Referer}i" "%{User-Agent}i"
,依次为:远程主机名称,远程用户名,被认证的远程用户,日期和时间,请求的第一行,response code,发送的字节数,request header的Referer信息,request header的User-Agent信息。
除了内置的模板,我们常用的配置有:
%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地址添加到右边。
pattern的配置:
- %a - Remote IP address,远程ip地址,注意不一定是原始ip地址,中间可能经过nginx等的转发
- %A - Local IP address,本地ip
- %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 if
enableLookups
for the connector is false),远程主机名称(如果resolveHosts为false则展示IP) - %H - Request protocol,请求协议
- %l - Remote logical username from identd (always returns '-')
- %m - Request method,请求方法(GET,POST)
- %p - Local port,接受请求的本地端口
- %q - Query string (prepended with a '?' if it exists, otherwise an empty string
- %r - First line of the request,HTTP请求的第一行(包括请求方法,请求的URI)
- %s - HTTP status code of the response,HTTP的响应代码,如:200,404
- %S - User session ID
- %t - Date and time, in Common Log Format format,日期和时间,Common Log 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,处理请求的时间,单位秒
- %I - current Request thread name (can compare later with stacktraces),当前请求的线程名,可以和打印的log对比查找问题
Access log 也支持将cookie、header、session或者其他在ServletRequest中的对象信息打印到日志中,其配置遵循Apache配置的格式({xxx}指值的名称):
%{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)
其他配置参考:
server.tomcat.accesslog.buffered=true # 缓冲输出,使其只定期刷新。
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 # 要放在日志文件名中的日期格式。rotate为false的时候,这个也就不生效了。
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.
tomcat access默认配置
127.0.0.1 | - | - | [07/Oct/2016:22:31:56 +0800] | “GET /dubbo/ HTTP/1.1” | 404 | 963 |
远程IP | logical username | remote user | 时间和日期 | http请求的第一行 | 状态码 | 除去http头的发送大小 |
参考文件:https://www.cnblogs.com/chrischennx/p/6746214.html