Home Java Example Java Util Setting a Filter on a Logger Handler in Java
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Setting a Filter on a Logger Handler in Java

Advertisement
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 filt

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:

Filter:
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.

getParameters():
Above method returns the parameters of LogRecord object.

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);
  demo.logMessn(n1);
  demo.logMessn(n2);
  demo.logMessn(n3);
  demo.logMessn(n4);
  demo.logMessn(n5);
  }
  public SetFilterLogger()  {
  logger = Logger.getLogger("RoseIndia.net");
  NumFilter filter = new NumFilter();
  logger.setFilter(filter);
  }
  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;
  else
  result = false;
  }
  return result;
  }
}

Download this example.

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: February 22, 2008

Related Tutorials

Ask Questions?    Discuss: Setting a Filter on a Logger Handler in Java  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com