Detailed parsing of _ Log4j _ configuration, parsing _ log4j Configuration
1. Introduction to Log4j
Log4j has three main components: Loggers, Appenders, and Layouts ). This can be simply understood as the log category, where the log is to be output, and in what form the log is output. Using these three components together, you can easily record the types and levels of information and control the log output style and position at runtime.
1. Loggers
Loggers components in this system are divided into five levels: DEBUG, INFO, WARN, ERROR, and FATAL. These five levels are ordered. DEBUG <INFO <WARN <ERROR <FATAL is used to specify the importance of the log information. To understand this, Log4j has a rule: only log information of the level not lower than the set level is output. If the Loggers level is set to INFO, logs of the INFO, WARN, ERROR, and FATAL levels are output, DEBUG with a lower level than INFO will not be output.
2. Appenders
Disabling and using log requests is only a basic function of Log4j. The Log4j log system also provides many powerful functions, such as allowing logs to be output to different places, such as the Console) files, such as Files, can generate new Files based on the number of days or file size, can be sent to other places in the form of a stream, and so on.
The frequently used classes are as follows:
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)
Configuration Mode:
Log4j. appender. appenderName = className
Log4j. appender. appenderName. Option1 = value1
...
Log4j. appender. appenderName. OptionN = valueN
3. Layouts
Sometimes you want to format your log output according to your preferences. Log4j can add Layouts to the Appenders to complete this function. Layouts provides four types of log output styles, such as HTML styles, custom styles, styles that contain log levels and information, and styles that contain log time, threads, and categories.
The frequently used classes are as follows:
Org. apache. log4j. HTMLLayout (layout in HTML form)
Org. apache. log4j. PatternLayout (you can flexibly specify the layout mode)
Org. apache. log4j. SimpleLayout (Level and string of log information)
Org. apache. log4j. TTCCLayout (including the log generation time, thread, category, and other information)
Configuration Mode:
Log4j. appender. appenderName. layout = className
Log4j. appender. appenderName. layout. Option1 = value1
...
Log4j. appender. appenderName. layout. OptionN = valueN
Ii. configuration details
In practical applications, configuration files must be set in advance for Log4j to run in the system. The configuration file is actually set for Logger, Appender, and Layout. Log4j supports two configuration file formats: XML and properties. The following uses the properties file as an example to introduce the configuration of log4j. properties.
1. Configure the root Logger:
Log4j. rootLogger = [level], appenderName1, appenderName2 ,...
Log4j.additivity.org. apache = false: indicates that Logger will not be output in the appender of the parent Logger. The default value is true.
Level: set the minimum log record level. The value can be OFF, FATAL, ERROR, WARN, INFO, DEBUG, ALL, or custom level. Log4j recommends only four levels in the middle. By setting the level here, you can control the switch of log information of the corresponding level in the application, for example, setting the INFO level here, all DEBUG-level logs in the application will not be printed.
AppenderName: specifies where the log information is to be output. Multiple Output destinations can be specified at the same time, separated by commas.
Example: log4j. rootLogger = INFO, A1, B2, C3
2. Configure the log output destination (appender ):
Log4j. appender. appenderName = className
AppenderName: Custom appderName, used in log4j. rootLogger settings;
ClassName: The configurable values are as follows:
(1) org. apache. log4j. leleappender (console)
(2) org. apache. log4j. FileAppender (file)
(3) org. apache. log4j. DailyRollingFileAppender (a log file is generated every day)
(4) org. apache. log4j. RollingFileAppender (a new file is generated when the file size reaches the specified size)
(5) org. apache. log4j. WriterAppender (send log information to any specified place in stream format)
(1) leleappender options:
Threshold = WARN: specify the minimum log output level. The default value is DEBUG.
ImmediateFlush = true: indicates that all messages are output immediately. If it is set to false, no messages are output. The default value is true.
Target = System. err: The default value is System. out.
(2) FileAppender options:
Threshold = WARN: specify the minimum log output level. The default value is DEBUG.
ImmediateFlush = true: indicates that all messages are output immediately. If it is set to false, no messages are output. The default value is true.
Append = false: true indicates that the message is added to the specified file. If false, the message overwrites the content of the specified file. The default value is true.
File = D:/logs/logging. log4j: Specify the message output to the logging. log4j File.
(3) DailyRollingFileAppender options:
Threshold = WARN: specify the minimum log output level. The default value is DEBUG.
ImmediateFlush = true: indicates that all messages are output immediately. If it is set to false, no messages are output. The default value is true.
Append = false: true indicates that the message is added to the specified file. If false, the message overwrites the content of the specified file. The default value is true.
File = D:/logs/logging. log4j: specify that the current message is output to the logging. log4j File.
DatePattern = '. 'yyyy-MM: the log file is rolled every month, that is, a new log file is generated every month. The log file name for the current month is logging. log4j, and the log file name for the previous month is logging. log4j. yyyy-MM.
In addition, you can specify weekly, daily, hourly, and grading to scroll the log file. The corresponding format is as follows:
1) '. 'yyyy-MM: monthly
2) '. 'yyyy-ww: weekly
3) '. 'yyyy-MM-dd: daily
4) '. 'yyyy-MM-dd-a: twice a day
5) '. 'yyyy-MM-dd-HH: Hourly
6) '. 'yyyy-MM-dd-HH-mm: minute
(4) RollingFileAppender options:
Threshold = WARN: specify the minimum log output level. The default value is DEBUG.
ImmediateFlush = true: indicates that all messages are output immediately. If it is set to false, no messages are output. The default value is true.
Append = false: true indicates that the message is added to the specified file. If false, the message overwrites the content of the specified file. The default value is true.
File = D:/logs/logging. log4j: Specify the message output to the logging. log4j File.
MaxFileSize = 100KB: The suffix can be KB, MB, or GB. When the log file reaches this hour, it will automatically scroll, moving the original content to the logging. log4j. 1 file.
MaxBackupIndex = 2: specify the maximum number of rolling files that can be generated. For example, if set to 2, logging can be generated. log4j. 1. logging. log4j. 2. Two Rolling files and one logging. log4j file.
3. Configure the log output format (Layout ):
Log4j. appender. appenderName. layout = className
ClassName: The configurable values are as follows:
(1) org. apache. log4j. HTMLLayout (layout in HTML form)
(2) org. apache. log4j. PatternLayout (you can flexibly specify the layout mode)
(3) org. apache. log4j. SimpleLayout (Level and information string containing log information)
(4) org. apache. log4j. TTCCLayout (including the log generation time, thread, category, and other information)
(1) HTMLLayout options:
LocationInfo = true: Name and row number of the output java file. The default value is false.
Title = My Logging: The default value is Log4J Log Messages.
(2) PatternLayout options:
ConversionPattern = % m % n: set the format of the message to display.
Format symbol description:
% P: Priority of the output log information, namely DEBUG, INFO, WARN, ERROR, and FATAL.
% D: date or time of the log output time point. The default format is ISO8601. You can also specify the format after this, for example, % d {yyyy/MM/dd HH: mm: ss, SSS }.
% R: The number of milliseconds it takes to output the log information from application startup.
% T: name of the thread that generates the log event.
% L: the location where the output log event occurs. It is equivalent to a combination of % c. % M (% F: % L), including the class full name, method, file name, and number of lines in the code. For example, test. TestLog4j. main (TestLog4j. java: 10 ).
% C: the category of the output log, usually the full name of the class.
% M: name of the method that outputs log information.
% F: name of the file in which the output Log message is generated.
% L: The row number in the output code.
% M: output the specific log information specified in the Code.
% N: returns a line break. For Windows, the line break is/r/n and for Unix ".
% X: NDC associated with the current thread (nested Diagnostic Environment), especially used in applications with multiple clients and multithreading such as java servlets.
%: Output A "%" character.
Additionally, you can add modifiers between % and formatted characters to control the minimum length, maximum length, and text alignment. For example:
1) % 20c: specify the name of the output category. The minimum length is 20. If the length of the category name is smaller than 20, it is right aligned by default.
2) %-20c: "-" indicates left alignment.
3) %. 30c: specify the name of the output category. The maximum length is 30. If the length of the category name is greater than 30, the extra characters on the left are truncated, if it is less than 30, no space will be filled.
Appendix: Comprehensive Log4j Configuration
The Log4j configuration file provides a full set of functions such as output to the console, files, rollback files, sending log emails, output to database log tables, and custom tags.
Log4j. rootLogger = DEBUG, console, dailyFile, im
Log4j.additivity.org. apache = true
# Console)
Log4j. appender. console = org. apache. log4j. leleappender
Log4j. appender. console. Threshold = DEBUG
Log4j. appender. console. ImmediateFlush = true
Log4j. appender. console. Target = System. err
Log4j. appender. console. layout = org. apache. log4j. PatternLayout
Log4j. appender. console. layout. ConversionPattern = [%-5 p] % d (% r) --> [% t] % l: % m % x % n
# LogFile)
Log4j. appender. logFile = org. apache. log4j. FileAppender
Log4j. appender. logFile. Threshold = DEBUG
Log4j. appender. logFile. ImmediateFlush = true
Log4j. appender. logFile. Append = true
Log4j. appender. logFile. File = D:/logs/log. log4j
Log4j. appender. logFile. layout = org. apache. log4j. PatternLayout
Log4j. appender. logFile. layout. ConversionPattern = [%-5 p] % d (% r) --> [% t] % l: % m % x % n
# RollingFile)
Log4j. appender. rollingFile = org. apache. log4j. RollingFileAppender
Log4j. appender. rollingFile. Threshold = DEBUG
Log4j. appender. rollingFile. ImmediateFlush = true
Log4j. appender. rollingFile. Append = true
Log4j. appender. rollingFile. File = D:/logs/log. log4j
Log4j. appender. rollingFile. MaxFileSize = 200KB
Log4j. appender. rollingFile. MaxBackupIndex = 50
Log4j. appender. rollingFile. layout = org. apache. log4j. PatternLayout
Log4j. appender. rollingFile. layout. ConversionPattern = [%-5 p] % d (% r) --> [% t] % l: % m % x % n
# Regularly roll back log files (dailyFile)
Log4j. appender. dailyFile = org. apache. log4j. DailyRollingFileAppender
Log4j. appender. dailyFile. Threshold = DEBUG
Log4j. appender. dailyFile. ImmediateFlush = true
Log4j. appender. dailyFile. Append = true
Log4j. appender. dailyFile. File = D:/logs/log. log4j
Log4j. appender. dailyFile. DatePattern = '. 'yyyy-MM-dd
Log4j. appender. dailyFile. layout = org. apache. log4j. PatternLayout
Log4j. appender. dailyFile. layout. ConversionPattern = [%-5 p] % d (% r) --> [% t] % l: % m % x % n
# Apply to socket
Log4j. appender. socket = org. apache. log4j. RollingFileAppender
Log4j. appender. socket. RemoteHost = localhost
Log4j. appender. socket. Port = 5001
Log4j. appender. socket. LocationInfo = true
# Set up for Log Factor 5
Log4j. appender. socket. layout = org. apache. log4j. PatternLayout
Log4j. appender. socket. layout. ConversionPattern = [%-5 p] % d (% r) --> [% t] % l: % m % x % n
# Log Factor 5 Appender
Log4j. appender. LF5_APPENDER = org. apache. log4j. lf5.LF5Appender
Log4j. appender. LF5_APPENDER.MaxNumberOfRecords = 2000
# Send Logs to a specified email
Log4j. appender. mail = org.apache.log4j.net. SMTPAppender
Log4j. appender. mail. Threshold = FATAL
Log4j. appender. mail. BufferSize = 10
Log4j. appender. mail. From = xxx@mail.com
Log4j. appender. mail. SMTPHost = mail.com
Log4j. appender. mail. Subject = Log4J Message
Log4j. appender. mail. To = xxx@mail.com
Log4j. appender. mail. layout = org. apache. log4j. PatternLayout
Log4j. appender. mail. layout. ConversionPattern = [%-5 p] % d (% r) --> [% t] % l: % m % x % n
# Apply to databases
Log4j. appender. database = org. apache. log4j. jdbc. JDBCAppender
Log4j. appender. database. URL = jdbc: mysql: // localhost: 3306/test
Log4j. appender. database. driver = com. mysql. jdbc. Driver
Log4j. appender. database. user = root
Log4j. appender. database. password =
Log4j. appender. database. SQL = INSERT INTO LOG4J (Message) VALUES ('= [%-5 p] % d (% r) --> [% t] % l: % m % x % n ')
Log4j. appender. database. layout = org. apache. log4j. PatternLayout
Log4j. appender. database. layout. ConversionPattern = [%-5 p] % d (% r) --> [% t] % l: % m % x % n
# Custom Appender
Log4j. appender. im = net. cybercorlin. util. logger. appender. IMAppender
Log4j. appender. im. host = mail.cybercorlin.net
Log4j. appender. im. username = username
Log4j. appender. im. password = password
Log4j. appender. im. recipient = corlin@cybercorlin.net
Log4j. appender. im. layout = org. apache. log4j. PatternLayout
Log4j. appender. im. layout. ConversionPattern = [%-5 p] % d (% r) --> [% t] % l: % m % x % n