java.util.logging JDK Log detailed

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.