ch.qos.logback.core.filter.Filter Java Examples

The following examples show how to use ch.qos.logback.core.filter.Filter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AbstractVirtualHostTest.java    From qpid-broker-j with Apache License 2.0 7 votes vote down vote up
private void assertActionProducesLogMessage(final Runnable action, final String loggerName,
                                            final Level logLevel, final String message) throws Exception
{
    final CountDownLatch logMessageReceivedLatch = new CountDownLatch(1);
    ch.qos.logback.classic.Logger rootLogger = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
    ListAppender<ILoggingEvent> appender = new ListAppender<>();
    appender.addFilter(new Filter<ILoggingEvent>()
    {
        @Override
        public FilterReply decide(final ILoggingEvent event)
        {
            if (event.getLoggerName().equals(loggerName) && event.getLevel().equals(logLevel) && event.getFormattedMessage().contains(message))
            {
                logMessageReceivedLatch.countDown();
            }
            return FilterReply.NEUTRAL;
        }
    });
    appender.setContext(rootLogger.getLoggerContext());
    appender.start();
    rootLogger.addAppender(appender);

    action.run();
    assertTrue("Did not receive expected log message", logMessageReceivedLatch.await(2, TimeUnit.SECONDS));
}
 
Example #2
Source File: BaleenLoggerBuilderTest.java    From baleen with Apache License 2.0 6 votes vote down vote up
/** Test abstract functions (using console implementation) and the functions of console */
@Test
public void testAbstractAndConsole() {
  BaleenConsoleLoggerBuilder builder =
      new BaleenConsoleLoggerBuilder(
          NAME, BaleenLogging.DEFAULT_PATTERN, new MinMaxFilter(Level.INFO, Level.WARN));

  LoggerContext context = new LoggerContext();
  Encoder<ILoggingEvent> encoder = new PatternLayoutEncoder();

  Appender<ILoggingEvent> appender = builder.build(context, encoder);

  assertEquals(NAME, builder.getName());

  assertTrue(appender instanceof ConsoleAppender);
  assertEquals(encoder, ((ConsoleAppender<ILoggingEvent>) appender).getEncoder());

  assertNotNull(appender);
  assertEquals(NAME, appender.getName());
  Filter<ILoggingEvent> filter = appender.getCopyOfAttachedFiltersList().get(0);
  assertTrue(filter instanceof MinMaxFilter);
  assertEquals(Level.INFO, ((MinMaxFilter) filter).getMin());
  assertEquals(Level.WARN, ((MinMaxFilter) filter).getMax());
}
 
Example #3
Source File: ServiceMain.java    From twill with Apache License 2.0 6 votes vote down vote up
private KafkaAppender getKafkaAppender(LoggerContext context) {
  KafkaAppender kafkaAppender = new KafkaAppender();
  kafkaAppender.setName("KAFKA");
  kafkaAppender.setTopic(Constants.LOG_TOPIC);
  kafkaAppender.setHostname(getHostname());
  // The Kafka ZK Connection shouldn't be null as this method only get called if log collection is enabled
  kafkaAppender.setZookeeper(getTwillRuntimeSpecification().getKafkaZKConnect());
  String runnableName = getRunnableName();
  if (runnableName != null) {
    kafkaAppender.setRunnableName(runnableName);
  }

  kafkaAppender.addFilter(new Filter<ILoggingEvent>() {
    @Override
    public FilterReply decide(ILoggingEvent event) {
      return event.getLoggerName().startsWith("kafka.") ? FilterReply.DENY : FilterReply.ACCEPT;
    }
  });
  kafkaAppender.setContext(context);

  return kafkaAppender;
}
 
Example #4
Source File: StartupAppender.java    From qpid-broker-j with Apache License 2.0 6 votes vote down vote up
public void logToConsole()
{
    Context context = getContext();
    ConsoleAppender<ILoggingEvent> consoleAppender = new ConsoleAppender<>();
    consoleAppender.setContext(context);
    PatternLayoutEncoder patternLayoutEncoder = new PatternLayoutEncoder();
    patternLayoutEncoder.setContext(context);

    // added MDC variable 'qpid.log.prefix' for test purposes
    patternLayoutEncoder.setPattern("%X{qpid.log.prefix}%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n");
    patternLayoutEncoder.start();

    consoleAppender.addFilter(new Filter<ILoggingEvent>()
    {
        @Override
        public FilterReply decide(final ILoggingEvent event)
        {
            return event.getLevel().isGreaterOrEqual(_consoleAppenderAcceptLogLevel) ? FilterReply.ACCEPT : FilterReply.DENY;
        }
    });

    consoleAppender.setEncoder(patternLayoutEncoder);
    consoleAppender.start();
    replayAccumulatedEvents(consoleAppender);
    consoleAppender.stop();
}
 
