2015-05-05 11:20:13 -07:00
|
|
|
//: logging/SimpleFilter.java
|
|
|
|
import java.util.logging.*;
|
|
|
|
|
|
|
|
public class SimpleFilter {
|
|
|
|
private static Logger logger =
|
|
|
|
Logger.getLogger("SimpleFilter");
|
|
|
|
static class Duck {};
|
|
|
|
static class Wombat {};
|
|
|
|
static void sendLogMessages() {
|
|
|
|
logger.log(Level.WARNING,
|
|
|
|
"A duck in the house!", new Duck());
|
|
|
|
logger.log(Level.WARNING,
|
|
|
|
"A Wombat at large!", new Wombat());
|
|
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
|
|
sendLogMessages();
|
|
|
|
logger.setFilter(new Filter() {
|
2015-05-18 23:05:20 -07:00
|
|
|
public boolean
|
|
|
|
isLoggable(LogRecord record) {
|
|
|
|
Object[] params =
|
|
|
|
record.getParameters();
|
2015-05-05 11:20:13 -07:00
|
|
|
if(params == null)
|
|
|
|
return true; // No parameters
|
2015-05-18 23:05:20 -07:00
|
|
|
if(record.getParameters()[0]
|
|
|
|
instanceof Duck)
|
2015-05-05 11:20:13 -07:00
|
|
|
return true; // Only log Ducks
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
logger.info("After setting filter..");
|
|
|
|
sendLogMessages();
|
|
|
|
}
|
2015-05-18 23:05:20 -07:00
|
|
|
} /* Output:
|
|
|
|
May 14, 2015 3:29:53 PM SimpleFilter sendLogMessages
|
|
|
|
WARNING: A duck in the house!
|
|
|
|
May 14, 2015 3:29:54 PM SimpleFilter sendLogMessages
|
|
|
|
WARNING: A Wombat at large!
|
|
|
|
May 14, 2015 3:29:54 PM SimpleFilter main
|
|
|
|
INFO: After setting filter..
|
|
|
|
May 14, 2015 3:29:54 PM SimpleFilter sendLogMessages
|
|
|
|
WARNING: A duck in the house!
|
|
|
|
*///:~
|