OnJava8-Examples/logging/CustomHandler.java

30 lines
872 B
Java
Raw Normal View History

2015-05-05 11:20:13 -07:00
//: logging/CustomHandler.java
// How to write custom handler
import java.util.logging.*;
import java.util.*;
public class CustomHandler {
private static Logger logger =
Logger.getLogger("CustomHandler");
2015-05-06 15:14:33 -07:00
private static List<String> trace = new ArrayList<>();
2015-05-05 11:20:13 -07:00
public static void main(String[] args) {
logger.addHandler(new Handler() {
@Override
public void publish(LogRecord logRecord) {
2015-05-06 15:14:33 -07:00
trace.add(logRecord.getLevel() + ":");
trace.add(logRecord.getSourceClassName()+":");
trace.add(logRecord.getSourceMethodName()+":");
trace.add("<" + logRecord.getMessage() + ">");
trace.add("\n");
2015-05-05 11:20:13 -07:00
}
@Override
public void flush() {}
@Override
public void close() {}
});
logger.warning("Logging Warning");
logger.info("Logging Info");
2015-05-06 15:14:33 -07:00
System.out.print(trace);
2015-05-05 11:20:13 -07:00
}
} ///:~