First, the preface
Java has too many open source projects, this situation is absolutely in the five dynasties period of China's "Hundred schools", "blooming" prosperity. With Java technology, our project has little need to purchase the Java support development products, just put an open source product through the architect's hand, reasonable collocation and stitching, can make flexible, good performance of commercial products.
Undoubtedly, as a popular logging tool, log4j is one of the most brilliant aspects of Java open source projects. The author sees the Java project, nine to ten all adopt the log4j, investigate its reason, I think has the following points:
A log4j is supported by most Web application servers: As far as I know, Tomcat,weblogic,websphere,jboss supports log4j.
b Fast, Powerful: log4j configuration file to achieve the output to the console, files, roll back files, send log mail, output to the database log table, custom tags and so on a full set of functions. In the speed, from the beginning of the log4j, pay attention to the speed of operation has been put in the first place, and perseverance to improve and improve.
c) Simple and convenient to use: simply import a simple log4j-1.2.x.jar, and then write the following sentence at the beginning of the program class, private final static Logger log =logger.getlogger ( Classname.class);
This allows you to get a log-object log that can be easily written to a specific target.
Second, why need log4j? The commissioning of---project is the internal driving force of log4j
The original approach is to output the information to the screen (console), using the System.out.println provided by the JDK. But the downside of doing so is obvious:
A the output of the information is not flexible and cumbersome. For example, to output the execution of the file name, line number, current time, etc., println appears very primitive.
b If you want to change the content and format of the output, you need to recompile the source program.
c More seriously, if there are many println in the program, it will seriously affect the performance of the program.
Three, log4j use of several key points?
Root logger (Logger), Output end (appenders), and layout (layouts)
A to define the root logger in the format
Log4j.rootlogger = [level], appendName1, appendName2, ... appendnamen. The same logger can have multiple output terminals.
Level of ps:level (this level can be customized, the system provides the following levels by default)
debug//Debugging Information
info//General Information
warn//warning message
error//error message
fatal//Fatal error message