Setting a Filter on a Logger Handler - Java Java Virtual Machine

Java examples for Java Virtual Machine:Logger

Description

Setting a Filter on a Logger Handler

Demo Code

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

public class Main {
  public static void main(String[] args) throws Exception {
    ConsoleHandler handler = new ConsoleHandler();

    handler.setFilter(new Filter() {
      public boolean isLoggable(LogRecord record) {
        // return true if the record should be logged;
        // false otherwise.
        return true;
      }/*w w  w .  jav a 2  s. c  om*/
    });

    Logger logger = Logger.getLogger("com.mycompany");
    logger.addHandler(handler);
  }
}

Related Tutorials