Debian Tomcat日志格式说明
Debian系统上的Tomcat默认使用java.util.logging
框架记录日志,主要日志文件位于$CATALINA_BASE/logs
目录($CATALINA_BASE
为Tomcat安装基目录)。默认日志格式为文本格式,包含以下字段:
YYYY-MM-DD HH:MM:SS,sss
(年-月-日 时:分:秒,毫秒),例如2023-04-01 12:34:56,789
;INFO
、WARNING
、SEVERE
等,标识日志的严重程度;main
(主线程)或其他线程名;org.apache.catalina.startup.Bootstrap
;Tomcat started
(Tomcat启动完成)。示例:
2023-04-01 12:34:56,789 [main] INFO Tomcat started
Tomcat的访问日志(记录HTTP请求信息)由AccessLogValve
组件控制,默认配置位于$CATALINA_BASE/conf/server.xml
文件中。默认使用common
模式,格式为:
%h
:远程主机IP地址;%l
:远程用户名(始终为-
);%u
:已验证的远程用户(未验证则为-
);%t
:访问时间(Common Log Format格式,如[10/Oct/2025:14:30:00 +0000]
);"%r"
:HTTP请求的第一行(包括方法、URL、协议,如GET /index.html HTTP/1.1
);%s
:HTTP状态码(如200
、404
);%b
:发送的字节数(不包含HTTP头,0则显示-
)。示例(common
模式):
192.168.1.100 - - [10/Oct/2025:14:30:00 +0000] "GET /index.html HTTP/1.1" 200 1024
若需更详细的访问日志,可将pattern
改为combined
模式,增加%{Referer}i
(来源页面)、%{User-Agent}i
(用户代理)等字段: 示例(combined
模式):
192.168.1.100 - - [10/Oct/2025:14:30:00 +0000] "GET /index.html HTTP/1.1" 200 1024 "http://example.com/ref" "Mozilla/5.0 (Windows NT 10.0; Win64; x64)"
若需调整默认日志格式,可通过修改$CATALINA_BASE/conf/logging.properties
文件实现:
java.util.logging.ConsoleHandler.formatter
配置项,将其值从java.util.logging.SimpleFormatter
改为org.apache.juli.OneLineFormatter
(支持单行输出),然后添加java.util.logging.SimpleFormatter.format
配置项定义格式字符串;%1$tY-%1$tm-%1$td
:年-月-日;%1$tH:%1$tM:%1$tS
:时:分:秒;%4$s
:日志级别;%2$s
:线程名;%5$s
:类名;%3$s
:日志消息;%n
:换行符。示例(自定义控制台日志格式):
java.util.logging.ConsoleHandler.formatter = org.apache.juli.OneLineFormatter java.util.logging.SimpleFormatter.format = %1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS %4$s [%2$s: %5$s] %3$s%n
效果:
2023-04-01 12:34:56 INFO [main: org.apache.catalina.startup.Bootstrap] Tomcat started
sudo systemctl restart tomcat
);