accesslog在springboot中开启,表达式介绍,配置详情,使用undertow容器时%D接口耗时不打印问题解决。
简介:
1.为了接入接口访问监控,可以直接使用容器(undertow,tomcat)自带的配置实现打印accesslog。
配置文件配置(springboot项目使用的容器默认是tomcat那就选2,如果是undertow就选1):
1.undertow容器:
server:
port: 8081
undertow:
accesslog:
enabled: true # true 开启accesslog,false:不开启
dir: /midware/logs/dataapi-service # 日志打印文件地址
prefix: dataapi_access. # 日志打印文件名称
file-date-format: .yyyy-MM-dd
pattern: %t [%I] %{X-Forwarded-For}i %a %r %q %s (%D ms) # 日志输出表达式,默认值是common
rotate: true # 为true时,上面的日期格式才有意义,否则就是写在一个文件里了
2.tomcat容器:
server:
port: 8081
tomcat:
accesslog:
enabled: true # true 开启accesslog,false:不开启
dir: /midware/logs/dataapi-service # 日志打印文件地址
prefix: dataapi_access. # 日志打印文件名称
file-date-format: .yyyy-MM-dd
pattern: %t [%I] %{X-Forwarded-For}i %a %r %q %s (%D ms) # 日志输出表达式,默认值是common
rotate: true # 为true时,上面的日期格式才有意义,否则就是写在一个文件里了
配置文件配置日志输出表达式介绍:(server.tomcat.accesslog.pattern 解释:)
- %a - Remote IP address 远程IP地址
- %A - Local IP address 本地IP地址
- %b - Bytes sent, excluding HTTP headers, or ‘-’ if no bytes were sent 返回给客户端的数据字节数,- 表示没有数据
- %B - Bytes sent, excluding HTTP headers 与上面一样,貌似是官方的bug,应该是一个包含 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
- %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 同上,单位秒
- %I - current Request thread name (can compare later with stacktraces) 执行当前请求的线程名称,输出可以统计多少线程在工作,当然也可以通过其他方式,jstack
undertow容器时%D接口耗时不打印问题解决
记录请求所费时间, 所以要在config配置内中加入下面代码,开启undertow计时:
@Bean
public UndertowServletWebServerFactory undertowServletWebServerFactory() {
UndertowServletWebServerFactory factory = new UndertowServletWebServerFactory();
factory.addBuilderCustomizers(new UndertowBuilderCustomizer() {
@Override
public void customize(Undertow.Builder builder) {
builder.setServerOption(UndertowOptions.RECORD_REQUEST_START_TIME, true);
}
});
return factory;
}