问题
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?
I'm using log4j 1.2.17 version.
回答1:
if the first filter delivers DENY or ACCEPT then the second filter is not being called.
if the return value is NEUTRAL - the second filter is called.
I'm not sure, but you could use regular expression in one filter.
source link
回答2:
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:
Using the filters element
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
but that can safely be ignored
2 using type property and multiple filters directly on the filter property
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 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
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
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;-)
Multiple filters should return NEUTRAL in order for the filter chain to continue. (The last filter could be DENY/ACCEPT)
Filters are sorted by ID, which is why having the ID as a number or single letter makes easier reading than actually naming them.
来源:https://stackoverflow.com/questions/34085162/multiple-filters-for-log4j-using-properties-file