如何筛选log4j2中没有标记集?

时间:2018-01-08 09:40:02

标签: java log4j2

我想以这样的方式配置log4j2,以便记录所有没有标记的消息。如果配置文件中有此标记的特殊ACCEPT规则,则只应记录带标记的所有消息。

我没有找到任何可以检测"无标记集的过滤规则"案件。有没有或者我必须自己实施?

log4j2配置的过滤器部分可能类似于以下示例,但 NoMarkerFilter 语句应该捕获没有设置标记的情况。

<Filters>
        <MarkerFilter marker="SQL_UPDATE" onMatch="ACCEPT" onMismatch="NEUTRAL" />
        <NoMarkerFilter onMatch="ACCEPT" onMismatch="DENY" />
</Filters>

1 个答案:

答案 0 :(得分:2)

我遇到了类似的问题,但找不到log4j2提供的任何此类实现。不得不自己实施一个。

@Plugin(name = "NoMarkerFilter", category = Node.CATEGORY, elementType = Filter.ELEMENT_TYPE, printObject = true)
public class NoMarkerFilter extends AbstractFilter {


  private NoMarkerFilter(final Result onMatch, final Result onMismatch) {
    super(onMatch, onMismatch);
  }

  @Override
  public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
                       final Object... params) {
    return filter(marker);
  }

  @Override
  public Result filter(final Logger logger, final Level level, final Marker marker, final Object msg,
                       final Throwable t) {
    return filter(marker);
  }

  @Override
  public Result filter(final Logger logger, final Level level, final Marker marker, final Message msg,
                       final Throwable t) {
    return filter(marker);
  }

  @Override
  public Result filter(final LogEvent event) {
    return filter(event.getMarker());
  }

  @Override
  public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
                       final Object p0) {
    return filter(marker);
  }

  @Override
  public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
                       final Object p0, final Object p1) {
    return filter(marker);
  }

  @Override
  public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
                       final Object p0, final Object p1, final Object p2) {
    return filter(marker);
  }

  @Override
  public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
                       final Object p0, final Object p1, final Object p2, final Object p3) {
    return filter(marker);
  }

  @Override
  public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
                       final Object p0, final Object p1, final Object p2, final Object p3,
                       final Object p4) {
    return filter(marker);
  }

  @Override
  public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
                       final Object p0, final Object p1, final Object p2, final Object p3,
                       final Object p4, final Object p5) {
    return filter(marker);
  }

  @Override
  public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
                       final Object p0, final Object p1, final Object p2, final Object p3,
                       final Object p4, final Object p5, final Object p6) {
    return filter(marker);
  }

  @Override
  public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
                       final Object p0, final Object p1, final Object p2, final Object p3,
                       final Object p4, final Object p5, final Object p6,
                       final Object p7) {
    return filter(marker);
  }

  @Override
  public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
                       final Object p0, final Object p1, final Object p2, final Object p3,
                       final Object p4, final Object p5, final Object p6,
                       final Object p7, final Object p8) {
    return filter(marker);
  }

  @Override
  public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
                       final Object p0, final Object p1, final Object p2, final Object p3,
                       final Object p4, final Object p5, final Object p6,
                       final Object p7, final Object p8, final Object p9) {
    return filter(marker);
  }

  private Result filter(final Marker marker) {
    return null == marker ? onMatch : onMismatch;
  }

  /**
   * Create the NoMarkerFilter.
   *
   * @param match    The action to take if a match occurs.
   * @param mismatch The action to take if no match occurs.
   * @return A NoMarkerFilter.
   */
  @PluginFactory
  public static NoMarkerFilter createFilter(
      @PluginAttribute("onMatch") final Result match,
      @PluginAttribute("onMismatch") final Result mismatch) {
    return new NoMarkerFilter(match, mismatch);
  }
}

这样,以下配置应该有效:

<Filters>
        <MarkerFilter marker="SQL_UPDATE" onMatch="ACCEPT" onMismatch="NEUTRAL" />
        <NoMarkerFilter onMatch="ACCEPT" onMismatch="DENY" />
</Filters>