Great Deals

Implement your own logging framework

17:34 suresh chaudhari 0 Comments


How to implement your own logging operation
import java.lang.ref.WeakReference;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.ReflectPermission;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Supplier;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogManager;

import javax.swing.JApplet;
//import sun.reflect.CallerSensitive;
//import sun.reflect.Reflection;


class Driver
{
 
String name;

public String getName() {
 return name;
}

public void setName(String name) {
 this.name = name;
}

}
class Car
{
 
String name;

public String getName() {
 return name;
}

public void setName(String name) {
 this.name = name;
}

}
public class MyLogger {//extends java.util.logging.Logger {
/*
 protected MyLogger(String name, String resourceBundleName) {
  super(name, resourceBundleName);
  // TODO Auto-generated constructor stub
 }
 
  /*******Function print array basically gets the name of methods present in the class using 
Element.getClass().getMethods().
After getting name of it uses method.invoke function to invoke the getter methods.
In this way we access getter methods.
    
 
  // generic method printArray                         
 public  < E > void printArray( List inputArray )
 {
  // Display array elements   

  for ( E element : inputArray ){    
    Method[] methods= element.getClass().getMethods();
    for(Method method:methods)
    {String name=method.getName();
     if (name.startsWith("get") && !name.equalsIgnoreCase("getClass")) {
       try {

     try {
      
      System.out.println("%%"+method.invoke(element,null) );
     } catch (IllegalAccessException | InvocationTargetException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }
        } catch (IllegalArgumentException e) {
     // TODO Auto-generated catch block
         e.printStackTrace();
        }/* catch (IllegalAccessException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }*/
     }
    }
  }

   // System.out.println();
   }
    public static void main(String[] args) {
     MyLogger myObj=new MyLogger();
     String s[]=new String[]{"Suresh","Chaudhari"};
     List driverList=new ArrayList();
     Driver drr=new Driver();
     drr.setName("YUI");
     Driver dr2=new Driver();
     dr2.setName("raman");
     driverList.add(drr);
     driverList.add(dr2);
     
     List carList=new ArrayList();
     Car cr1=new Car();
     cr1.setName("D");
     Car cr2=new Car();
     cr2.setName("R");
     carList.add(cr1);
     carList.add(cr2);
     
     //myObj.printArray(s);
     myObj.printArray(driverList);
     myObj.printArray(carList);
 }

}

0 comments:

Advertising