'log4j2: How do you create & set level for LevelMatchFilter in Java?

I'm trying to migrate the following Java code to log4j2:

private Filter setupWarningLevelFilter()
{
    LevelMatchFilter levelFilter = new LevelMatchFilter();
    levelFilter.setLevelToMatch(Level.ERROR.toString());
    levelFilter.setAcceptOnMatch(false);
    
    return levelFilter;
}

I have not found an example googling. (XML config won't help since custom appender doesn't recognize it - have to pass filter & layout in to custom appender via Java instead.) I don't understand how to interpret the cryptic apache documentation. I have gone in all the hacking circles I can think of using all kinds of combinations of build(), newBuilder(), etc. to create the LevelMatchFilter.

Related to this problem is a compilation error that appears to not like me importing both org.apache.logging.log4j.filter.LevelMatchFilter.Builder and org.apache.logging.log4j.layout.PatternLayout.Builder It gives error for PatternLayout.Builder stating " a type with the same simple name is already defined by the single-type-import of Builder".

Appreciate any insight/examples anyone can offer.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source