org.apache.log4j.spi.Filter类的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(9.5k)|赞(0)|评价(0)|浏览(472)

本文整理了Java中org.apache.log4j.spi.Filter类的一些代码示例,展示了Filter类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Filter类的具体详情如下:
包路径:org.apache.log4j.spi.Filter
类名称:Filter

Filter介绍

[英]Users should extend this class to implement customized logging event filtering. Note that org.apache.log4j.Category and org.apache.log4j.AppenderSkeleton, the parent class of all standard appenders, have built-in filtering rules. It is suggested that you first use and understand the built-in rules before rushing to write your own custom filters.

This abstract class assumes and also imposes that filters be organized in a linear chain. The #decide method of each filter is called sequentially, in the order of their addition to the chain.

The #decide method must return one of the integer constants #DENY, #NEUTRAL or #ACCEPT.

If the value #DENY is returned, then the log event is dropped immediately without consulting with the remaining filters.

If the value #NEUTRAL is returned, then the next filter in the chain is consulted. If there are no more filters in the chain, then the log event is logged. Thus, in the presence of no filters, the default behaviour is to log all logging events.

If the value #ACCEPT is returned, then the log event is logged without consulting the remaining filters.

The philosophy of log4j filters is largely inspired from the Linux ipchains.

Note that filtering is only supported by the org.apache.log4j.xml.DOMConfigurator. The org.apache.log4j.PropertyConfigurator does not support filters.
[中]用户应扩展此类以实现自定义日志事件筛选。请注意,org。阿帕奇。log4j。类别和组织。阿帕奇。log4j。AppenderSkleton是所有标准Appender的父类,具有内置的筛选规则。建议您在匆忙编写自己的自定义过滤器之前,首先使用并理解内置规则。
这个抽象类假设并强制过滤器以线性链的形式组织。每个过滤器的#decise方法按其添加到链的顺序依次调用。
#decise方法必须返回整数常量#DENY、#NEUTRAL或#ACCEPT中的一个。
如果返回值#DENY,则会立即删除日志事件,而无需咨询其余筛选器。
如果返回值#NEUTRAL,则会参考链中的下一个过滤器。如果链中没有更多筛选器,则记录日志事件。因此,在没有过滤器的情况下,默认行为是记录所有日志事件。
如果返回值#ACCEPT,则记录日志事件,而不咨询其余筛选器。
log4j过滤器的原理在很大程度上受Linux ipchains的启发。
请注意,筛选仅由组织支持。阿帕奇。log4j。xml。DOMConfigulator。组织。阿帕奇。log4j。PropertyConfiguration不支持筛选器。

代码示例

代码示例来源:origin: log4j/log4j

/**
 * This method performs threshold checks and invokes filters before
 * delegating actual logging to the subclasses specific {@link
 * AppenderSkeleton#append} method.
 * */
public
synchronized 
void doAppend(LoggingEvent event) {
 if(closed) {
  LogLog.error("Attempted to append to closed appender named ["+name+"].");
  return;
 }
 
 if(!isAsSevereAsThreshold(event.getLevel())) {
  return;
 }
 Filter f = this.headFilter;
 
 FILTER_LOOP:
 while(f != null) {
  switch(f.decide(event)) {
  case Filter.DENY: return;
  case Filter.ACCEPT: break FILTER_LOOP;
  case Filter.NEUTRAL: f = f.getNext();
  }
 }
 
 this.append(event);    
}

代码示例来源:origin: log4j/log4j

/**
  Add a filter to end of the filter list.
  @since 0.9.0
 */
public
void addFilter(Filter newFilter) {
 if(headFilter == null) {
  headFilter = tailFilter = newFilter;
 } else {
  tailFilter.setNext(newFilter);
  tailFilter = newFilter;    
 }
}

代码示例来源:origin: org.eclipse.xtext/org.eclipse.xtext.junit4

private static void removeFilter(final Appender appender, final Filter filter) {
 Filter _filter = appender.getFilter();
 boolean _equals = Objects.equal(_filter, filter);
 if (_equals) {
  appender.clearFilters();
  Filter _next = filter.getNext();
  appender.addFilter(_next);
 } else {
  for (Filter current = appender.getFilter(); (!Objects.equal(current, null)); current = current.getNext()) {
   Filter _next_1 = current.getNext();
   boolean _equals_1 = Objects.equal(_next_1, filter);
   if (_equals_1) {
    Filter _next_2 = filter.getNext();
    current.setNext(_next_2);
    return;
   }
  }
 }
}

