tomcat日志配置

一.作用

参考地址:http://xiaobaoqiu.github.io/blog/2014/12/30/tomcat-access-logpei-zhi/

参考地址: http://tomcat.apache.org/tomcat-5.5-doc/config/valve.html

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

二.配置详情:在Spring boot中使用了内嵌的tomcat,可以通过server.tomcat.accesslog配置tomcat 的access日志,这里就以Spring boot 1.5.3为例。

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.

  

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

  • enabled,取值true、false,需要accesslog时设置为true

  • directory,指定access文件的路径

  • rotate,指定是否启用日志轮转。默认为true。这个参数决定是否需要切换切换日志文件,如果被设置为false,则日志文件不会切换,即所有文件打到同一个日志文件中,并且file-date-format参数也会被忽略

  • pattern,定义日志的格式,

    pattern的配置:

    %a - 远程IP地址

    %A - 本地IP地址

    %b - 发送的字节数(Bytes sent), 不包括HTTP headers的字节,如果为0则展示'-'

    %B - 发送的字节数(Bytes sent), 不包括HTTP headers的字节

    %h - 远程主机名称(如果resolveHosts为false则展示IP)

    %H - 请求协议

    %l - 远程用户名,始终为'-'(Remote logical username from identd)

    %m - 请求的方法(GET, POST等)%p - 接受请求的本地端口

    %q - 查询字符串,如果存在,有一个前置的'?'

    %r - 请求的第一行(包括请求方法和请求的URI)

    %s - response的HTTP状态码(200,404等)%S - 用户的session ID

    %t - 日期和时间,Common Log Format格式

    %u - 被认证的远程用户, 不存在则展示'-'

    %U - 请求URL路径%v - 本地服务名

    %D - 处理请求的时间,单位为毫秒

    %T - 处理请求的时间,单位为秒%I - 当前请求的线程名(can compare later with stacktraces)

2.3 Access log内置了两个日志格式模板,可以直接指定pattern为模板名称,如:server.tomcat.accesslog.pattern=common`:

  • common - %h %l %u %t "%r" %s %b,依次为:远程主机名称,远程用户名,被认证的远程用户,日期和时间,请求的第一行,response code,发送的字节数

  • combined - %h %l %u %t "%r" %s %b "%{Referer}i" "%{User-Agent}i",依次为:远程主机名称,远程用户名,被认证的远程用户,日期和时间,请求的第一行,response code,发送的字节数,request header的Referer信息,request header的User-Agent信息。

2.4. Access Log中也支持cookie,请求header,响应headers,Session或者其他在ServletRequest中的对象的信息。格式遵循apache语法:

%{xxx}i 请求headers的信息

%{xxx}o 响应headers的信息

%{xxx}c 请求cookie的信息

%{xxx}r xxx是ServletRequest的一个属性

%{xxx}s xxx是HttpSession的一个属性

说明:combined模式的pattern可以增加Referer和User-Agent headers的参数形式,每个参数用双引号包起来,引号中的内容还是上面列举的参数。比如"%{User-Agent}i"使其为”%{User-Agent}i“,即请求的User-Agent(客户端,浏览器)。

原文地址:https://www.cnblogs.com/KdeS/p/11975642.html