Log4j learning Summary

Source: Internet
Author: User
Log4j supports two configuration file formats: XML and Java. log4j. properties (Key = value ).
Log4j is an open source project and is a widely used logging package written in Java. Due to the outstanding performance of log4j, at the time when log4j was completed, the log4j development organization suggested that sun replace jdk1.4's log tool class with log4j in jdk1.4, but jdk1.4 was nearing completion, therefore, sun refused to use log4j. The most actually used log4j in Java Development was log4j, and Sun's log tool class was forgotten. One of its unique features includes the concept inherited in categories. By using the category hierarchy, this reduces the log record output and minimizes the overhead of log records.
The introduction of log4j is no longer mentioned. There are a lot of related information on the Internet. Currently, our South China R & D center is also using log4j. However, we should not use log4j for the projects I participated in, using log4j can bring us convenience and improve efficiency. The following describes how to use log4j quickly and reasonably. (Please correct me if something is wrong)
Log4j has three main components: logger, appender, and layout. log4j allows developers to define multiple logger, and each logger has its own name, logger names are used to indicate the affiliation. There is a logger called root, which always exists and cannot be retrieved or referenced by name. You can use logger. the getrootlogger () method is used. getlogger (string name) or logger. getlogger (class clazz) method.
Appender indicates the location where all log information is stored. log4j supports multiple appender types, such as console, files, Gui components, and NT Event loggers, a logger can have multiple appender, that is, you can output log information to the console screen and store it in a file.
Layout controls the output mode of log information, that is, formatting the output information.
Log4j defines five levels of log information to be output. The levels are debug, info, warn, error, and fatal from low to high, information can be output only when the level is higher than or equal to the level specified in the configuration. If the configuration is off, no information is output. If the loggers level is P, if a level of Q is higher than P in loggers, it can be started; otherwise, it will be blocked. If the defined level is info, the logs of error and warn can be displayed, but the debug information lower than him will not be displayed. In this way, you can easily configure the content to be output under different circumstances without changing the content. Code . (This is really convenient. We do not need to use system. Out to remove a large number of system. Out values when using the project)
Log4j has two configuration methods. We are familiar with the properties file. But in the recent project implementation, every time I install the system on the user, I am worried that the log file should be placed in different locations, to change the property file, you need to re-create the jar package, which is troublesome. If you use the xml configuration method, you can directly put it under Web-info. It is very convenient to modify the path. After checking some information, I finally changed the system's log4j to configuration in XML.
Step 1: Add log4j-1.2.8.jar to Lib. Step 2: Create log4j. properties under classpath. Step 3: Use

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.