Log4j uses detailed parsing

In short, log4j is an API class library that helps developers manage log output. Its most important feature is that the configuration file can flexibly set the priority, output destination and output format of log information

Log4j not only can record program running log information, but also has an important function to display debugging information.

Programmers often encounter the situation of debugging programs outside the Java ide environment. At this time, most people will choose to use system out. Println statement is used to debug the method of outputting the value of a variable. This leads to a very troublesome problem: once one day the programmer decides not to display these systems out. Println can only comment out these garbage statements line by line. If you need to debug the variable value one day, you can only remove these comments and restore the system out. Println statement. Using log4j can handle similar situations well.

Log4j usage

The following is log4j some theoretical knowledge.

1. Define profile

First, using the configuration file will make our application more flexible to configure log output methods, including output priority, output destination and output format.

Log4j supports two configuration file formats,

1) Files in XML format,

2) Java properties file log4j Properties (key = value).

The following describes how to use log4j Properties file as configuration file:

① Configure root logger with syntax: 0

Among them, level is the priority of logging, which is divided into off, fat, error, warn, info, debug, all or user-defined levels.

Log4j recommends using only four levels. The priority from high to low is error, warn, info and debug

Through the levels defined here, you can control the switch to the corresponding level of log information in the application. For example, if the info level is defined here, all debug level log information in the application will not be printed. Appendername specifies where the log information is output. Multiple output destinations can be specified at the same time.

② Configure the log information output destination appender. Its syntax is:

Among them, log4j provides the following types of Appenders:

③ Configure the format (layout) of log information. Its syntax is:

Among them, log4j provides the following layouts:

Log4j formats log information in a print format similar to the printf function in C language. The print parameters are as follows:

%M the message% P output priority specified in the output code, i.e. debug, info, warn, error, fat% r the number of milliseconds it takes from application startup to output the log information% C the category to which the output belongs, usually the full name of the class% t the name of the thread generating the log event% n a carriage return line feed is output, and the windows platform is "\ R \ n", UNIX platform is "\ n"% d to output the date or time of the log time point. The default format is iso8601. You can also specify the format after it, such as:% d{yyy MMM DD HH: mm: SS, SSS}. The output is similar to: 22:10:28921 on October 18, 2002%l to output the location of the log event, including the class name, the thread that occurred, and the number of lines in the code. Example: testlog4 main(TestLog4.java:10)

2. Using log4j in code

① Get recorder

Using log4j, the first step is to obtain the logger, which will be responsible for controlling the log information. Its syntax is:

Get the recorder by the specified name and create a new recorder for that name if necessary. Name generally takes the name of this class, for example:

② Read configuration file

After obtaining the logger, the second step is to configure the log4j environment. The syntax is:

Example:

③ Insert record information (format log information)

When the above two necessary steps are completed, you can easily insert logging statements with different priorities anywhere you want to log. The syntax is as follows:

Log4j sample program

The simplest example program will be used to further illustrate the use of log4j. The program code is as follows:

Procedure description:

① static Logger logger = Logger. getLogger(LogTest.class.getName()); This is to create a logger object belonging to the logtest class. When creating, you should tell the logger what your current class is.

② PropertyConfigurator. Configure ("Src / log4j. Properties") means to use log4j. In the SRC folder under the current project directory The properties file is used as the configuration file. If log4j Properties can also be placed in the project root directory without writing this sentence, and the program will automatically find the configuration file.

③ logger. Debug is to output the debug information, logger Info is the output prompt, logger Warn is to display a warning message, logger Error is to display an error message.

Here is the configuration file log4j Properties:

Procedure description:

① log4j. rootCategory=DEBUG,stdout,R

That is, I want to display all information with priority equal to and higher than debug.

"Stdout", "R" means that I have defined two outputs (any name is good)

② The following three lines say that the stdout output is actually the standard output console, that is, the screen. The output format is patternlayout. The conversion method is%5p (% F:% L) -% m% N, that is, the first five boxes are used to display priority, then the current file name and the current number of lines. The last%m is logger Debug () or logger Info() or logger Warn() or logger Information in error().% N means carriage return blank line

③ In addition to the following six lines, the log information will not only be displayed on the screen, but will be saved in a file called "log. TXT", with a maximum file size of 100kb. If the file size exceeds 100kb, the file will be backed up as "log. TXT. 1", and the new "log. TXT" will continue to record log information.

Next we can change log4j Properties without recompiling, you can control whether log information is displayed, the output type, output mode, output format, etc.

Examples are as follows:

① In log4j In the properties file, rewrite "log4j. Rootcategory = debug, stdout, R" to "log4j. Rootcategory = off, R", so that all log information will not be displayed; It solves the problems raised at the beginning of this paper.

② In log4j In the properties file, rewrite "log4j. Rootcategory = debug, R" to "log4j. Rootcategory = info, R", so that only the log information of info, warn and error is displayed, and the debug information will not be displayed;

Using log4j in Web Application

1. Because JSP or servlet does not have the concept of current path when executing state, all use propertyconfigurator When the configure (string) statement finds the log4j.properties file, it should give the path relative to the current JSP or servlet and convert it into an absolute file system path.

The method is to use ServletContext Getrealpath (string) statement. Example:

2. Corresponding log4j When setting a property, you should also set the absolute path in the program. Example:

log4j. appender. R. The file property sets the location where log files are stored. We can read and write in English The method of the properties configuration file can be set flexibly.

Under the spring framework, it is easier to use log4j. The following describes the application of log4j under spring.

1) First, download the corresponding jar package (log4j. Jar)

2) Configure web XML, on the web Add the following configuration to XML

Note: in the above configuration, log4jconfiglistener will go to WEB-INF / config / log4j Propeties read the configuration file;

Open a watchdog thread to scan the changes of the configuration file every 60 seconds (so that you don't have to restart the web service to modify the configuration file after the web service is started);

And press the path of the web directory into a file called webapp The system variable of root (webapp.root will be used in the log4j.properties file).

summary

The above is the detailed analysis of log4j introduced by Xiaobian. I hope it will be helpful to you. If you have any questions, please leave me a message and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>