The JDK's own log, the structure is not complex, function can also meet most functions. The log write location is open and can receive log writes as long as the handler is inherited. The handler itself relies on the LogRecord object, which represents a log. Handler calls the logger bound Formater after receiving the log object to format the log in the specified format.
The formatted message is then written to handler in turn. There is a leval in the field of the log object, an enumeration that contains an int type value that can be used to represent the level of the log. Logger also binds a level, and the log is not rejected unless the level of logger is higher than the level of the Log object. External through a Loggermanager management log. Let's look at this complex class structure.
The demo is as follows:
Public Static voidMain (string[] args)throwsIOException, dataformatexception {Logger Logger= Logmanager.getlogmanager (). GetLogger ("global"); Logger.info ("Test"); Logger.log (Level.info,"Info"); Handler Handler=NewHandler () {@Override Public voidPublish (LogRecord record) {System.out.println (Record.getmessage ()); } @Override Public voidflush () {} @Override Public voidClose ()throwsSecurityException {}}; Handler.setlevel (Level.info); Logger.addhandler (handler); Logger.config ("I write Failed"); Logger.info ("I wrote it successfully."); Filter Filter=NewFilter () { Public Booleanisloggable (LogRecord record) {return!record.getmessage (). Contains ("uncivilized"); } }; Logger.setfilter (filter); Logger.info ("Uncivilized language cannot be written"); Logger.info ("Civilized language can be written"); }
Operation Result:
August 12, 2017 1:18:27 am Demo.nio.CrcDemo Main
Info:test
August 12, 2017 1:18:27 am Demo.nio.CrcDemo Main
Info:info
August 12, 2017 1:18:27 am Demo.nio.CrcDemo Main
INFO: I succeeded in writing
August 12, 2017 1:18:27 am Demo.nio.CrcDemo Main
INFO: Civilized language can be written
I wrote it.
Civilized language can be written
java.util.logging JDK Log detailed