如何将 log4j 消息路由到唯一的附加程序
在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将 LevelMatchFilter 附加到任何附加程序仅按确切级别过滤日志消息。
来自javadoc:
这是一个基于级别匹配的非常简单的过滤器。
该过滤器接受两个选项
LevelToMatch
和AcceptOnMatch
。如果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
andAcceptOnMatch
. If there is an exact match between the value of theLevelToMatch
option and the level of theLoggingEvent
, then thedecide(org.apache.log4j.spi.LoggingEvent)
method returnsFilter.ACCEPT
in case theAcceptOnMatch
option value is set to true, if it is false thenFilter.DENY
is returned. If there is no match,Filter.NEUTRAL
is returned.