'Log4j2 custom filter not working getting Filters contains an invalid element or attribute

While working with log4j custom filters getting "ERROR Filters contains an invalid element or attribute StringMatchFilter" while tomcat server startup

My class

@Plugin(
        name = "StringMatchFilter",
        category = "Core",
        elementType = "filter",
        printObject = true
)
public class StringMatchFilter extends AbstractFilter {
    @SuppressWarnings("unused")
    private static final String VERSION = ModuleVersion
            .register("$Id: StringMatchFilter.java 207693 2016-08-11 23:17:59Z DavisWalsh $");
    private static final long serialVersionUID = 5382929677973762187L;

    private boolean acceptOnMatch = true;
    private String stringToMatch;

    public void setStringToMatch(String s) {
        stringToMatch = s;
    }

    public String getStringToMatch() {
        return stringToMatch;
    }

    public void setAcceptOnMatch(boolean acceptOnMatch) {
        this.acceptOnMatch = acceptOnMatch;
    }

    public boolean getAcceptOnMatch() {
        return acceptOnMatch;
    }

    /**
     * Called by log4j to decide if event is to be logged.
     * @param event 
     * @return result
     */
    @Override
    public Result filter(final LogEvent event) {
        String msg = event.getMessage().getFormattedMessage();

        if(msg == null || stringToMatch == null)
            return Result.NEUTRAL;

        if( msg.indexOf(stringToMatch) == -1 ) {
            return Result.NEUTRAL;
        } else { // we've got a match
            if(acceptOnMatch) {
                return Result.ACCEPT;
            } else {
                return Result.DENY;
            }
        }
    }
}

my log4j2.xml file looks like below

<Configuration status="warn" >
<Appenders>
<RollingRandomAccessFile  name="Mytest" fileName="${sys:test.logdir}/TestRULES.log" filePattern="${sys:test.logdir}/TestRULES-%d{MM-dd-yyyy}-%i.log.gz">
    <PatternLayout>
                <Pattern>%d [%20.20t] [%10.10X{pegathread}] [%20.20X{tenantid}] [%20.20X{app}] (%30.30c{3}) %-5p %X{stack} %X{userid} - %m%n</Pattern>
            </PatternLayout>
<Filters>

<StringMatchFilter stringToMatch="StringMatchFilter"/>
 </Filters>     
</Appenders>
</Configuration>

Can any one help me. Even for my requirement i'm trying to use in built logger i.e "<RegexFilter regex=".*MatchFilter.*" onMatch="ACCEPT" onMismatch="DENY"/>", but at run time Filter object not getting initialized. I would like to know is any thing wrong i'm doing?



Sources

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

Source: Stack Overflow

Solution Source