title: Log4j2 custom filter
body: <p>I'm attempting to implement and configure a custom Filter in Log4J2 - based on ThresholdFilter, but intended to do more. I've seen topics on custom appenders, which follow the same plugin annotation syntax, but haven't found a topic on custom fitlers.</p>
  
  <p><strong>MyCustomFilter.java</strong> (based on ThresholdFilter)</p>
  
  <pre><code>@Plugin(name = "MyCustomFilter", category = Node.CATEGORY, elementType = Filter.ELEMENT_TYPE, printObject = true)
  public class MyCustomFilter extends AbstractFilter {
  
      private static final long serialVersionUID = 1L;
  
      private final Level level;
  
      private MyCustomFilter(final Level level, final Result onMatch, final Result onMismatch) {
          super(onMatch, onMismatch);
          this.level = level;
      }
  
      @Override
      public Result filter(final Logger logger, final Level level, final Marker marker, final String msg, final Object... params) {
          return filter(level);
      }
  
      @Override
      public Result filter(final Logger logger, final Level level, final Marker marker, final Object msg, final Throwable t) {
          return filter(level);
      }
  
      @Override
      public Result filter(final Logger logger, final Level level, final Marker marker, final Message msg, final Throwable t) {
          return filter(level);
      }
  
      @Override
      public Result filter(final LogEvent event) {
          return filter(event.getLevel());
      }
  
      private Result filter(final Level level) {
          return level.isMoreSpecificThan(this.level) ? onMatch : onMismatch;
      }
  
      @Override
      public String toString() {
          return level.toString();
      }
  
      /**
       * Create a MyCustomFilter.
       * 
       * @param level
       *            The log Level.
       * @param match
       *            The action to take on a match.
       * @param mismatch
       *            The action to take on a mismatch.
       * @return The created MyCustomFilter.
       */
      @PluginFactory
      public static MyCustomFilter createFilter(@PluginAttribute("level") final Level level, @PluginAttribute("onMatch") final Result match,
              @PluginAttribute("onMismatch") final Result mismatch) {
          final Level actualLevel = level == null ? Level.ERROR : level;
          final Result onMatch = match == null ? Result.NEUTRAL : match;
          final Result onMismatch = mismatch == null ? Result.DENY : mismatch;
          return new MyCustomFilter(actualLevel, onMatch, onMismatch);
      }
  
  }
  </code></pre>
  
  <p><strong>log4j2.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;Configuration&gt;
  
    &lt;MyCustomFilter level="fatal" /&gt;
  
    &lt;Appenders&gt;
      &lt;Console name="STDOUT" target="SYSTEM_OUT"&gt;
        &lt;PatternLayout pattern="%d %-5p [%c{5}.%M():%L] %m%n" /&gt;
      &lt;/Console&gt;
    &lt;/Appenders&gt;
  
    &lt;Loggers&gt;
      &lt;Logger name="com.me.test.logger" level="info" additivity="false"&gt;
        &lt;AppenderRef ref="STDOUT" /&gt;
      &lt;/Logger&gt;
  
      &lt;!-- Root Logger --&gt;
      &lt;Root level="warn"&gt;
        &lt;AppenderRef ref="STDOUT" /&gt;
      &lt;/Root&gt;
    &lt;/Loggers&gt;
  
  &lt;/Configuration&gt;
  </code></pre>
  
  <p><strong>LoggingRunner.java</strong></p>
  
  <pre><code>public class LoggingRunner {
  
      public static void main(String[] args) {
          Logger logger = LogManager.getLogger("com.me.test.logger");
  
          logger.fatal("Fatal");
          logger.error("Error");
          logger.debug("Debug");
          logger.info("Info");
  
          System.out.println("end it");
      }
  
  }
  </code></pre>
  
  <p>The configuration syntax seems to match that in the Apache documentation and mimics ThresholdFilter (a working, provided filter). If I place it as shown, as a Context level filter, I don't see any errors, but the filter isn't invoked or applied.</p>
  
  <p>If I move my custom filter tag inside an Appender (which is possible with out-of-the-box filters, I get <code>2015-03-24 16:20:11,713 ERROR AppenderRef contains an invalid element or attribute "MyCustomFilter"</code>.</p>
  
  <p>Based on <a href="http://logging.apache.org/log4j/2.x/" rel="nofollow" title="Apache Log4J2 Documentation">Apache Log4J2 documentation</a> and examples in the log42j core source, I thought this would work.</p>
  
  <p>What am I doing wrong?</p>