Commissioning with Java log pack

xiaoxiao2021-03-06  47

Debug BUILDER.com with Java Log Pack

6/4/2004

URL:

Http://www.zdnet.com.cn/developer/code/story/0,2000081534,39225509,00.htm

A log package java.util.logging is introduced in Java1.4. If you have already used the previous ordinary log structure frame --log4j, then you will be very familiar with the internal record of Java.

You can use the log package without installing. The simplest method is to create an instance of java.util.logging.logger. Once this instance is established, you can start recording. Below 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 ("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 this class, you will have the following output display in your console:

Apr 1, 2003 11:09:05 PM Tips.logtip maininfo: a little bit of informationapr 1, 2003 11:09:05 PM Tips.logtip mainwarning: a Warning Messageapr 1, 2003 11:09:05 PM Tips.logtip mainsevere : a Severe Message

Not all record items will be displayed in the console, because Java's log package applied to the log level. The log level allows you to control the record output. During the development process, you want to get a lot of debugging information, so you can understand what problems will appear in your application.

But as a product, you don't want him to output debug results and do not have an error. You can also control it by using the log package in the log package.

In Java.util.logging.Lavel, you define 7 log levels, they are: Severe, Warning, Info, Config, Fine, Finer, and Finest. SEVERE is used to handle errors and disaster events. The Fine, Finer and Finest are used to handle some less important information, as well as program debugging.

Alternatively, 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. In contrast, all record items are forbidden when you are set to OFF.

Due to the log level, you can record it if you define it as a certain level, only this level or higher level can be recorded. In seven levels, Severe is the highest level, and Finest is the lowest level. The default level is info; this is why we only see three record information in the above code segment. If you want to see all the record information, we will change the settings. There are many ways to change the default settings, such as using tool files, programming runtime or present your own structural classes. If you set the record through the tool option, you have two options. First, you can use the default props file (this method is not recommended) -% your_jre_path% / lib / logging.properties; and you can pass Set the system file java.util.logging.config.File, specify it to the file name you need to use to set your own item file, the method is as follows:

Java -djava.util.logging.config.file = log.properties tips.logtip.

This cannot be recorded from the log.properties file, in this example, this is the revision of the default logging.properties file, as follows:

Handlers = java.util.logging.consolehandler .level = finest java.util.logging.consolehandler.Logging.ConsoleHandler.Logging.consoleHandler.MMATTER = java.util.logging.simpleFormatter

When running this class, there will be the following output display:

Apr 1, 2003 11:12:00 PM Tips.Logtip Mainfinest: The Finest MessageApr 1, 2003 11:12:00 PM Tips.logtip Mainfiner: Finer MessageApr 1, 2003 11:12:00 PM Tips.logtip Mainfine: a Fine Messageapr 1, 2003 11:12:00 PM Tips.Logtip Mainconfig: Some Configuration Messageapr 1, 2003 11:12:00 PM Tips.logtip MainInfo: a little bit of informationapr 1, 2003 11:12:00 PM Tips.logtip mainwarning : a Warning MessageApr 1, 2003 11:12:00 PM Tips.logtip Mainsevere: a Severe Message

In practical applications, you need to control as much as possible. You have to define some records as a Finest, and some class records are Severe or other levels. You may also need to record some information about the console, or some information about the file, and sometimes information about the database.

转载请注明原文地址:https://www.9cbs.com/read-116407.html

New Post(0)