Tutorial/system programming process logging in Java

Source: Internet
Author: User

JDK1.4 provides a logging package: java. util. logging, which can be used to control the log records in the program. For example, you can specify the Log Level and log location (console, file, socket, memory buffer), or create a sub-recorder, you can use the program control method to specify the content you want to record, or you can use the configuration file to specify the content without modifying the program. Compared with Log4j, Log4j is simpler and lighter. It is undoubtedly a better choice when the requirements for log output are not very complex.

First, we obtain an instance of the LogManager class:

LogManager lMgr = LogManager. getLogManager ();

Then we create a recorder and add it to the current Manager:

String thisName = "Logpkg ";

Logger log = Logger. getLogger (thisName );

LMgr. addLogger (log );

If the location of the log file is not specified, the log information is displayed on the console according to the content specified in the logging. properties file under the jre/lib directory. The default value is lelehandler.

In the program, we can publish log information by level. There are 7 levels: SERVER (maximum value), WARNING, INFO, CONFIG, FINE, FINER, FINEST (minimum value ), and OFF (not recorded ).

Log. server ("error ");

We can set the record level of the recorder to ignore messages lower than the WARNING level. Only information recorded by the server and warning can be output.

Log. setLevel (Level. WARNING );

Log.info ("This message is info"); // This information will be ignored and not output

Log. warning ("This message is warning"); // This information will be output

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.