+

Search Tips   |   Advanced Search

Example: Creating custom filters with java.util.logging


A custom filter provides optional, secondary control over what is logged, beyond the control that is provided by the level.

The mechanism for creating a customer filter is the Filter interface support that is provided by the IBM Developer Kit, Java Technology Edition. If not familiar with filters, as implemented by the Developer Kit, we can get more information from various texts, or by reading the API documentation the for the java.util.logging API.

The following example shows a custom filter:

/**
 * This class filters out all log messages starting with SECJ022E, SECJ0373E, or SECJ0350E.
 */ import java.util.logging.Filter;
 import java.util.logging.Handler;
 import java.util.logging.Logger;
 import java.util.logging.LogRecord;
 public class MyFilter implements Filter {
  public boolean isLoggable(LogRecord lr) {
    String msg = lr.getMessage();
    if (msg.startsWith("SECJ0222E") || msg.startsWith("SECJ0373E") || msg.startsWith("SECJ0350E")) {
      return false;
    }
    return true;
  }
}


//This code will register the above log filter with the root Logger's handlers (including the WAS system logs):
... Logger rootLogger = Logger.getLogger(""); rootLogger.setFilter(new MyFilter());





 

Related tasks


Set the logger hierarchy
Create log resource bundles and message files
Use a logger

 

Related


Example: Creating custom log handlers with java.util.logging
Example: Creating custom formatters with java.util.logging
Example: Adding custom handlers, filters, and formatters