1. Background
Adding a log record to an application is generally based on three purposes: to monitor variable changes in the Code and periodically record the changes to the file for statistical analysis by other applications; tracking the code running track as the basis for future auditing; acting as a debugger in the integrated development environment, printing code debugging information to the file or console.
The most common practice is to embed many print statements into the code, which can be output to the console or file. A good practice is to construct a log operation class to encapsulate such operations, instead of flooding a series of print statements with the subject of the Code.
If the program uses a large number of system. out. If I don't want to print out the debugging information one day, I will comment out the sentence in the entire program, and I will print it again in a few days, then we need to remove them one by one. The workload can be imagined.
2 log4j Introduction
With the emphasis on reusable component development, in addition to developing a reusable log operation class from start to end, Apache provides us with a powerful log operation package-log4j.
Log4j is an open-source project of Apache. By using log4j, we can control the log information delivery destination, including the console, files, Gui components, and even the interface server, NT event recorder, and Unix Syslog daemon; we can also control the output format of each log. By defining the level of each log information, we can control the log generation process in more detail. The most interesting thing is that these can be configured flexibly through a configuration file, without the need to modify the application code.
In addition, through interfaces of other log4j languages, you can ,. net and PL/SQL programs use log4j. Its syntax and usage are the same as those in Java programs, so that the multi-language distributed system can obtain a unified and consistent log component module. Moreover, by using various third-party extensions, you can easily integrate log4j into J2EE, Jini, and even SNMP applications.
Therefore, the advantage of using log4j is that it can easily control whether log information is displayed, the output type, output mode, and output format of log information, and more precisely control the log generation process, however, the configuration file allows you to flexibly configure the configuration without having to change the code a lot.
3. Use and configure log4j
Log4j has three main components:
* Logger: Filters log information based on the set priority and forwards it to the storage device.
* Appender: receives and processes the log information forwarded by the recorder. The common processing is to output the log information to the screen or store it to a disk file.
* Layout: used to format log information.
Relationship between the above three:
One logger can mount multiple appender (log information is forwarded to multiple devices at the same time)
One appender specifies a layout for formatting.
1. Define the configuration file log4j. Properties
1.
Log4j. Recorder name = [level], storage device name 1, storage device name 2 ,...
Levels of priority from high to low are: Off, fatal, error, warn, info, debug, all
We recommend that you use only four levels of log4j. The priority ranges from high to low: error, warn, info, and debug.
Only log information with a higher priority or equal to the set level is forwarded. For example, if it is defined as info, logs with a debug level in the program will not be output.
Log4j. rootlogger is the parent object of all recorders (formerly rootcategory obsolete) and can be used to set the default priority of all recorders.
Log4j. mylogger. childlogger =, file
The recorder is obtained by name. It is automatically created (always exists) during the first access)
If it is agreed that each class only outputs logs to its recorder with the same name, you can configure logs for each specific class in the configuration file.
2.
Log4j. appender. storage device name = Storage Class Name
Currently, log4j implements the following types of storage classes:
Org. Apache. log4j. leleappender (console)
Org. Apache. log4j. fileappender (file)
Org. Apache. log4j. dailyrollingfileappender (a log file is generated every day)
Org. Apache. log4j. rollingfileappender (a new file is generated when the file size reaches the specified size)
Org. Apache. log4j. writerappender (send log information to any specified place in stream format)
3.
Log4j. appender. Name of the storage device. layout = Class Name of the layout Device
Currently, log4j implements the following types of la er classes:
Org. Apache. log4j. patternlayout (the standard log4j layout er can flexibly specify the layout mode)
Org. Apache. log4j. htmllayout (layout in HTML form)
Org. Apache. log4j. simplelayout (simple string containing level and log information)
Org. Apache. log4j. ttcclayout (including the log generation time, thread, category, and so on)
4.
Set the layout options (mainly set the conversion format)
Log4j. appender. Name of the storage device. layout. Option name = Value
The standard log4j la er is usually used:
Log4j. appender. Name of the storage device. layout = org. Apache. log4j. patternlayout
Log4j. appender. Name of the storage device. layout. conversionpattern = format Parameter
Log4j uses formatting parameters similar to the printf function in C language to format log information. The parameters are as follows:
% M output the specified information in the code
% P output priority, namely debug, info, warn, error, fatal
% R Number of milliseconds that the self-la er constructs to the time point
% C output category, usually the full name of the class
% T name of the thread that outputs the log event
% N output a carriage return line break. For Windows, the value is/R/N, and for UNIX, the value is/n"
% D indicates the date or time of the time point. The default format is iso8601. You can also specify the format later. For example: % d {YYY Mmm dd hh: mm: SS, SSS}, output class
Like: December 03 12:04:52, 531
% L location of log event output, including category name, thread, and number of lines in the code. For example: testlog4.main (testlog4.java: 10)
% F output file name
% L output row number
2. Call log4j in the code
1. Import the jar package (log4j-1.2.15.jar) of log4j into the project.
2. Write the configuration file log4j. properties in the src directory. The file is automatically copied to the bin directory during system running.
Java code
1. <span style = "font-family: Courier New, courier;">
2. Import org. Apache. log4j. Logger;
3 ....
4. Private Static final logger log = logger. getlogger (class name. Class );
5 ....
6. log. debug (Object message );
7. log.info (Object message );
8. log. Warn (Object message );
9. log. Error (Object message );
10.
11. </span>
Import org. Apache. log4j. Logger;
...
Private Static final logger log = logger. getlogger (class name. Class );
...
Log. debug (Object message );
Log.info (Object message );
Log. Warn (Object message );
Log. Error (Object message );
Iii. Example of log4j. Properties
XML Code
1. <span style = "font-family: Courier New, courier;">
2. # Set root logger priority to Info and its only appender to console.
3. log4j. rootlogger = info, console
4. log4j. rootlogger = info, console, logfile
5.
6. # Set the enterprise logger category to fatal and its only appender to console.
7.log4j.logger.org. Apache. axis. Enterprise = fatal, console
8.
9. # console is set to be a consoleappender using a patternlayout.
10. log4j. appender. Console = org. Apache. log4j. leleappender
11. log4j. appender. Console. Threshold = info
12. log4j. appender. Console. layout = org. Apache. log4j. patternlayout
13.
14. # pattern to output the caller's file name and line number.
15. log4j. appender. Console. layout. conversionpattern = % 5 p [% T] (% F: % L)-% m % N
16.
17. # logfile is set to be a file appender using a patternlayout.
18. log4j. appender. logfile = org. Apache. log4j. fileappender
19. log4j. appender. logfile. File = axis. Log
20. log4j. appender. logfile. append = true
21. log4j. appender. logfile. Threshold = info
22. log4j. appender. logfile. layout = org. Apache. log4j. patternlayout
23. log4j. appender. logfile. layout. conversionpattern = %-4r [% T] %-5 p % C % x-% m % N
24. </span>
# Set root logger priority to Info and its only appender to console.
Log4j. rootlogger = info, console
Log4j. rootlogger = info, console, logfile
# Set the enterprise logger category to fatal and its only appender to console.
Log4j.logger.org. Apache. axis. Enterprise = fatal, console
# Console is set to be a consoleappender using a patternlayout.
Log4j. appender. Console = org. Apache. log4j. leleappender
Log4j. appender. Console. Threshold = info
Log4j. appender. Console. layout = org. Apache. log4j. patternlayout
# Pattern to output the caller's file name and line number.
Log4j. appender. Console. layout. conversionpattern = % 5 p [% T] (% F: % L)-% m % N
# Logfile is set to be a file appender using a patternlayout.
Log4j. appender. logfile = org. Apache. log4j. fileappender
Log4j. appender. logfile. File = axis. Log
Log4j. appender. logfile. append = true
Log4j. appender. logfile. Threshold = info
Log4j. appender. logfile. layout = org. Apache. log4j. patternlayout
Log4j. appender. logfile. layout. conversionpattern = %-4r [% T] %-5 p % C % x-% m % N
Iv. Common Faults
Log4j: Warn please initialize the log4j system properly.
Solution:
[Eclipse Environment] copies the file log4j. properties to the src directory. The runtime automatically copies the file to the bin directory. Refresh may be required before.