解读Ubuntu Tomcat日志中的访问记录可按以下步骤进行:
定位日志文件
/var/log/tomcat/
或/opt/tomcat/logs/
,可通过Tomcat配置文件server.xml
中AccessLogValve
标签的directory
和prefix
属性确认。localhost_access_log.{日期}.txt
(需在conf/logging.properties
中启用)。查看日志内容
cat
:查看小文件全部内容;less
/more
:分页查看大文件。tail -f
:实时查看新增日志(如tail -f localhost_access_log.2025-08-29.txt
)。grep
:搜索特定关键字(如grep "404" localhost_access_log.*.txt
)。awk
统计访问次数、sort/uniq
去重排序。解析日志字段
combined
格式为例):
%h
:客户端IP地址。%t
:访问时间(格式如[10/Oct/2025:14:55:36 +0000]
)。%r
:请求行(如GET /index.html HTTP/1.1
)。%s
:HTTP状态码(200为成功,404为未找到)。%b
:响应字节数(不包括HTTP头)。%{Referer}i
:来源页面URL。%{User-Agent}i
:客户端浏览器信息。分析关键指标
%D
/%T
)定位慢请求。示例命令:
grep " 404 " localhost_access_log.$(date +%Y-%m-%d).txt | wc -l
awk '{print $1}' localhost_access_log.*.txt | sort | uniq -c | sort -nr | head -5
通过以上方法可快速定位访问异常、优化性能并保障安全。