Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training

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 filt

Ads

Tutorials   
How to read properties file in Java? Generate random numbers in Java Cloneable Interface in Java What is the flaw with the Stack class? List In Java Java Stack Example HashSet In Java Enumeration in java How to generate random number in java Iterator in java Queue in java Transient Java Keyword clone method in Java Java Queue example Logger in Java Java current date - How to get current date in Java Collection : LinkedList Example Collection : ArrayList Example Random alpha numeric string of fixed size Java List Iterator Example The Hashtable Class The BitSet class Java Vector Java Enumeration Data Structures in Java Java Transient Variable Java Date Format Example Java Properties File Example Map.Entry Java Interface Java Property File java preferences,Change the value of Preferences by user Retrieving a Preference Node Java hasNext Write Keys and Values to the Properties file in Java Get All Keys and Values of the Properties files in Java Properties file in Java Properties file in Java Determining If a Preference Node Contains a Specific Value Change the value of Preferences by user Clone method example in Java Implementing a Simple Event Notifier Determining When a Preference Node Is Added or Removed Listening for Changes to Preference Values in a Preference Node Importing Preferences Finding a Preference in a Preference Tree Retrieving the Parent and Child Nodes of a Preference Node Determining If a Preference Node Exists Removing a Preference Node Creating a Preference Node Getting the Maximum Size of a Preference Key and Value
Ads

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

Share on Google+Share on Google+

Setting a Filter on a Logger Handler in Java

Posted on: February 22, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: Setting a Filter on a Logger Handler in Java  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads