简体   繁体   English

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

[英]How to log thread context value in tomcat access logs

I have logging thread context value in application logs successfully with %X pattern.我使用 %X 模式在应用程序日志中成功记录了线程上下文值。 Now, I want log same value in tomcat access log tomcat.现在,我想在 tomcat 访问日志 tomcat 中记录相同的值。 Is there any pattern to log?是否有任何模式可以记录?

You can't log thread context in access log, I'm afraid.恐怕您无法在访问日志中记录线程上下文。

Here's available options:以下是可用选项:

%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)

As far as I know, the only thing you can do is to log the current thread name ( %I ) and then look it up in the application logs in order get its context information.据我所知,您唯一能做的就是记录当前线程名称( %I ),然后在应用程序日志中查找它以获取其上下文信息。

There are 2 things to be consider NDC and MDC, you can try both in tomcat logs, please check this link for detailed Pattern of conversion chrachters ( https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PatternLayout.html )有两件事需要考虑 NDC 和 MDC,你可以在 tomcat 日志中尝试,请查看此链接以获取详细的转换字符模式( https://logging.apache.org/log4j/1.2/apidocs/org/apache/ log4j/PatternLayout.html )

try with %x and %X both, check here how you can change tomcat logging properties ( http://www.laliluna.com/articles/posts/log4j-tutorial.html )尝试同时使用 %x 和 %X,在此处查看如何更改 tomcat 日志记录属性( http://www.laliluna.com/articles/posts/log4j-tutorial.html

I had a similar requirement where I have to log correlation Id in my access logs which was set in MDC.我有一个类似的要求,我必须在 MDC 中设置的访问日志中记录相关性 ID。 I could not find a way to write MDC to access logs, but in my cases since I was writing the correlationId from MDC to outbound request headers, I leverage this to copy the value from headers into access logs.我找不到将 MDC 写入访问日志的方法,但在我的情况下,由于我将来自 MDC 的correlationId 写入出站请求标头,因此我利用它来将标头中的值复制到访问日志中。

Ex前任

server.tomcat.accesslog.pattern=%A %t %m %U %q %H %s %D %{X-Correlation-Id}o

X-Correlation-Id was my header name and o in the end says to read the value from outbound request. X-Correlation-Id 是我的标头名称,最后 o 表示从出站请求中读取值。

This helped me out.这帮助了我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM