Springboot项目中配置tomcta监控日志

背景:在tomcat的access中打印出请求的情况可以帮助我们分析问题,通常比较关注的有访问IP、线程号、访问url、返回状态码、访问时间、持续时间。

在Spring boot中使用了内嵌的tomcat,可以通过server.tomcat.accesslog配置tomcat 的access日志

tomcat的accesslog日志 

server.tomcat.accesslog.buffered=true
server.tomcat.accesslog.enabled=true
server.tomcat.accesslog.file-date-format=.yyyy-MM-dd
server.tomcat.accesslog.pattern=%a %h %m %l %u %t %r %s %S %b %D %T %I
server.tomcat.accesslog.prefix=access_log
server.tomcat.accesslog.rename-on-rotate=false
server.tomcat.accesslog.request-attributes-enabled=false
server.tomcat.accesslog.rotate=true
server.tomcat.accesslog.suffix=.log
server.tomcat.accesslog.directory=tomcat-access-logs

比较常用的有(省略了前缀server.tomcat.accesslog.):

  • enabled,取值true、false,需要accesslog时设置为true
  • directory,指定access文件的路径
  • pattern,定义日志的格式,后续详述
  • rotate,指定是否启用日志轮转。默认为true。这个参数决定是否需要切换切换日志文件,如果被设置为false,则日志文件不会切换,即所有文件打到同一个日志文件中,并且file-date-format参数也会被忽略

tomcat日志路径

server.tomcat.basedir=D:/logs(例)

主要字段:
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) 执行当前请求的线程名称,输出可以统计多少线程在工作。

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/hrbeuwhw/article/details/81704549

https://blog.csdn.net/u013732378/article/details/100151205

 

 

上一篇:springboot中的access-log


下一篇:日志文件分析溯源(SQL注入IP地址2)