Example #5
Source File: XodusFileDataWriterLogLevelModificatorSingularityTest.java    From hivemq-community-edition with Apache License 2.0 5 votes vote down vote up
@NotNull
private Filter<ILoggingEvent> createFilter(final CountDownLatch countDownLatch, final String text) {
    return new Filter<ILoggingEvent>() {
        @Override
        public FilterReply decide(final ILoggingEvent event) {
            if (event.getLevel().equals(Level.DEBUG)) {
                if (event.getFormattedMessage().equals(text)) {
                    countDownLatch.countDown();
                    return FilterReply.NEUTRAL;
                }
            }
            return FilterReply.NEUTRAL;
        }
    };
}
 
Example #6
Source File: BaleenLoggerBuilderTest.java    From baleen with Apache License 2.0 5 votes vote down vote up
@Test
public void testAbstractAndConsoleMultipleFilters() {
  BaleenConsoleLoggerBuilder builder =
      new BaleenConsoleLoggerBuilder(
          NAME,
          BaleenLogging.DEFAULT_PATTERN,
          Arrays.asList(
              new MinMaxFilter(Level.INFO, Level.ERROR),
              new MinMaxFilter(Level.INFO, Level.WARN)));

  LoggerContext context = new LoggerContext();
  Encoder<ILoggingEvent> encoder = new PatternLayoutEncoder();

  Appender<ILoggingEvent> appender = builder.build(context, encoder);

  assertEquals(NAME, builder.getName());

  assertTrue(appender instanceof ConsoleAppender);
  assertEquals(encoder, ((ConsoleAppender<ILoggingEvent>) appender).getEncoder());

  assertNotNull(appender);
  assertEquals(NAME, appender.getName());
  assertEquals(2, appender.getCopyOfAttachedFiltersList().size());
  Filter<ILoggingEvent> filter = appender.getCopyOfAttachedFiltersList().get(0);
  assertTrue(filter instanceof MinMaxFilter);
  assertEquals(Level.INFO, ((MinMaxFilter) filter).getMin());
  assertEquals(Level.ERROR, ((MinMaxFilter) filter).getMax());

  filter = appender.getCopyOfAttachedFiltersList().get(1);
  assertTrue(filter instanceof MinMaxFilter);
  assertEquals(Level.INFO, ((MinMaxFilter) filter).getMin());
  assertEquals(Level.WARN, ((MinMaxFilter) filter).getMax());

  // TODO: Test that both filters are being applied
}
 
Example #7
Source File: BaleenLoggerBuilderTest.java    From baleen with Apache License 2.0 5 votes vote down vote up
@Test
public void testNullFilters() {
  BaleenConsoleLoggerBuilder singleBuilder =
      new BaleenConsoleLoggerBuilder(BaleenLogging.DEFAULT_PATTERN, (Filter<ILoggingEvent>) null);
  List<Filter<ILoggingEvent>> list = null;
  BaleenConsoleLoggerBuilder listBuilder =
      new BaleenConsoleLoggerBuilder(BaleenLogging.DEFAULT_PATTERN, list);

  singleBuilder.build(null, null);
  listBuilder.build(null, null);
}
 
Example #8
Source File: BaleenFileLoggerBuilder.java    From baleen with Apache License 2.0 5 votes vote down vote up
/**
 * Create a new instance of the BaleenFileLoggerBuilder
 *
 * @param name The name of the logger
 * @param pattern The logging pattern, even though it isn't set explicitly by this builder it is
 *     passed to the super() which might set it
 * @param file The file to write to (absolute or relative path)
 * @param filters A list of filters to apply to logging events
 * @param dailyLogFiles Should the log files be rotated each day?
 * @param maxSize What is the max size of file (in kb) before rotating? Defaults to no maximum
 * @param maxNumberLogs The max number of log files to keep. Defaults to 1
 */
public BaleenFileLoggerBuilder(
    String name,
    String pattern,
    String file,
    List<Filter<ILoggingEvent>> filters,
    boolean dailyLogFiles,
    Optional<Integer> maxSize,
    Optional<Integer> maxNumberLogs) {
  super(name, pattern, filters);
  this.file = file;
  this.dailyLogFiles = dailyLogFiles;
  this.maxSize = maxSize;
  this.maxNumberLogs = maxNumberLogs;
}
 
