Tomcat AccessLog 格式化
有的时候我们要使用日志分析工具对日志进行分析,需要对日志进行格式化,比如,要把accessLog格式化成这样的格式
c-ip s-ip x-InstancePort date time-taken x-Protocol cs-method cs-uri sc-status x-ResponseSize bytes x-Referrer x-UserAgent
在tomcat的conf/server.xml加入配置:
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log" suffix=".txt" pattern="%{X-Real-IP}i %A %p %{yyyy-MM-dd HH:mm:ss}t %D %H %m %U %s %B %B %{Referer}i %{User-Agent}i" />
关于accessLog的官方说明,请看 http://tomcat.apache.org/tomcat-8.0-doc/config/valve.html#Access_Logging
常用配置如下:
- %a - Remote IP address --远程IP地址
- %A - Local IP address --本地IP地址
- %b - Bytes sent, excluding HTTP headers, or '-' if zero --发送的字节数(Bytes sent), 不包括HTTP headers的字节,如果为0则展示'-'
- %B - Bytes sent, excluding HTTP headers --发送的字节数(Bytes sent), 不包括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 '-') --远程用户名,始终为'-'(Remote logical username from identd)
- %m - Request method (GET, POST, etc.) --请求的方法(GET, POST等)
- %p - Local port on which this request was received. See also
%{xxx}p
below. --接受请求的本地端口 - %q - Query string (prepended with a '?' if it exists) --查询字符串,如果存在,有一个前置的'?'
- %r - First line of the request (method and request URI) --请求的第一行(包括请求方法和请求的URI)
- %s - HTTP status code of the response --response的HTTP状态码(200,404等)
- %S - User session ID --用户的session ID
- %t - Date and time, in Common Log Format --日期和时间,Common Log Format格式
- %u - Remote user that was authenticated (if any), else '-' --被认证的远程用户, 不存在则展示'-'
- %U - Requested URL path --请求URL路径
- %v - Local server name --本地服务名
- %D - Time taken to process the request, in millis --处理请求的时间,单位为毫秒
- %T - Time taken to process the request, in seconds --处理请求的时间,单位为秒
- %F - Time taken to commit the response, in millis --提交响应的时间,以毫秒为单位
- %I - Current request thread name (can compare later with stacktraces) --当前请求的线程名
Access Log中也支持cookie,请求header,响应headers,Session或者其他在ServletRequest中的对象的信息
%{xxx}i
write value of incoming header with namexxx
%{xxx}o
write value of outgoing header with namexxx
%{xxx}c
write value of cookie with namexxx
%{xxx}r
write value of ServletRequest attribute with namexxx
%{xxx}s
write value of HttpSession attribute with namexxx
%{xxx}p
write local (server) port (xxx==local
) or remote (client) port (xxx=remote
)%{xxx}t
write timestamp at the end of the request formatted using the enhanced SimpleDateFormat patternxxx
如果tomcat前面有nginx做负载均衡,则要修改nginx的配置
Nginx的server主机配置段中添加:
proxy_set_header X-Real-IP $remote_addr; #真实的远端IP地址
然后再tomcat accessLog用%{X-Real-IP}i 获取即可
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?