Log4j learning notes 1-level, initialization and category

Source: Internet
Author: User

Log4j is a common log output tool in Java. The original intention of Log4j naming is what Log for Java means. Although JDK has provided a Log API since 1.4, so far, I have never seen anyone use the Log API that comes with JDK. However, using Log4j for development has become a de facto standard.

Log4j is divided into three parts: Level, Appender, and Layout ). Among them, Level is commonly used by log4j. From the perspective of Source, it is divided into eight levels. The levels from low to high are ALL, TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF. Among them, the logs output by ALL are the most detailed, indicating that ALL the logs are output, while the logs output by OFF are the least. The remaining six levels except the ALL and OFF levels are commonly used. during development, we often use the DEBUG level for debugging. During the interim test, it is okay to use INFO to view some important information. During the system test, turn the level into WARN to view some warning information or error information. In real environments. It is enough to open the log level to ERROR, because in the real environment, we are most concerned about the ERROR information. If the log level is relatively low, too many log outputs may affect the application performance.

Log4j can be initialized in two ways. The first is the Properties file initialization, and the second is the xml file initialization. When the system used the Log4j class for the first time, for example, the getLogger method of the Logger class was used. The source code of log4j of this method is as follows:

Static public Logger getLogger (String name ){
Return LogManager. getLogger (name );
}

Here we can see that the LogManager class is used, and the static initialization static statement of this class has the following section.

Url = Loader. getResource (DEFAULT_XML_CONFIGURATION_FILE );
If (url = null ){
Url = Loader. getResource (DEFAULT_CONFIGURATION_FILE );
}

Through this code, we can see that the system will first find log4j. xml in the root path of classpath. If it cannot find it, find the log4j. properties file for initialization.

In the initialization code. First, we will look for our category configuration. If the category is found, we will initialize the appender configured in the category and bind this category with the appender.

There is another question: how does the system know what the current log-level configuration is. That is actually very simple. In the logger. getlogger method, the following code is available in our org. Apache. log4j. Hierarchy class:

CategoryKey key = new CategoryKey (name );
Object o = ht. get (key );
If (o = null ){
Logger = factory. makeNewLoggerInstance (name );
Logger. setHierarchy (this );
Ht. put (key, logger );
UpdateParents (logger );
Return logger;
} Else if (o instanceof Logger ){
Return (Logger) o;
} Else if (o instanceof ProvisionNode ){
Logger = factory. makeNewLoggerInstance (name );
Logger. setHierarchy (this );
Ht. put (key, logger );
UpdateChildren (ProvisionNode) o, logger );
UpdateParents (logger );
Return logger;
} Else {
Return null; // but let's keep the compiler happy.
}

It can be seen that if the current class name is not configured in the category in the configuration file, the sub-category will be taken. If the sub-category cannot be obtained, the sub-category will be taken again, if none of them are obtained, a null value is returned. On the console, the system exception output stream information configured by logger cannot be found.

When the sub-category is obtained, the classname is substring based on the first to last decimal point, and then check whether there is a match. If not, the loop goes down, and then the beginning is truncated to the second decimal point, it cannot be found until it is found or the decimal point is truncated.

For such a simple introduction to log4j, I believe that everyone will have an understanding of the level of log4j and the order of the initialization files, I also know how log4j is output by searching for category. In the future, if I have time, I will write the rest of my thoughts on log4j, including how to customize appender and custom layout.

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.