什么是 NDC 日志以及我们如何在我们的应用程序中使用它以及它的意义是什么

发布于 2024-09-05 05:24:47 字数 50 浏览 1 评论 0原文

什么是 NDC 日志以及我们如何在我们的应用程序中使用它以及它的意义是什么......

what is the NDC logs and how we can use it in our application and what is the significance of that...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梅窗月明清似水 2024-09-12 05:24:47

嵌套诊断上下文特定于线程。

常见用途是记录每个会话的信息(如果会话使用一个线程),因此您可以记录原始客户端、用户名等以及其他横切属性,而无需:

  1. 通过以下方式传递这些属性应用程序的各层
  2. 将它们显式记录在每个日志语句中。如果适当配置了 PatternLayout,Log4j 将输出 NDC。

另请参阅 Log4j 的映射诊断上下文。

Nested Diagnostic Contexts are particular to a thread.

Common uses are for recording info per-session (if one thread is used for a session), so you can log the originating client, username etc. and other cross-cutting attributes without:

  1. passing these attributes through the layers of your application
  2. explicitly logging them in each log statement. Log4j will output the NDC if PatternLayout is suitably configured.

See also Log4j's Mapped Diagnostic Contexts.

栖竹 2024-09-12 05:24:47

NDC 代表“嵌套诊断上下文”,它是 log4j 的一个功能。 log4j 最常见的用法只是记录内容,而不指示它属于哪个客户端请求,其结果是,当您的应用程序在生产环境中运行并发请求时,所有请求的所有日志消息都混杂在一起日志文件并告诉谁做了什么是不可能的。 NDC 允许您将日志消息标记为属于特定客户端,以便您可以区分谁在做什么,而无需为每个客户端设置单独的记录器。

NDC stands for "Nested Diagnostic Contexts", it's a feature of log4j. The most common usage of log4j is just to log stuff without any indication of what client request it was part of, with the result that, when your application runs in production with concurrent requests, all the log messages for all the requests are jumbled together in the log file and telling who did what is impossible. NDC allows you to mark log messages as belonging to particular clients so that you can distinguish who is doing what, without having separate loggers for each client.

权谋诡计 2024-09-12 05:24:47

Logger 通常在代码中静态定义,这使得 log 有时难以理解。

NDC 允许动态地 push 一个参数,该参数将显示在线程发出的每个后续日志行中,直到它被popped。

如果您想要如下日志,则很有用:(

[request=x] a
[request=y] a
[request=x] b
[request=x] c
[request=y] b
[request=x] d
[request=y] c
[request=y] d

免责声明:我不记得确切的格式)

仅使用 a,b,c,d 很难理解哪个线程执行什么操作。如果您动态地pushpop 请求ID,那么就更容易遵循。也可用于其他类型的上下文信息。

Logger are usually statically defined in the code, which make log sometimes hard to understand.

The NDC allows the dynamically push a parameter that will be displayed in every subsequent log line issued by the thread, until it is popped.

Useful if you want a log like:

[request=x] a
[request=y] a
[request=x] b
[request=x] c
[request=y] b
[request=x] d
[request=y] c
[request=y] d

(Disclaimer: I don't remember the exact formatting)

With just a,b,c,d it's hard to understand which thread does what. If you push and pop the request id dynamically, then it's easier to follow. Can also be used for other kinds of contextual information.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文