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

相关推荐
青云交4 小时前
Java 大视界 -- 基于 Java 的大数据机器学习模型在图像识别中的迁移学习与模型优化
java·大数据·迁移学习·图像识别·模型优化·deeplearning4j·机器学习模型
2501_909800814 小时前
Java 集合框架之 Set 接口
java·set接口
断剑zou天涯4 小时前
【算法笔记】暴力递归尝试
java·笔记·算法
Nobody_Cares5 小时前
JWT令牌
java
沐浴露z5 小时前
Kafka入门:基础架构讲解,安装与使用
java·分布式·kafka
神秘的土鸡5 小时前
从数据仓库到数据中台再到数据飞轮:我的数据技术成长之路
java·服务器·aigc·数据库架构·1024程序员节
vir025 小时前
P1928 外星密码(dfs)
java·数据结构·算法·深度优先·1024程序员节
摇滚侠6 小时前
全面掌握PostgreSQL关系型数据库,备份和恢复,笔记46和笔记47
java·数据库·笔记·postgresql·1024程序员节
eguid_17 小时前
【开源项目分享】JNSM1.2.0,支持批量管理的jar包安装成Windows服务可视化工具,基于Java实现的支持批量管理已经安装服务的可视化工具
java·开源·jar·1024程序员节·windows服务·jar包安装成服务·exe安装成服务
杯莫停丶7 小时前
设计模式之:享元模式
java·设计模式·享元模式