Java 如何在tomcat访问日志中记录线程上下文值

Java 如何在tomcat访问日志中记录线程上下文值,java,tomcat,logging,Java,Tomcat,Logging,我已成功使用%X模式在应用程序日志中记录线程上下文值。现在,我希望在tomcat访问日志tomcat中记录相同的值。是否有任何日志模式?< p>有2件事需要考虑NDC和MDC,您可以在Tomcat日志中同时尝试这两个链接,请检查此链接以查看Cracrter的详细模式() 尝试同时使用%x和%x,在这里检查如何更改tomcat日志记录属性()恐怕您不能在访问日志中记录线程上下文 以下是可用的选项: %a - Remote IP address %A - Local IP address %b -

我已成功使用%X模式在应用程序日志中记录线程上下文值。现在,我希望在tomcat访问日志tomcat中记录相同的值。是否有任何日志模式?

< p>有2件事需要考虑NDC和MDC,您可以在Tomcat日志中同时尝试这两个链接,请检查此链接以查看Cracrter的详细模式()


尝试同时使用%x和%x,在这里检查如何更改tomcat日志记录属性()

恐怕您不能在访问日志中记录线程上下文

以下是可用的选项:

%a - Remote IP address
%A - Local IP address
%b - Bytes sent, excluding HTTP headers, or '-' if zero
%B - Bytes sent, excluding HTTP headers
%h - Remote host name (or IP address if enableLookups for the connector is false)
%H - Request protocol
%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
%u - Remote user that was authenticated (if any), else '-'
%U - Requested URL path
%v - Local server name
%D - Time taken to process the request, in millis
%T - Time taken to process the request, in seconds
%F - Time taken to commit the response, in millis
%I - Current request thread name (can compare later with stacktraces)

据我所知,您唯一能做的就是记录当前线程名(
%I
),然后在应用程序日志中查找它,以获取其上下文信息。

我有一个类似的要求,我必须在MDC中设置的访问日志中记录相关Id。我找不到将MDC写入访问日志的方法,但在我的例子中,因为我正在将correlationId从MDC写入出站请求头,所以我利用它将值从头复制到访问日志中

X-Correlation-Id是我的头名称,最后o表示从出站请求读取值


这对我很有帮助。

您好,谢谢您的快速回复。但我必须在TOMCAT访问日志中记录线程上下文值,而不是在应用程序日志中(我在应用程序日志中获得该值),而是在TOMCAT访问日志中?我在TOMCAT根目录的server.xml文件中找到了模式布局,但在类似(%X)的文件中找不到线程上下文属性在服务器模式中,感谢您提供的宝贵信息。它正在提供%s来记录会话信息,但我需要记录线程上下文值
server.tomcat.accesslog.pattern=%A %t %m %U %q %H %s %D %{X-Correlation-Id}o