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的基础目录当中。

相关推荐
zaizai10073 分钟前
STL深度剖析2
java·c++·算法
周厉18 分钟前
WebSocket 使用场景与实现方法:Java + Vue3 全栈实战
java·websocket·vue3·websocket双向通讯
wdfk_prog21 分钟前
lwip教程 38:从 `lwipopts.h` 到 Port Contract——lwIP 裁剪、构建选择、OS Port 与 Network Port
java·开发语言
吹什么轩23 分钟前
MySQL 内置函数全景:日期 / 字符串 / 数学 / 其他,一次讲透(附索引杀手排查)
java·数据库·面试
略略略咯咯27 分钟前
12333
java
会编程的吕洞宾31 分钟前
Spring AI 语义缓存:重复提问不重复花钱
java·人工智能·后端·spring
海绵宝宝转agent38 分钟前
leetcodehot100个人题解
java·笔记·学习·算法
sunburn-43 分钟前
数据库增删改查:从 SQL 基础到实践要点
java·数据库·mysql
郭小李1 小时前
spring @RequestMapping注解
java·后端·spring
晨曦中的暮雨10 小时前
限流算法——固定窗口、滑动窗口、漏桶、令牌桶
java·开发语言·算法