如何将 log4j 消息路由到唯一的附加程序

发布于 2025-01-01 06:24:47 字数 920 浏览 2 评论 0原文

log4j 中,大多数(如果不是全部)appender 都会扩展 AppenderSkeleton,它提供了 setThreshold(Priority) 方法来设置消息的最低“级别”必须设置为以便附加程序将其记录在某处。

我想根据消息的确切级别/优先级将消息记录到不同的附加程序。

例如,我希望 DEBUG 消息记录到 ConsoleAppender,而不是其他地方。我希望 INFO 消息记录到 FileAppender 而不是其他地方。我希望错误消息记录到 JMSAppender 而不是其他地方。

问题在于这个 setThreshold(Priority) 方法,它设置记录消息所需的“最小阈值”。

当然,我可以将 ConsoleAppender 的阈值设置为 DEBUG,但由于 INFO 和 ERROR 消息比 DEBUG 消息“更高”,因此我也会收到发送到 ConsoleAppender 的 INFO 和 ERROR 消息。

是否有一些方法或方式可以准确地配置附加程序“级别”/优先级,或者这个最小阈值是我唯一的资源?

使用 LevelMatchFilter 进行编辑
这是朝着正确方向迈出的一步吗?

LevelMatchFilter filter = new LevelMatchFilter();
filter.setLevelToMatch(Level.DEBUG.toString());

consoleAppender.addFilter(filter);

上面的代码片段能否完成确保 ConsoleAppender 记录 DEBUG 且仅记录 DEBUG 的工作?

In log4j most (if not all) appenders extend AppenderSkeleton which provides a setThreshold(Priority) method for setting the minimal "level" that a message must be set to in order for the appender to log it somewhere.

I want to log messages to different appenders based on the exact level/priority of the message.

For instance, I want DEBUG messages to log to a ConsoleAppender, but nowhere else. I want INFO messages to log to a FileAppender and nowhere else. I want ERROR messages to log to a JMSAppender and nowhere else.

The problem is this setThreshold(Priority) method, which sets the "minimal threshold" necessary to log a message.

Sure, I could set the ConsoleAppender's threshold to DEBUG, but since INFO and ERROR messages are "higher" than DEBUG messages, I will also get INFO and ERROR messages sent to the ConsoleAppender as well.

Are there are methods or ways of configuring appender "levels"/priorities exactly or is this minimal threshold my only recourse?

Edit using LevelMatchFilter:

Is this a step in the right direction?

LevelMatchFilter filter = new LevelMatchFilter();
filter.setLevelToMatch(Level.DEBUG.toString());

consoleAppender.addFilter(filter);

Would that code snippet above accomplish the job of making sure the ConsoleAppender logs DEBUGs, and only DEBUGs?

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

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

发布评论

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

评论(1

拿命拼未来 2025-01-08 06:24:47

您可以将 LevelMatchFilter 附加到任何附加程序仅按确切级别过滤日志消息。

来自javadoc:

这是一个基于级别匹配的非常简单的过滤器。

该过滤器接受两个选项 LevelToMatchAcceptOnMatch。如果 LevelToMatch 选项的值与 LoggingEvent 的级别完全匹配,则 decide(org.apache.log4j.spi.LoggingEvent ) 方法在 AcceptOnMatch 选项值设置为 true 的情况下返回 Filter.ACCEPT,如果为 false,则返回 Filter.DENY被返回。如果没有匹配,则返回Filter.NEUTRAL

You can attach a LevelMatchFilter to any appender to filter log messages by only the exact level.

From the javadoc:

This is a very simple filter based on level matching.

The filter admits two options LevelToMatch and AcceptOnMatch. If there is an exact match between the value of the LevelToMatch option and the level of the LoggingEvent, then the decide(org.apache.log4j.spi.LoggingEvent) method returns Filter.ACCEPT in case the AcceptOnMatch option value is set to true, if it is false then Filter.DENY is returned. If there is no match, Filter.NEUTRAL is returned.

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