logging-log4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Tony DeFusco (JIRA)" <j...@apache.org>
Subject [jira] [Created] (LOG4J2-1039) SMTPAppender needs the ability to filter out events that contain a specific Marker
Date Tue, 02 Jun 2015 21:36:49 GMT
Tony DeFusco created LOG4J2-1039:
------------------------------------

             Summary: SMTPAppender needs the ability to filter out events that contain a specific
Marker
                 Key: LOG4J2-1039
                 URL: https://issues.apache.org/jira/browse/LOG4J2-1039
             Project: Log4j 2
          Issue Type: Improvement
          Components: Appenders, Filters
    Affects Versions: 2.3
            Reporter: Tony DeFusco


I have a use case where some of my logged events have been marked with a custom _Marker_ (e.g.
"PRIVATE").  When the _SMTP Appender_ sends out an e-mail with the triggering event and its
cyclic buffer of collected log events, I do not want the log events that have been set with
a custom _Marker_  to be included in the e-mail.

Such a use case could be configured as follows for an _SMTP Appender_ that needs to respond
to *WARN* level or greater:
{code:xml}
<Appender type="SMTP" name="smtp"
              bufferSize="5"
              smtpHost="${smtpHost}" smtpPort="${smtpPort}" smtpProtocol="${smtpProtocol}"
              smtpUsername="${smtpUsername}" smtpPassword="${smtpPassword}"
              subject="${smtpSubject}"
              from="${smtpFrom}"
              to="${smtpTo}"
              replyTo="${smtpReplyTo}"
              cc="${smtpCc}"
              bcc="${smtpBcc}"
       >
      <Layout type="PatternLayout">
        <Pattern>%m</Pattern>
      </Layout>
      <Filters>
        <MarkerFilter marker="PRIVATE" onMatch="DENY" onMismatch="NEUTRAL"/>
        <ThresholdFilter level="warn" onMatch="ACCEPT" onMismatch="DENY"/>
      </Filters>
    </Appender>
  </Appenders>
{code}

Here is a patch that I made to _org.apache.logging.log4j.core.appender.SmtpAppender.java_
that seems to support this use case:
{code:title=org.apache.logging.log4j.core.appender.SmtpAppender.java}...

package org.apache.logging.log4j.core.appender;

import java.io.Serializable;
import java.util.List;

import org.apache.logging.log4j.core.Filter;
import org.apache.logging.log4j.core.Layout;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
import org.apache.logging.log4j.core.config.plugins.PluginElement;
import org.apache.logging.log4j.core.config.plugins.PluginFactory;
import org.apache.logging.log4j.core.filter.CompositeFilter;
import org.apache.logging.log4j.core.filter.MarkerFilter;
import org.apache.logging.log4j.core.filter.ThresholdFilter;
import org.apache.logging.log4j.core.layout.HtmlLayout;
import org.apache.logging.log4j.core.net.SmtpManager;
import org.apache.logging.log4j.core.util.Booleans;

...

  /**
   * Capture all events in CyclicBuffer.  Ignore events that are
   * denied by a MarkerFilter.
   * @param event The Log event.
   * @return true if the event should be filtered.
   */
  @Override
  public boolean isFiltered(final LogEvent event) {
    boolean filtered = false;

    final Filter filter = this.getFilter();
    if (filter instanceof CompositeFilter) {
      final List<Filter> filters = ((CompositeFilter)filter).getFilters();
      for (final Filter aFilter : filters) {
        final Filter.Result filterResult = (aFilter != null) ? aFilter.filter(event) : Filter.Result.NEUTRAL;
        if (!Filter.Result.NEUTRAL.equals(filterResult)) {
          filtered = (Filter.Result.DENY.equals(filterResult));
          if (filtered) {
            final boolean isMarkerFilter = aFilter instanceof MarkerFilter;
            // Ignore events that are denied by a MarkerFilter.
            if (!isMarkerFilter) {
              manager.add(event);
            }
          }
          break;
        }
      }
    }
    else {
      filtered = super.isFiltered(event);
      if (filtered) {
        final boolean isMarkerFilter = filter instanceof MarkerFilter;
        if (!isMarkerFilter) {
          manager.add(event);
        }
      }
    }

    return filtered;
  }

...{code}



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-dev-help@logging.apache.org


Mime
View raw message