OnJava8-Examples/logging/CustomHandler.java

45 lines
1.2 KiB
Java
Raw Normal View History

2015-09-07 11:44:36 -06:00
// logging/CustomHandler.java
2015-11-14 16:18:05 -08:00
// <20>2016 MindView LLC: see Copyright.txt
2015-06-15 17:47:35 -07:00
// How to write custom handler
import java.util.logging.*;
import java.util.*;
public class CustomHandler {
private static Logger logger =
Logger.getLogger("CustomHandler");
private static List<String> trace =
new ArrayList<>();
public static void main(String[] args) {
logger.addHandler(new Handler() {
@Override
public void publish(LogRecord logRecord) {
trace.add(logRecord.getLevel() + ":");
trace.add(logRecord.getSourceClassName()
+ ":");
trace.add(
logRecord.getSourceMethodName() +":");
trace.add("<" + logRecord.getMessage()
+ ">");
trace.add("\n");
}
@Override
public void flush() {}
@Override
public void close() {}
});
logger.warning("Logging Warning");
logger.info("Logging Info");
System.out.print(trace);
}
2015-09-07 11:44:36 -06:00
}
/* Output:
2015-06-15 17:47:35 -07:00
[WARNING:, CustomHandler:, main:, <Logging Warning>,
, INFO:, CustomHandler:, main:, <Logging Info>,
]
___[ Error Output ]___
Jun 15, 2015 3:47:52 PM CustomHandler main
WARNING: Logging Warning
Jun 15, 2015 3:47:52 PM CustomHandler main
INFO: Logging Info
2015-09-07 11:44:36 -06:00
*/