Logger: setFilter(Filter newFilter) : Logger « java.util.logging « Java by API






Logger: setFilter(Filter newFilter)

  



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

public class Main {
  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();
  }
}

   
    
  








Related examples in the same category

1.Logger: addHandler(Handler handler)
2.Logger: config(String msg)
3.Logger: exiting(String sourceClass, String sourceMethod, Object result)
4.Logger: fine(String msg)
5.Logger: finer(String msg)
6.Logger: finest(String msg)
7.Logger: getAnonymousLogger()
8.Logger: getLogger(String name)
9.Logger: info(String msg)
10.Logger: isLoggable(Level level)
11.Logger: logp(Level level, String sourceClass, String sourceMethod, String msg)
12.Logger: severe(String msg)
13.Logger: setUseParentHandlers(boolean useParentHandlers)
14.Logger: warning(String msg)