+

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 you are not familiar with filters, as implemented by the Developer Kit, you 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:

import java.util.Vector; import java.util.logging.Filter; import java.util.logging.LogRecord;

/**
 * MyCustomFilter rejects any log records whose Level is not contained in the
 * configured list of Levels. 
 */ public class MyCustomFilter implements Filter {

  private Vector acceptableLevels;

  public MyCustomFilter(Vector acceptableLevels) {
    super();
    this.acceptableLevels = acceptableLevels;
  }

  /* (non-API documentation)
   * @see java.util.logging.Filter#isLoggable(java.util.logging.LogRecord)
   */
  public boolean isLoggable(LogRecord record) {     
    return (acceptableLevels.contains(record.getLevel()));
  }

}




 

Related tasks


Configure the logger hierarchy
Creating log resource bundles and message files
Use a logger

 

Related Reference


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

 

Reference topic