Detailed description of the built-in logging tool in jdk1.4

Source: Internet
Author: User

Sun developed a logger in jdk1.4, and related classes are organized in the Java. util. Logging package. The most important is the logger class. Its instances can help us record log information.

We often use the following two steps to record log information:

Logger log = logger. getlogger ("log name ");

Log.info ("information to be recorded ");

So, what is behind this simple two-step operation?

Let's take a look at the implementation of the logger. getlogger (...) method:

Public static synchronized logger getlogger (string name ){
Logmanager manager = logmanager. getlogmanager ();
Logger result = manager. getlogger (name );
If (result = NULL ){
Result = new logger (name, null );
Manager. addlogger (result );
Result = manager. getlogger (name );
}
Return result;
}

We can find that it first calls the static method getlogmanager () of the logmanager class (). The logmanager class represents a log manager. All the logstores are maintained and managed in a tree structure. Getlogmanager () is used to read the jre_home/lib/logging. properties log configuration file into the memory and return a static field defined in the logmanager class named "manager". The type is logmanager. This manager field is initialized when the JVM loads the logmanager class, and the root node (rootlogger) of the tree is also created and put into the manager at this time.
Detailed description of the built-in logging tool in jdk1.4
The manager. getlogger (name) method is to find the logger corresponding to this name from the log tree maintained and managed by the manager. Then determine whether the result exists. If it does not exist, a new logstore is created and added to the logstore tree. How to determine its parent node? According to this name. It should be written as parent. Child. When you write only child, it will be added to the root node.

In general, the role of logger. getlogger (name) is:

Load the configuration file

Get global log Manager

Search for the logstore corresponding to name from the logstore tree maintained by the log manager, and create and add it to the logstore tree if it does not exist.

Let's talk about how logger records information. Logger defines Info (), debug (), and other methods to record different levels of information. The procedure is similar. Info (string MSG) is used below) for example, it is important to remember that information is recorded from the sub-logstore to the parent logstore.

First, construct a logrecord instance. In addition to saving MSG, this instance also accesses the stack to obtain the method name and class that calls the info () method. Therefore, the log record format is: method name MSG of the full name of the class where the date and time are located

Then, the handler of the logstore is obtained to record information externally. You can use log. sethandler (the actual implementation class instance of handler) to set handler for it. Hander is an abstract class. It is mainly used to record information through its internal publish () method. Apart from the lelehandler and filehandler prepared by Sun for us, we can also customize a handler.

Finally, process its parent logstore until it reaches the root node.

* That is why MSG is also recorded when we do not set hadler for the current log because the root node processes This msg, the root node Handler comes from the configuration file jre_home/lib/logging. properties.

In addition, the logstore provides a filter function.

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.