Example #9
Source File: BaleenFileLoggerBuilder.java    From baleen with Apache License 2.0 5 votes vote down vote up
/**
 * Create a new instance of the BaleenFileLoggerBuilder
 *
 * @param name The name of the logger
 * @param pattern The logging pattern, even though it isn't set explicitly by this builder it is
 *     passed to the super() which might set it
 * @param file The file to write to (absolute or relative path)
 * @param filter A filter, for example a MinMaxFilter, to apply to logging events
 * @param dailyLogFiles Should the log files be rotated each day?
 * @param maxSize What is the max size of file (in MB) before rotating? Defaults to no maximum
 * @param maxNumberLogs The max number of log files to keep. Defaults to 1
 */
public BaleenFileLoggerBuilder(
    String name,
    String pattern,
    String file,
    Filter<ILoggingEvent> filter,
    boolean dailyLogFiles,
    Optional<Integer> maxSize,
    Optional<Integer> maxNumberLogs) {
  super(name, pattern, filter);
  this.file = file;
  this.dailyLogFiles = dailyLogFiles;
  this.maxSize = maxSize;
  this.maxNumberLogs = maxNumberLogs;
}
 
Example #10
Source File: BaleenLogging.java    From baleen with Apache License 2.0 5 votes vote down vote up
private BaleenLoggerBuilder configureLogger(Map<String, Object> config)
    throws InvalidParameterException {
  // Extract the specified configuration parameters
  String name = (String) config.get("name");
  String pattern = (String) config.getOrDefault("pattern", DEFAULT_PATTERN);

  String file = (String) config.get("file");
  Boolean rolling = (Boolean) config.getOrDefault("daily", false);

  Double maxSize = parseToDouble(config.get("size"));
  Integer maxHistory = (Integer) config.get("history");

  List<Filter<ILoggingEvent>> filters = configureFilters(config);

  // Do we have a name, and if so is it a special case (e.g. console)
  if (Strings.isNullOrEmpty(name)) {
    LOGGER.warn("Required parameter 'name' not specified for logger - logger will be skipped");
    return null;
  } else if ("console".equalsIgnoreCase(name)) {
    return new BaleenConsoleLoggerBuilder(name, pattern, filters);
  } else {
    if (Strings.isNullOrEmpty(file)) {
      file = name;
    }

    Optional<Integer> integerMaxSize = Optional.empty();
    if (maxSize != null) {
      integerMaxSize = Optional.of((int) (maxSize * 1024));
    }
    return new BaleenFileLoggerBuilder(
        name, pattern, file, filters, rolling, integerMaxSize, Optional.ofNullable(maxHistory));
  }
}
 
Example #11
Source File: BaleenLogging.java    From baleen with Apache License 2.0 5 votes vote down vote up
private List<Filter<ILoggingEvent>> configureFilters(Map<String, Object> config)
    throws InvalidParameterException {
  List<Filter<ILoggingEvent>> filters = new ArrayList<>();

  List<String> includeLoggers = yamlToList(config.get("includeLoggers"));
  List<String> excludeLoggers = yamlToList(config.get("excludeLoggers"));

  if (!includeLoggers.isEmpty()) {
    LoggerFilter includeFilter = new LoggerFilter(includeLoggers, false);
    filters.add(includeFilter);
  }

  if (!excludeLoggers.isEmpty()) {
    LoggerFilter excludeFilter = new LoggerFilter(excludeLoggers, true);
    filters.add(excludeFilter);
  }

  String minLevelStr = (String) config.get("minLevel");
  String maxLevelStr = (String) config.get("maxLevel");

  Level minLevel = convertToLevel(minLevelStr);
  Level maxLevel = convertToLevel(maxLevelStr);

  MinMaxFilter levelFilter = new MinMaxFilter(minLevel, maxLevel);
  filters.add(levelFilter);

  return filters;
}
 
Example #12
Source File: CompositeFilterTest.java    From qpid-broker-j with Apache License 2.0 5 votes vote down vote up
private LogBackLogInclusionRule createRule(final FilterReply decision, String name)
{
    LogBackLogInclusionRule rule = mock(LogBackLogInclusionRule.class);
    when(rule.getName()).thenReturn(name);
    Filter filter = mock(Filter.class);
    when(filter.getName()).thenReturn(name);
    when(filter.decide(any(ILoggingEvent.class))).thenReturn(decision);
    when(rule.asFilter()).thenReturn(filter);
    return rule;
}
 