代码示例来源:origin: camunda/camunda-bpm-platform

/**
 * This method performs threshold checks and invokes filters before
 * delegating actual logging to the subclasses specific {@link
 * AppenderSkeleton#append} method.
 * */
public
synchronized 
void doAppend(LoggingEvent event) {
 if(closed) {
  LogLog.error("Attempted to append to closed appender named ["+name+"].");
  return;
 }
 
 if(!isAsSevereAsThreshold(event.getLevel())) {
  return;
 }
 Filter f = this.headFilter;
 
 FILTER_LOOP:
 while(f != null) {
  switch(f.decide(event)) {
  case Filter.DENY: return;
  case Filter.ACCEPT: break FILTER_LOOP;
  case Filter.NEUTRAL: f = f.next;
  }
 }
 
 this.append(event);    
}

代码示例来源:origin: ops4j/org.ops4j.pax.logging

/**
 *  {@inheritDoc}
 */
public void activateOptions() {
 for (Filter f = headFilter; f != null; f = f.next) {
  f.activateOptions();
 }
}

代码示例来源:origin: org.ops4j.pax.logging/pax-logging-service

/**
 * If this event does not already contain location information, 
 * evaluate the event against the expression.
 * 
 * If the expression evaluates to true, generate a LocationInfo instance 
 * by creating an exception and set this LocationInfo on the event.
 * 
 * Returns {@link Filter#NEUTRAL}
 */
public int decide(final LoggingEvent event) {
 boolean accepted = true;
 Filter f = headFilter;
 while (f != null) {
  accepted = accepted && (Filter.ACCEPT == f.decide(event));
  f = f.next;
 }
 if (accepted) {
  if(acceptOnMatch) {
   return Filter.ACCEPT;
  }
   return Filter.DENY;
 }
 return Filter.NEUTRAL;
}

代码示例来源:origin: org.ops4j.pax.logging/pax-logging-service

/**
 *  {@inheritDoc}
 */
public void activateOptions() {
 for (Filter f = headFilter; f != null; f = f.next) {
  f.activateOptions();
 }
}

代码示例来源:origin: apache/log4j

/**
 * This method performs threshold checks and invokes filters before
 * delegating actual logging to the subclasses specific {@link
 * AppenderSkeleton#append} method.
 * */
public
synchronized 
void doAppend(LoggingEvent event) {
 if(closed) {
  LogLog.error("Attempted to append to closed appender named ["+name+"].");
  return;
 }
 
 if(!isAsSevereAsThreshold(event.getLevel())) {
  return;
 }
 Filter f = this.headFilter;
 
 FILTER_LOOP:
 while(f != null) {
  switch(f.decide(event)) {
  case Filter.DENY: return;
  case Filter.ACCEPT: break FILTER_LOOP;
  case Filter.NEUTRAL: f = f.getNext();
  }
 }
 
 this.append(event);    
}

代码示例来源:origin: apache/log4j

/**
  Add a filter to end of the filter list.
  @since 0.9.0
 */
public
void addFilter(Filter newFilter) {
 if(headFilter == null) {
  headFilter = tailFilter = newFilter;
 } else {
  tailFilter.setNext(newFilter);
  tailFilter = newFilter;    
 }
}

代码示例来源:origin: org.ops4j.pax.logging/pax-logging-service

/**
  * {@inheritDoc}
  */
public boolean isTriggeringEvent(LoggingEvent event) {
 //
 //   in the abnormal case of no contained filters
 //     always return true to avoid each logging event
 //     from having its own file.
 if (headFilter == null) {
  return false;
 }
 //
 //    otherwise loop through the filters
 //
 for (Filter f = headFilter; f != null; f = f.next) {
  switch (f.decide(event)) {
  case Filter.DENY:
   return false;
  case Filter.ACCEPT:
   return true;
  }
 }
 return true;
 }

代码示例来源:origin: org.jboss.logmanager/log4j-jboss-logmanager

public boolean isLoggable(final LogRecord record) {
    final ExtLogRecord extRec = ExtLogRecord.wrap(record);
    Filter filter = filterChain;
    while (filter != null) {
      final int result = filter.decide(new LoggingEvent(extRec, Logger.getLogger(record.getLoggerName())));
      switch (result) {
        case Filter.DENY: return false;
        case Filter.ACCEPT: return true;
      }
      filter = filter.getNext();
    }
    return defaultResult;
  }
}

代码示例来源:origin: org.apache.logging.log4j/log4j-1.2-api

