【问题标题】:How to log thread context value in tomcat access logs如何在tomcat访问日志中记录线程上下文值
【发布时间】:2015-09-30 10:16:36
【问题描述】:

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

【问题讨论】:

    标签: java tomcat logging


    【解决方案1】:

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

    这里有可用的选项:

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

    【讨论】:

    • 您好,感谢您提供宝贵的信息。它提供 %s 来记录会话信息,但我需要记录线程上下文值
    【解决方案2】:

    有两件事需要考虑 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)

    【讨论】:

    • 嗨,感谢您的快速回复。但我必须在 TOMCAT ACCESS 日志中记录线程上下文值,而不是在应用程序日志中(我在应用程序日志中获取该值),而是在 tomcat 访问日志中?
    • 我在 tomcat 根目录的 server.xml 文件中找到了模式布局,但我在服务器模式中的 like(%X) 中找不到线程上下文属性
    【解决方案3】:

    我有一个类似的要求,我必须在 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 表示从出站请求中读取值。

    这对我有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-27
      • 1970-01-01
      • 2012-02-23
      • 1970-01-01
      • 2019-06-05
      • 2019-05-11
      相关资源
      最近更新 更多