Springboot项目开启Tomcat的AccessLog。设置AccessLog样式,并打印指定的Header内容
SpringBoot项目开启Tomcat的AccessLog
server.tomcat.basedir=my-tomcat
server.tomcat.accesslog.enabled=true
指定打印Header
server.tomcat.accesslog.pattern=%t %a "%r" %s (%D ms) %{xxx}i
%{xxx}i 就是打印指定Header的写法,其中xxx
就是header的name
SpringBoot配置文件参考示例
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.
常用的样式参数
- %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)
配置参考:https://blog.csdn.net/u013732378/article/details/100151205