1. log4j
IntroductionLog4j is an open-source project of Apache. It allows developers to output log information at any interval. Log4j consists of three main types of components: 1) logger-is responsible for outputting log information and classifying and filtering log information to determine which logs should be output and which should be ignored. The loggers component outputs log information at five levels: Debug, info, warn, error, and fatal. The order of these five levels is: Debug <info <warn <error <fatal. If the level of a logger component is set to P, only logs with a higher level than P can be output. Logger has an inheritance relationship, and the top layer is rootlogger. Other defined logger will inherit rootlogger. 2) appender-defines the log output destination and specifies where the log information should be output. The output destination can be a console, file, or network device. 3) Layout-the output is formatted by attaching layout to the appender. A logger can have multiple appender. Each appender corresponds to a layout.
2. loggersThe format defined by logger: log4j. [loggername] = [level], appendername, appendername ,... Here level refers to the priority of logger. appendername is the output location of log information, and multiple output locations can be defined at the same time.
3. appendersThe Definition Format of appender: log4j. appender. appendername = fully. qualified. name. of. appender. class // "fully. qualified. name. of. appender. class "can specify one of the following five destinations: appender class and its role list
Appender Class Name |
For use |
Org. Apache. log4j. leleappender |
Output logs to the console |
Org. Apache. log4j. fileappender |
Output logs to files |
Org. Apache. log4j. dailyrollingfileappender |
Generates a log file 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 |
1) leleappender option-Threshold = warn: specify the minimum output level of log messages. -Immediateflush = true: the default value is true, meaning that all messages will be output immediately. -Target = system. Err: system. Out by default, which specifies the output console. 2) fileappender option-Threshold = warn: specify the minimum output level of log messages. -Immediateflush = true: the default value is true, meaning that all messages will be output immediately. -File1_mylog.txt: refers to the output of the message to the mylog.txt file. -Append = false: The default value is true. To add a message to a specified file, false means to overwrite the specified file content. 3) dailyrollingfileappender option-Threshold = warn: specify the minimum output level of log messages. -Immediateflush = true: the default value is true, meaning that all messages will be output immediately. -File1_mylog.txt: refers to the output of the message to the mylog.txt file. -Append = false: The default value is true. To add a message to a specified file, false means to overwrite the specified file content. -Datepattern = '. 'yyyy-ww: The file is rolled every week, that is, a new file is generated every week. You can also specify monthly, weekly, daily, and hour/minute. The corresponding format is as follows :'. 'yyyy-MM: month '. 'yyyy-ww: weekly '. 'yyyy-mm-DD: daily '. 'yyyy-mm-dd-A: Twice a day '. 'yyyy-mm-dd-hh: Hourly '. 'yyyy-mm-dd-hh-MM: 4 per minute) rollingfileappender option-Threshold = warn: specify the minimum output level of log messages. -Immediateflush = true: the default value is true, meaning that all messages will be output immediately. -File1_mylog.txt: refers to the output of the message to the mylog.txt file. -Append = false: The default value is true. To add a message to a specified file, false means to overwrite the specified file content. -Maxfilesize = 100kb: The suffix can be kb, MB, or GB. When the log file reaches this size, it will automatically scroll and move the original content to the mylog. log.1 file. -Maxbackupindex = 2: specify the maximum number of rolling files that can be generated.
4. layoutsLayout Definition Format: Part 1 log4j. appender. appendername. layout = fully. qualified. name. of. layout. class // "fully. qualified. name. of. layout. you can specify one of the following four formats: Layout class and its role list.
Layout Class Name |
For use |
Org. Apache. log4j. htmllayout |
Layout in HTML form |
Org. Apache. log4j. patternlayout |
You can flexibly specify the layout mode. |
Org. Apache. log4j. simplelayout |
The log information level and information string. |
Org. Apache. log4j. ttcclayout |
Contains the log generation time, thread, category, and other information |
1) htmllayout option-locationinfo = true: the default value is false. The Java file name and row number are output. -Title = My app file: The default value is log4j log messages. 2) patternlayout option-conversionpattern = % m % N: specify how to format the specified message. 3) xmllayout option-locationinfo = true: the default value is false, and the Java file and row number are output. Part 2: log4j. appender. a1.layout. conversionpattern = %-4r %-5 p % d {yyyy-mm-dd hh: mm: SSS} % C % m % N here we need to describe the meaning of several symbols in the log information format: 1)-X: Left alignment when X information is output. 2) % P: Priority of output log information, that is, debug, info, warn, error, and fatal. 3) % d: date or time of the log output time point. The default format is iso8601. You can also specify the format after it, for example, % d {YYY Mmm dd hh: mm: SS, SSS}, output is similar to: October 18, 2002 22:10:28, 921. 4) % R: The number of milliseconds it takes to output the log information from application startup to output. 5) % C: the category of the output log information, usually the full name of the class. 6) % T: name of the thread that generates the log event. 7) % L: location where the log event is output, equivalent to % C. % m (% F: % L) combination, including the category name, the thread that occurs, and the number of lines in the code. Example: testlog4.main (testlog4.java: 10 ). 8) % x: The NDC associated with the current thread (nested Diagnostic Environment), especially for applications with multiple clients and multithreading such as Java Servlets. 9) %: Output A "%" character. 10) % F: name of the file where the output Log message was generated. 11) % L: the row number in the output code. 12) % m: output the specific log information of the specified message in the code. 13) % N: Output A line break. For Windows, the line feed is "\ r \ n", and for UNIX, the line feed is "\ n. You can add modifiers between the % and mode characters to control the alignment of the minimum width, maximum width, and text. For example: 1) % 20c: specify the name of the output category. The minimum width is 20. If the category name is smaller than 20, it is right aligned by default. 2) %-20C: specify the name of the output category. The minimum width is 20. If the category name is smaller than 20, the "-" sign specifies the left alignment. 3) %. 30C: specify the name of the output category. The maximum width is 30. If the category name is greater than 30, the extra characters on the left will be truncated, but there will be no spaces if the value is less than 30. 4) % Category 30C: If the category name is less than 20, fill in spaces and align right. If the category name is longer than 30 characters, it will be truncated from the character sold on the left.
5. Apache Log
Introduction set[1]
Apache log4j configuration instructions[2]
Apache log4j instance[3]
Apache commons-logging instance[4]
How to build an appender to expand the log4j framework
6.
References[1] Zhao Qiang, proficient in JSP programming, Electronic Industry Press [2] log4j configuration process [url] Routing