【发布时间】:2015-09-30 10:16:36
【问题描述】:
我已使用 %X 模式在应用程序日志中成功记录线程上下文值。现在,我想在 tomcat 访问日志 tomcat 中记录相同的值。有什么模式可以记录吗?
【问题讨论】:
我已使用 %X 模式在应用程序日志中成功记录线程上下文值。现在,我想在 tomcat 访问日志 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),然后在应用程序日志中查找它以获取其上下文信息。
【讨论】:
有两件事需要考虑 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)
【讨论】:
我有一个类似的要求,我必须在 MDC 中设置的访问日志中记录相关 ID。我找不到编写 MDC 来访问日志的方法,但在我的情况下,由于我正在将 MDC 中的相关 ID 写入出站请求标头,因此我利用此方法将标头中的值复制到访问日志中。
前
server.tomcat.accesslog.pattern=%A %t %m %U %q %H %s %D %{X-Correlation-Id}o
X-Correlation-Id 是我的标头名称,最后 o 表示从出站请求中读取值。
这对我有帮助。
【讨论】: