使用属性文件的log4j的多个过滤器 [英] Multiple filters for log4j using properties file
问题描述
我正在尝试使用这种配置过滤日志消息(我假装忽略包含文本Could not refresh JMS Connection for destination
以及包含文本org.apache.activemq.transport.failover.FailoverTransport
的消息):
I'm trying to filter log messages with this configuration (I pretend to ignore the messages containing the texts Could not refresh JMS Connection for destination
and also the ones containing org.apache.activemq.transport.failover.FailoverTransport
):
log4j.appender.stdout.filter.1=org.apache.log4j.varia.StringMatchFilter
log4j.appender.stdout.filter.1.StringToMatch=Could not refresh JMS Connection for destination
log4j.appender.stdout.filter.1.AcceptOnMatch=false
log4j.appender.stdout.filter.2=org.apache.log4j.varia.StringMatchFilter
log4j.appender.stdout.filter.2.StringToMatch=org.apache.activemq.transport.failover.FailoverTransport
log4j.appender.stdout.filter.2.AcceptOnMatch=false
但是只有第一个过滤器正在工作.配置多个过滤器的正确方法是哪种?
But only the first filter is working. Which is the correct way to configure multiple filters?
我正在使用log4j 1.2.17版本.
I'm using log4j 1.2.17 version.
推荐答案
我对此进行了广泛的研究,据我所知,该方法会随着时间的推移而有所变化,具体取决于您的版本.因此,我发现了应该可以使用的三种方法,但只有版本1适用于我:
I have worked on this extensively and as far as I can tell the method changes a little over time depending on your version. So I have found three methods that supposedly should work but only version 1 works for me:
log4j2.appender.event.filter.1.type = Filters
log4j2.appender.event.filter.1.a.type = RegexFilter
log4j2.appender.event.filter.1.a.regex = .*(C_Radon_(Level|Updated|Running)|C_Solar_(PowerTotal|PowerEast|PowerWest|TotalYield|DailyYield)|OFFLINE.*10\.13\.0\.70).*
log4j2.appender.event.filter.1.a.onMatch = DENY
log4j2.appender.event.filter.1.a.onMismatch = NEUTRAL
log4j2.appender.event.filter.1.b.type = RegexFilter
log4j2.appender.event.filter.1.b.regex = .*(C_Air_).*
log4j2.appender.event.filter.1.b.onMatch = DENY
log4j2.appender.event.filter.1.b.onMismatch = NEUTRAL
这可能会在控制台中生成并出错
This might generate and error in the console
org.ops4j.pax.logging.pax-logging-api [log4j2] ERROR : Filters contains invalid attributes "onMatch", "onMismatch" Ignored FQCN: org.apache.logging.log4j.spi.AbstractLogger
但是可以放心地忽略
According to the source code, this should work. https://github.com/apache/log4j/blob/7be00eed88152dd011a619e8bae5a631235c3f4c/src/main/java/org/apache/log4j/PropertyConfigurator.java#L881
## Danfoss Air Updates (because the key is cair this will be the first filter)
log4j2.appender.event.filter.1.type = RegexFilter
log4j2.appender.event.filter.1.regex = .*(Solar_PowerTotal).*
log4j2.appender.event.filter.1.onMatch = DENY
log4j2.appender.event.filter.1.onMismatch = NEUTRAL
## Frequest updates
log4j2.appender.event.filter.2.type = RegexFilter
log4j2.appender.event.filter.2.regex = .*(C_Solar_PowerWest).*
log4j2.appender.event.filter.2.onMatch = DENY
log4j2.appender.event.filter.2.onMismatch = NEUTRAL
3使用上面记录的方法
这实际上是log4j文档中正式记录的方法: http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PropertyConfigurator.html#method_detail
3 using your documented method above
This is actually the official documented method in the log4j docs: http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PropertyConfigurator.html#method_detail
在docs中指定直接在过滤器ID上使用该名称,并具有完全限定的名称(在openHAB2中从不对我有用)
In the docs is specifies to use the name directly on the filter ID and have a fully qualified name (never worked for me in openHAB2)
log4j2.appender.event.filter.1 = org.apache.logging.log4j.core.filter.RegexFilter
log4j2.appender.event.filter.1.regex = .*(C_Radon _(Level|Updated|Running)|C_Solar_(PowerTotal|PowerEast|PowerWest|TotalYield|DailyYield)|OFFLINE.*10\.13\.0\.70).*
log4j2.appender.event.filter.1.onMatch = DENY
log4j2.appender.event.filter.1.onMismatch = NEUTRAL
log4j2.appender.event.filter.2 = org.apache.logging.log4j.core.filter.RegexFilter
log4j2.appender.event.filter.2.regex = .*(C_Air_).*
log4j2.appender.event.filter.2.onMatch = DENY
log4j2.appender.event.filter.2.onMismatch = NEUTRAL
要注意的其他事项:
正则表达式过滤器的onMismatch名称已从onMisMatch更改为onMismatch,并且错误消息实际上给出了错误消息(因此,如果您键入onMisMatch将会抱怨键入了错误的onMismatch,这很有趣;-)
Additional things to be aware of:
The name of onMismatch for the Regex filter has changed from onMisMatch to onMismatch and the error messages actually give the wrong message (so if you type onMisMatch is will complain about a wrongly typed onMismatch, fun stuff;-)
多个过滤器应返回NEUTRAL,以使过滤器链继续. (最后一个过滤器可能是DENY/ACCEPT)
Multiple filters should return NEUTRAL in order for the filter chain to continue. (The last filter could be DENY/ACCEPT)
过滤器按ID排序,这就是为什么将ID作为数字或单个字母比实际命名更容易阅读的原因.
Filters are sorted by ID, which is why having the ID as a number or single letter makes easier reading than actually naming them.
这篇关于使用属性文件的log4j的多个过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!