Log4j SyslogAppender是否支持MDC和NDC

发布于 2024-07-23 12:27:03 字数 118 浏览 5 评论 0原文

真的很简单,Log4j SyslogAppender 是否支持 MDC 和 NDC,因为输出是结构化数据,即使用协议的结构化数据功能?

此外,对于可以放入 MDC 并成功附加到日志的内容是否有任何限制?

Simple really, does Log4j SyslogAppender support MDC and NDC in the sense that the output is structured data i.e. uses the structured data features of the protocol?

Further, are there any limits on what can be put in the MDC and successfully appended to the log?

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

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

发布评论

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

评论(2

笑,眼淚并存 2024-07-30 12:27:03

MDC 和 NDC 都是 Syslog 协议的一部分。 因此,log4j 不(也不可能)支持 Syslog 协议的结构化数据内的 MDC/NDC。 但是,通过设置 ConversionPattern 参数以包含 MDC 信息,您可以在 syslog 消息的“消息”部分中添加 MDC 或 NDC 数据。

以下是带有键“ki”的 MDC 条目的示例:

log4j.rootLogger=INFO, SYSLOG
log4j.appender.SYSLOG=org.apache.log4j.net.SyslogAppender
log4j.appender.SYSLOG.SyslogHost=a.host.name

# Facility must be one of the case-insensitive strings:
# KERN, USER, MAIL, DAEMON, AUTH, SYSLOG, LPR, NEWS, UUCP, CRON,
# AUTHPRIV, FTP, LOCAL0, LOCAL1, LOCAL2, LOCAL3, LOCAL4, LOCAL5, LOCAL6,
# LOCAL7 
log4j.appender.SYSLOG.facility=KERN

log4j.appender.SYSLOG.layout=org.apache.log4j.PatternLayout
log4j.appender.SYSLOG.layout.ConversionPattern=%r %p %c %X{ki} - %m\n

对于 NDC,您可以将“%X{ki}”替换为“%x”(注意使用小写字母)。

至于问题的第二部分,您可以在 MDC 或 NDC 中放置的值没有限制。

MDC nor NDC are part of the Syslog protocol. Thus, log4j does not (nor can it) support MDC/NDC within the structured data of the Syslog protocol. However, nothing prevents you from adding MDC or NDC data in the "message" part of the syslog message by setting the ConversionPattern parameter to include MDC informatation.

Here is an example for an MDC entry with the key "ki" :

log4j.rootLogger=INFO, SYSLOG
log4j.appender.SYSLOG=org.apache.log4j.net.SyslogAppender
log4j.appender.SYSLOG.SyslogHost=a.host.name

# Facility must be one of the case-insensitive strings:
# KERN, USER, MAIL, DAEMON, AUTH, SYSLOG, LPR, NEWS, UUCP, CRON,
# AUTHPRIV, FTP, LOCAL0, LOCAL1, LOCAL2, LOCAL3, LOCAL4, LOCAL5, LOCAL6,
# LOCAL7 
log4j.appender.SYSLOG.facility=KERN

log4j.appender.SYSLOG.layout=org.apache.log4j.PatternLayout
log4j.appender.SYSLOG.layout.ConversionPattern=%r %p %c %X{ki} - %m\n

For NDC, you would replace "%X{ki} with just "%x" (note the use of lower case).

As for the second part of your question, there are no limits to the values you can place within MDC or NDC.

最初的梦 2024-07-30 12:27:03

查看源代码,我看不出有任何不应该这样做的理由。

您是否尝试过成功写入 syslog,然后在 NDC/MDC 中写入某些内容? 请注意,您必须在 PatternLayout 中启用此功能(使用 %x 或 %X)。

MDC 似乎对您可以插入的内容没有任何限制(同样,基于检查源代码)。 我怀疑 MDC 写入只会对 MDC 的内容执行 toString() ,因此您可能会受到以这种方式呈现的对象的有用性/可读性的限制。

Looking at the source code, I don't see any reason why it shouldn't.

Have you tried successfully writing to syslog, and then writing with something in your NDC/MDC ? Note that you'll have to enable this in the PatternLayout (using %x or %X).

The MDC doesn't appear to have any limitations on what you can insert (again, based on inspecting the source code). I suspect the MDC writing will simply perform a toString() on the contents of the MDC, and so you may be limited simply by how useful/readable your objects are when rendered this way.

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