Debug with a Java log package

Source: Internet
Author: User
Debug with a Java log package
Builder.com
6/4/2004
URL: http://www.zdnet.com.cn/developer/code/story/0,2000081534,39225509,00.htm

Java. util. Logging is introduced in java1.4. If you have used the previous general log structure framework-log4j, you will be very familiar with the internal record of Java.

You can use a log package without installing it. The simplest method is to create a java. util. Logging. Logger instance. Once this instance is created, you can start recording it. The following is a complete example of a record class:

Package tips;

Import java. util. Logging. Logger;

Public class logtip {
// Create an instance of the logger class
Private Static logger log = logger. getlogger ("tips. logtip ");

Public static void main (string ARGs []) {
Log. Finest ("The FINEST message ");
Log. Finer ("finer message ");
Log. Fine ("a fine message ");
Log. config ("Some configuration message ");
Log.info ("a little bit of information ");
Log. Warning ("a warning message ");
Log. Severe ("A severe message ");
}
}

When you compile and run this class, the following output is displayed in your console:

Apr 1, 2003 11:09:05 tips. logtip main
Info: A little bit of information
Apr 1, 2003 11:09:05 tips. logtip main
Warning: a warning message
Apr 1, 2003 11:09:05 tips. logtip main
Severe: A severe message

Not all record items are displayed on the console, because the Java log package applies the log level. Log level allows you to control record output. In the development process, you want to get a lot of debugging information, so that you can understand what problems your application will encounter.

However, as a product, you do not want it to output debugging results without errors. You can also control it by using the Log Level in the log package.

Seven types of log levels are defined in the Java. util. Logging. level class: severe, warning, info, config, fine, finer, and finest. Specifically, severe is used to handle errors and disaster events. Fine, finer, and finest are used to process less important information and for program debugging.

In addition, you can set the log level to all or off to set its extreme value. When you set it to all, all record items are open. On the contrary, when you set it to off, all record items are forbidden.

Because the log level is hierarchical, When you define it as a level, only this level or a higher level can be recorded. Among the seven levels, severe is the highest level and finest is the lowest level. The default value is info. That is why we only see three records in the code segment above. If you want to view all the record information, we need to change the settings.

There are many ways to change the default settings, such as using a tool file, programming the running time or proposing your own structural class. If you use the tool option to set the record, you have two options. One is that you can use the default prop file (this method is not recommended) -- % your_jre_path %/lib/logging. properties; you can also set the system fileJava. util. Logging. config. FileTo specify the file name you want to use.ComeSet the prop file by yourself as follows:

Java-djava. util. Logging. config. File = log. properties tips. logtip.

In this case, the logging tool cannot be used from the log. properties file. In this example, the default logging. properties file is revised as follows:

Handlers = java. util. Logging. lelehandler
. Level = finest
Java. util. Logging. lelehandler. Level = finest
Java. util. Logging. lelehandler. formatter = java. util. Logging. simpleformatter

When this class is run, the following output is displayed:

Apr 1, 2003 11:12:00 tips. logtip main
Finest: The FINEST message
Apr 1, 2003 11:12:00 tips. logtip main
Finer: finer message
Apr 1, 2003 11:12:00 tips. logtip main
Fine: a fine message
Apr 1, 2003 11:12:00 tips. logtip main
Config: Some configuration message
Apr 1, 2003 11:12:00 tips. logtip main
Info: A little bit of information
Apr 1, 2003 11:12:00 tips. logtip main
Warning: a warning message
Apr 1, 2003 11:12:00 tips. logtip main
Severe: A severe message

In practice, you need to control as many records as possible. You should define some records as finest, and some class records as severe or other levels. You may also need to record some information about the console, files, and sometimes databases.

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.