Example #13
Source File: VirtualHostNameAndLevelLogInclusionRuleImplTest.java    From qpid-broker-j with Apache License 2.0 5 votes vote down vote up
@Test
public void testAsFilter()
{
    VirtualHostNameAndLevelLogInclusionRule<?> rule = createRule("org.apache.qpid", LogLevel.INFO);

    Filter<ILoggingEvent> filter = rule.asFilter();

    final boolean condition = filter instanceof LoggerNameAndLevelFilter;
    assertTrue("Unexpected filter instance", condition);

    LoggerNameAndLevelFilter f = (LoggerNameAndLevelFilter)filter;
    assertEquals("Unexpected log level", Level.INFO, f.getLevel());
    assertEquals("Unexpected logger name", "org.apache.qpid", f.getLoggerName());
}
 
Example #14
Source File: BrokerNameAndLevelLogInclusionRuleTest.java    From qpid-broker-j with Apache License 2.0 5 votes vote down vote up
@Test
public void testAsFilter()
{
    BrokerNameAndLevelLogInclusionRule<?> rule = createRule("org.apache.qpid", LogLevel.INFO);

    Filter<ILoggingEvent> filter = rule.asFilter();

    final boolean condition = filter instanceof LoggerNameAndLevelFilter;
    assertTrue("Unexpected filter instance", condition);

    LoggerNameAndLevelFilter f = (LoggerNameAndLevelFilter)filter;
    assertEquals("Unexpected log level", Level.INFO, f.getLevel());
    assertEquals("Unexpected logger name", "org.apache.qpid", f.getLoggerName());
}
 
Example #15
Source File: XodusEnvironmentImplLogLevelModificatorTest.java    From hivemq-community-edition with Apache License 2.0 5 votes vote down vote up
@NotNull
private Filter<ILoggingEvent> createFilter(final CountDownLatch countDownLatch, final String text) {
    return new Filter<ILoggingEvent>() {
        @Override
        public FilterReply decide(final ILoggingEvent event) {
            if (event.getLevel().equals(Level.TRACE)) {
                if (event.getFormattedMessage().equals(text)) {
                    countDownLatch.countDown();
                    return FilterReply.NEUTRAL;
                }
            }
            return FilterReply.NEUTRAL;
        }
    };
}
 
Example #16
Source File: CompositeFilter.java    From qpid-broker-j with Apache License 2.0 5 votes vote down vote up
@Override
public FilterReply decide(ILoggingEvent event)
{
    FilterReply reply = DENY;
    for(Filter<ILoggingEvent> filter : _filterList)
    {
        FilterReply filterReply = filter.decide(event);
        if (filterReply == DENY)
        {
            reply = filterReply;
            break;
        }
        if (filterReply == ACCEPT)
        {
            reply = filterReply;
        }
    }
    if(reply == ACCEPT)
    {
        switch(event.getLevel().toInt())
        {
            case WARN_INT:
                _warnCount.incrementAndGet();
                break;
            case ERROR_INT:
                _errorCount.incrementAndGet();
                break;
            default:
                // do nothing
        }
        return ACCEPT;
    }
    return DENY;
}
 
Example #17
Source File: CompositeFilter.java    From qpid-broker-j with Apache License 2.0 5 votes vote down vote up
public void removeLogInclusionRule(LogBackLogInclusionRule logInclusionRule)
{
    Iterator<Filter<ILoggingEvent>> it = _filterList.iterator();
    while(it.hasNext())
    {
        Filter f = it.next();
        if (f.getName().equals(logInclusionRule.getName()))
        {
            _filterList.remove(f);
            break;
        }
    }
}
 
Example #18
Source File: XodusFileDataWriterLogLevelModificatorTest.java    From hivemq-community-edition with Apache License 2.0 5 votes vote down vote up
@NotNull
private Filter<ILoggingEvent> createFilter(final CountDownLatch countDownLatch, final String text) {
    return new Filter<ILoggingEvent>() {
        @Override
        public FilterReply decide(final ILoggingEvent event) {
            if (event.getLevel().equals(Level.DEBUG)) {
                if (event.getFormattedMessage().equals(text)) {
                    countDownLatch.countDown();
                    return FilterReply.NEUTRAL;
                }
            }
            return FilterReply.NEUTRAL;
        }
    };
}
 
Example #19
Source File: LambdaConsoleAppender.java    From lambda-monitoring with Apache License 2.0 5 votes vote down vote up
private void startAndAddFilter(Filter<ILoggingEvent> filter) {
    if (filter != null) {
        if (!filter.isStarted()) {
            filter.start();
        }
        addFilter(filter);
    }
}
 
