23.06.2008

Logging Filter in Java

The aim of the project is to achieve it's job, secondly the features of the standards come important for example compability,extansibilty,successfull logging ..etc. These features can also decrease performance. Doing logging without thinking increases I/O of the computer and this come with late response. In this article i show how to add Filter to logging in java

*/
public class PersonFilter implements Filter
{

public PersonFilter(){}


public boolean isLoggable(LogRecord record)
{
boolean result = false;

Object[] obj = record.getParameters();
Person p = (Person)obj[0];
if(p != null && p.getAge() > 20)
result = true;


return result;

}
}


public class PersonLogger
{
private static Logger logger = Logger.getLogger("montoya.logging");
private ConsoleHandler conHandler = null;
private PersonFilter pFilter = new PersonFilter();

public PersonLogger()
{
conHandler = new ConsoleHandler();
logger.addHandler(conHandler);
logger.setFilter(pFilter);
}

public void Log(Person p)
{
logger.log(Level.INFO,"Person age is : " + p.getAge(),p);
}
}

*/
public class Main
{

public static void main(String[] args)
{


PersonLogger logger = new PersonLogger();

Person p = new Person();
p.setAge(35);
logger.Log(p);


Person p2 = new Person();
p2.setAge(19);
logger.Log(p2);


}

}

Hiç yorum yok: