Simple filter : Log Filter « Log « Java Tutorial






import java.util.logging.Filter;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;

public class MainClass {
  private static Logger logger = Logger.getLogger("SimpleFilter");

  static class A {
  };

  static class B {
  };

  static void sendLogMessages() {
    logger.log(Level.WARNING, "message 1!", new A());
    logger.log(Level.WARNING, "message 2!", new B());
  }

  public static void main(String[] args) {
    sendLogMessages();
    logger.setFilter(new Filter() {
      public boolean isLoggable(LogRecord record) {
        Object[] params = record.getParameters();
        if (params == null)
          return true;
        if (record.getParameters()[0] instanceof A)
          return true;
        return false;
      }
    });
    logger.info("After setting filter..");
    sendLogMessages();
  }
}








35.3.Log Filter
35.3.1.Simple filter
35.3.2.Setting a Filter on a Logger Handler
35.3.3.Custom filter