@Override
public void addFilter(Filter newFilter) {
  if(headFilter == null) {
    headFilter = tailFilter = newFilter;
  } else {
    tailFilter.setNext(newFilter);
    tailFilter = newFilter;
  }
}

代码示例来源:origin: ops4j/org.ops4j.pax.logging

/**
 * If this event does not already contain location information, 
 * evaluate the event against the expression.
 * 
 * If the expression evaluates to true, generate a LocationInfo instance 
 * by creating an exception and set this LocationInfo on the event.
 * 
 * Returns {@link Filter#NEUTRAL}
 */
public int decide(final LoggingEvent event) {
 boolean accepted = true;
 Filter f = headFilter;
 while (f != null) {
  accepted = accepted && (Filter.ACCEPT == f.decide(event));
  f = f.next;
 }
 if (accepted) {
  if(acceptOnMatch) {
   return Filter.ACCEPT;
  }
   return Filter.DENY;
 }
 return Filter.NEUTRAL;
}

代码示例来源:origin: org.jboss.logmanager/jboss-logmanager-log4j

/**
   * Determine if the record is loggable.
   *
   * @param record the log record
   * @return {@code true} if it is loggable
   */
  public boolean isLoggable(final LogRecord record) {
    final ExtLogRecord extRec = ExtLogRecord.wrap(record);
    Filter filter = filterChain;
    while (filter != null) {
      final int result = filter.decide(new ConvertedLoggingEvent(extRec));
      switch (result) {
        case Filter.DENY: return false;
        case Filter.ACCEPT: return true;
      }
      filter = filter.getNext();
    }
    return defaultResult;
  }
}

代码示例来源:origin: apache-log4j/log4j

/**
  Add a filter to end of the filter list.
  @since 0.9.0
 */
public
void addFilter(Filter newFilter) {
 if(headFilter == null) {
  headFilter = tailFilter = newFilter;
 } else {
  tailFilter.setNext(newFilter);
  tailFilter = newFilter;    
 }
}

代码示例来源:origin: ops4j/org.ops4j.pax.logging

/**
  * {@inheritDoc}
  */
public boolean isTriggeringEvent(LoggingEvent event) {
 //
 //   in the abnormal case of no contained filters
 //     always return true to avoid each logging event
 //     from having its own file.
 if (headFilter == null) {
  return false;
 }
 //
 //    otherwise loop through the filters
 //
 for (Filter f = headFilter; f != null; f = f.next) {
  switch (f.decide(event)) {
  case Filter.DENY:
   return false;
  case Filter.ACCEPT:
   return true;
  }
 }
 return true;
 }

代码示例来源:origin: org.ops4j.pax.logging/pax-logging-service

public void doAppend(LoggingEvent event) {
 if(closed) {
  LogLog.error("Attempted to append to closed appender named ["+name+"].");
  return;
 }
 if(!isAsSevereAsThreshold(event.getLevel())) {
  return;
 }
 Filter f = this.headFilter;
 FILTER_LOOP:
 while(f != null) {
  switch(f.decide(event)) {
  case Filter.DENY: return;
  case Filter.ACCEPT: break FILTER_LOOP;
  case Filter.NEUTRAL: f = f.getNext();
  }
 }
 this.append(event);
}

代码示例来源:origin: org.apache.log4j/com.springsource.org.apache.log4j

/**
  Add a filter to end of the filter list.
  @since 0.9.0
 */
public
void addFilter(Filter newFilter) {
 if(headFilter == null) {
  headFilter = tailFilter = newFilter;
 } else {
  tailFilter.setNext(newFilter);
  tailFilter = newFilter;    
 }
}

代码示例来源:origin: org.ops4j.pax.logging/pax-logging-service

public int decide(LoggingEvent event)
{
  PaxFilter filter = (PaxFilter) m_tracker.getService();
  if (filter != null)
  {
    return filter.doFilter( new PaxLoggingEventImpl( event ) );
  }
  return m_fallback.decide( event );
}

代码示例来源:origin: ops4j/org.ops4j.pax.logging

public void doAppend(LoggingEvent event) {
 if(closed) {
  LogLog.error("Attempted to append to closed appender named ["+name+"].");
  return;
 }
 if(!isAsSevereAsThreshold(event.getLevel())) {
  return;
 }
 Filter f = this.headFilter;
 FILTER_LOOP:
 while(f != null) {
  switch(f.decide(event)) {
  case Filter.DENY: return;
  case Filter.ACCEPT: break FILTER_LOOP;
  case Filter.NEUTRAL: f = f.getNext();
  }
 }
 this.append(event);
}

相关文章