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

时间:2015-09-30 10:16:37

标签: java tomcat logging

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

3 个答案:

答案 0 :(得分:0)

有两件事需要考虑NDC和MDC,您可以在tomcat日志中同时尝试,请查看此链接以获取详细的转换模式(https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PatternLayout.html

尝试使用%x和%X,请在此处查看如何更改tomcat日志记录属性(http://www.laliluna.com/articles/posts/log4j-tutorial.html

答案 1 :(得分:0)

你不能在访问日志中记录线程上下文,我担心。

以下是可用选项:

%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),然后在应用程序日志中查找,以获取其上下文信息。

答案 2 :(得分:0)

我有一个类似的要求,即必须在MDC中设置的访问日志中记录相关性ID。我找不到写MDC来访问日志的方法,但是在我的情况下,因为我是将MDC的correlationIdId写入出站请求标头,所以我利用它将标头中的值复制到访问日志中。

Ex

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

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

这帮助了我。