Example #20
Source File: PredicateAndLoggerNameAndLevelFilter.java    From qpid-broker-j with Apache License 2.0 5 votes vote down vote up
@Override
protected Filter<ILoggingEvent> createFilter(final String loggerName)
{
    final Filter<ILoggingEvent> filter = super.createFilter(loggerName);
    return new Filter<ILoggingEvent>()
    {
        @Override
        public FilterReply decide(final ILoggingEvent event)
        {
            final FilterReply result = filter.decide(event);
            if (result == FilterReply.ACCEPT)
            {
                if (_predicate.evaluate(event))
                {
                    return FilterReply.ACCEPT;
                }
                else
                {
                    return FilterReply.NEUTRAL;
                }
            }
            else
            {
                return result;
            }
        }
    };
}
 
Example #21
Source File: PrincipalLogEventFilter.java    From qpid-broker-j with Apache License 2.0 4 votes vote down vote up
@Override
public Filter<ILoggingEvent> asFilter()
{
    return this;
}
 
Example #22
Source File: TestLogAppender.java    From specification-arg-resolver with Apache License 2.0 4 votes vote down vote up
@Override
public List<Filter<E>> getCopyOfAttachedFiltersList() {
	return Collections.emptyList();
}
 
Example #23
Source File: TestLogAppender.java    From specification-arg-resolver with Apache License 2.0 4 votes vote down vote up
@Override
public void addFilter(Filter<E> newFilter) {
}
 
Example #24
Source File: LoggerThresholdFilterTest.java    From logging-java with Apache License 2.0 4 votes vote down vote up
@Test
public void filterSpotifyAtInfoOthersAtWarn() {
    List<Filter<ILoggingEvent>> filters = new ArrayList<Filter<ILoggingEvent>>();

    LoggerThresholdFilter filter = new LoggerThresholdFilter();
    filter.setLogger(spotifyLog);
    filter.setLevel(INFO);
    filter.start();
    filters.add(filter);

    filter = new LoggerThresholdFilter();
    filter.setExceptLogger(spotifyLog);
    filter.setLevel(WARN);
    filter.start();
    filters.add(filter);

    for(Level level : allLevels) {
        final LoggingEvent evt = new LoggingEvent();
        evt.setLevel(level);

        for(String logger : variousLoggers) {
            evt.setLoggerName(logger);

            FilterReply expected;
            FilterReply actual = FilterReply.NEUTRAL;

            if(spotifyLoggers.contains(logger)) {
                if(level.isGreaterOrEqual(INFO))
                    expected = FilterReply.NEUTRAL;
                else
                    expected = FilterReply.DENY;
            } else {
                if(level.isGreaterOrEqual(WARN))
                    expected = FilterReply.NEUTRAL;
                else
                    expected = FilterReply.DENY;
            }

            for(Filter<ILoggingEvent> logFilter : filters) {
                FilterReply nextReply = logFilter.decide(evt);
                actual = andFilterReplies(actual, nextReply);
            }

            assertEquals(String.format("Logger: %s, Level: %s", logger, level.toString()), expected, actual);
        }
    }
}
 
Example #25
Source File: LambdaConsoleAppender.java    From lambda-monitoring with Apache License 2.0 4 votes vote down vote up
public LambdaConsoleAppender(String pattern, Filter<ILoggingEvent> filter) {
    this();
    addPattern(pattern);
    startAndAddFilter(filter);
}
 
Example #26
Source File: AbstractPredicateLogInclusionRule.java    From qpid-broker-j with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unused")
public Filter<ILoggingEvent> asFilter()
{
    return _filter;
}
 
Example #27
Source File: AbstractNameAndLevelLogInclusionRule.java    From qpid-broker-j with Apache License 2.0 4 votes vote down vote up
public Filter<ILoggingEvent> asFilter()
{
    return _filter;
}
 
Example #28
Source File: MetricsLogAppender.java    From pravega with Apache License 2.0 4 votes vote down vote up
@Override
public List<Filter<ILoggingEvent>> getCopyOfAttachedFiltersList() {
    return null;
}
 
Example #29
Source File: MetricsLogAppender.java    From pravega with Apache License 2.0 4 votes vote down vote up
@Override
public void addFilter(Filter<ILoggingEvent> newFilter) {
}
 
Example #30
Source File: CompositeFilter.java    From qpid-broker-j with Apache License 2.0 4 votes vote down vote up
public void addLogInclusionRule(LogBackLogInclusionRule logInclusionRule)
{
    Filter<ILoggingEvent> f = logInclusionRule.asFilter();
    f.setName(logInclusionRule.getName());
    _filterList.add(f);
}