Tomcat中间件打印请求日志

修改conf下的server.xml文件中的Valve配置

主要修改pattern的值

xml 复制代码
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
       prefix="localhost_access_log" suffix=".txt"
       pattern="%h %a %A %l %u %t %I &quot;%r&quot; %s %b %Ts %Dms" />

相关pattern的含义可以参考官方文档,这里贴上方便查询

shell 复制代码
Values for the pattern attribute are made up of literal text strings, combined with pattern identifiers prefixed by the "%" character to cause replacement by the corresponding variable value from the current request and response. The following pattern codes are supported:

%a - Remote IP address. See also %{xxx}a below.
%A - Local IP address
%b - Bytes sent, excluding HTTP headers, or '-' if zero
%B - Bytes sent, excluding HTTP headers
%D - Time taken to process the request in millis. Note: In httpd %D is microseconds. Behaviour will be aligned to httpd in Tomcat 10 onwards.
%F - Time taken to commit the response, in milliseconds
%h - Remote host name (or IP address if enableLookups for the connector is false)
%H - Request protocol
%I - Current request thread name (can compare later with stacktraces)
%l - Remote logical username from identd (always returns '-')
%m - Request method (GET, POST, etc.)
%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)
%s - HTTP status code of the response
%S - User session ID
%t - Date and time, in Common Log Format
%T - Time taken to process the request, in seconds. Note: This value has millisecond resolution whereas in httpd it has second resolution. Behaviour will be align to httpd in Tomcat 10 onwards.
%u - Remote user that was authenticated (if any), else '-' (escaped if required)
%U - Requested URL path
%v - Local server name
%X - Connection status when response is completed:
X = Connection aborted before the response completed.
+ = Connection may be kept alive after the response is sent.
- = Connection will be closed after the response is sent.

最后打印格式如下

在SpringBoot使用嵌入式Tomcat可以在配置文件中按照如下配置

properties 复制代码
server.tomcat.accesslog.enabled=true
server.tomcat.accesslog.directory=D:/home/application/publish/logs
server.tomcat.accesslog.pattern=%h %a %A %l %u %t %I "%r" %s %b %Ts %Dms

directory可以配置相对路径或绝对路径,如果是相对路径,那么最终会放在Tomcat的基础目录当中。

相关推荐
无限的鲜花2 小时前
反射(原创推荐)
java·开发语言
IT二叔2 小时前
Java项目部署-03-teamcity-cicd-docker镜像流水线方式部署
java·ci/cd·持续部署
一路向北he2 小时前
字节钢铁军团--“提供情境,而非控制”
java·开发语言·前端
超级数据查看器3 小时前
超级数据查看器 v10.0 发布
java·大数据·数据库·sqlite·安卓
折哥的程序人生 · 物流技术专研4 小时前
《Java 100 天进阶之路》第50篇:阻塞队列与并发容器(2026版)
java·面试题·java进阶·blockingqueue·并发容器·集合源码·java100天进阶
ai_coder_ai4 小时前
编写自动化脚本,在自己后端服务中使用Open Api进行设备相关操作
java·运维·自动化
硕风和炜5 小时前
【LeetCode: 2492. 两个城市间路径的最小分数 + DFS】
java·算法·leetcode·深度优先·dfs·bfs·并查集
格子软件5 小时前
2026年GEO贴牌代理:分布式多级分账状态机源码深度解构
java·vue.js·分布式·vue·geo
我是一颗柠檬6 小时前
【Java项目技术亮点】加权轮询负载均衡算法
java·算法·负载均衡
灯厂码农6 小时前
C语言动态内存分配完全指南(malloc、calloc、realloc、free)
java·c语言·算法