[an error occurred while processing this directive]

Setting a Filter on a Logger Handler in Java


This section illustrates you how to set filter on a logger handler in Java. Generally, Filter is a program that provides the facility to get data if the certain conditions are satisfied otherwise don't get any data. In other words, you get data to be filtered.

Logger handler provides the filtering facility that means when you set a filter on a logger object, the filter determines which data to be published or not. The filter facility deals those data that are recognized with the logger object or certain conditions are verify otherwise couldn't published those data.

Descriptions of program:

Program creates a logger object that assists for filtering the data and it has three classes as: Num, NumFilter and SetFilterLogger class. The SetFilterLogger class is a main class and other are inner classes. The Num class gets and returns given numbers and NumFilter class provides the facility for filtering data that implements a Filter. In this class, you give the certain conditions for filtering the number. This program shows result that are greater or equal to 20 with the help of logMessn() method.

Descriptions of code:

This is the interface of
java.util.logging package that assists the fragment control over logger object. This fragments provided by log levels. Each Logger object filtered associated with the filter. The log records checked by isLoggable method should be published that provides Boolean types value either true or false. If returns false then LogRecord discard.

Above method returns the parameters of LogRecord object.

This is the constructor of Object class. This class extends the import
java.lang.Object package. This class provides all arrays, objects and implementing the method.

Here is the code of program:

import java.util.logging.*;

public class SetFilterLogger{
  Logger logger;
  public static void main(String args[]){
    SetFilterLogger demo = new SetFilterLogger();
    Integer n1 = new Integer(30);
    Integer n2 = new Integer(24);
    Integer n3 = new Integer(45);
    Integer n4 = new Integer(12);
    Integer n5 = new Integer(8);
  public SetFilterLogger()  {
    logger = Logger.getLogger("RoseIndia.net");
    NumFilter filter = new NumFilter();
  public void logMessn(Integer nu){
    logger.log(Level.INFO, "Number = " + String.valueOf(nu.intValue()), nu);
class NumFilter implements Filter {
  public NumFilter() {}
    public boolean isLoggable(LogRecord record) {
      boolean result = false;
      Object[] objs = record.getParameters();
      Integer nu = (Integer)objs[0];
      if(nu !=null) {
        int n = nu.intValue();
        if(n >= 20)
          result = true;
          result = false;
      return result;

Download this example.


[an error occurred while processing this directive]