Tomcat access log配置
在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.
比较常用的有(省略了前缀server.tomcat.accesslog.
):
- enabled,取值true、false,需要accesslog时设置为true
- directory,指定access文件的路径
- pattern,定义日志的格式,后续详述
- rotate,指定是否启用日志轮转。默认为true。这个参数决定是否需要切换切换日志文件,如果被设置为false,则日志文件不会切换,即所有文件打到同一个日志文件中,并且file-date-format参数也会被忽略
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)
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信息。
除了内置的模板,我们常用的配置有:
%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地址添加到右边。
参考
Tomcat Access Log配置
AccessLogValve
X-Forwarded-For
Tomcat access log配置的更多相关文章
- Tomcat access log配置(二)
前次讨论了spring boot 中添加Tomcat access log 是轻松愉快,配置文件中添加server.tomcat.accesslog即可,那么如果是外置的Tomcat容器又该如何配置呢 ...
- Tomcat Access Log 的格式
名称 含义 %a Remote IP address %A Local IP address %b Bytes sent, excluding HTTP headers, or ‘-‘ if zero ...
- tomcat access log 参数
%a - 客户端IP地址 %A - 本机IP地址 %b - 发送字节数,不含HTTP头 如果为空是 '-' %B - 同上 %h - 客户端机器名 (如果connector的enableLookup ...
- Tomcat访问(access)日志配置
在tomcat的access中打印出请求的情况可以帮助我们分析问题,通常比较关注的有访问IP.线程号.访问url.返回状态码.访问时间.持续时间. 最近在跟一个图片请求超时的问题,需要在项目的acce ...
- springboot中配置tomcat的access log
在tomcat的access中打印出请求的情况可以帮助我们分析问题,通常比较关注的有访问IP.线程号.访问url.返回状态码.访问时间.持续时间. 在Spring boot中使用了内嵌的tomcat, ...
- 通过Nginx,Tomcat访问日志(access log)记录请求耗时
一.Nginx通过$upstream_response_time $request_time统计请求和后台服务响应时间 nginx.conf使用配置方式: log_format main '$remo ...
- tomcat 和 jboss access log 日志输出详解
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt179 工作中nginx+jboss/tomcat反向代理集成,想打开后端jb ...
- Servlet、Tomcat访问(access)日志配置、记录Post请求参数
一.运行环境: Maven:3.5.2(点击下载) ,下载页 Tomcat:8.5.29(点击下载) ,下载页 JDK:jdk1.7.0_80(点击下载) ,下载页 MavenDependency: ...
- resin access.log format配置详解
The access log formatting variables follow the Apache variables: %b result content length %D tim ...
随机推荐
- [HDU1232] 畅通工程 (并查集 or 连通分量)
Input 测试输入包含若干测试用例.每个测试用例的第1行给出两个正整数,分别是城镇数目N ( < 1000 )和道路数目M:随后的M行对应M条道路,每行给出一对正整数,分别是该条道路直接连通的 ...
- 【Spring】使用Spring和AMQP发送接收消息(上)
讲AMQP之前,先讲下传统的JMS的消息模型,JMS中主要有三个参与者:消息的生产者.消费者.传递消息的通道(队列或者主题),两种消息模型如下:通道是队列: 通道是队列: 通道是主题: 在JMS中,虽 ...
- 关于nodeJS多线程的支持,目前看来无法实现,讲解v8的一些东西
关于这个,我这几天一直在研究,国内关于v8的资料很少,stackoverflow上也不多. 说起来我得说声抱歉,虽然并没有承诺什么.这个功能大概是无法实现.下面我来解释一下为什么. 首先我们要了解一下 ...
- 1624: [Usaco2008 Open] Clear And Present Danger 寻宝之路
1624: [Usaco2008 Open] Clear And Present Danger 寻宝之路 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 3 ...
- shiro学习笔记_0100_shiro简介
前言:第一次知道shiro是2016年夏天,做项目时候我要写springmvc的拦截器,申哥看到后,说这个不安全,就给我捣鼓了shiro,我就看了下,从此认识了shiro.此笔记是根据网上的视频教程记 ...
- 有个程序猿要去当CEO了:(一)事情始末
事情大概是这样的: 去年年底,我从原公司离职,原因大概是公司绩效不好,呆着也没意思. 后来听说,年终结算遣散了所有人. 今年年初的时候,前老板又找上我,说希望能和我再合作. 起先是想分我一部分干股,让 ...
- 初识 Javascript.01 -- Javascript基础|输出方式、变量、变量命名规范、数据类型、
Javascript基础 1 聊聊Javascript 1.1 Javascript的历史来源 94年网景公司 研发出世界上第一款浏览器. 95年 sun公司 java语言诞生 网景公司和su ...
- 网关、DNS、路由器区别
1.首先得区分一下网关和路由器的区别: 网关是一个IP地址.是一个网络连接到另一个网络的"关口". 路由器是一个物理设备.一般局域网的网关就是路由器的IP地址. 2. 网关.DNS ...
- android 热更新 tinker 从零开始到使用
这几天项目完结了,闲来无事,想起来了以前研究的热更新,那个开源的只有nvwa.recoo,等,不是很好用,最近听说tinker开源一段时间了,用的人还挺多,决定研究一下! 首先进入了官方文档 http ...
- 老司机实战Windows Server Docker:5 Windows Server Dockerfile葵花宝典
前面两篇(简单运维1.简单运维2)介绍了一些Windows Server Docker相关的基本运维知识.今天这一篇,Windows Server Dockerfile葵花宝典,涵盖了许多典型场景的W ...