Log4j_ Configuration method (comprehensive explanation)

1、 Log4j introduction

Log4j has three main components: loggers, Appenders, and layouts. It can be simply understood here as log category, where the log is to be output and in what form. Using these three components together, you can easily record the type and level of information, and control the style and location of log output at run time.

1、Loggers

Loggers components are divided into five levels in this system: debug, info, warn, error and fatal. These five levels are sequential. Debug < info < warn < error < fatal are used to specify the importance of this log information respectively. It is important to understand this. Log4j has a rule: only log information with a level not lower than the set level is output. Assuming that the loggers level is set to info, the log information of info, warn, error and fatal levels will be output, Debug with a lower level than info will not be output.

2、Appenders

Disabling and using log requests is only the 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 console and files. New files can be generated according to days or file size, and can be sent to other places in the form of streams.

The commonly used classes are as follows:

org. apache. log4j. Consoleappender (console) org.apache.log4j.fileappender (file) org.apache.log4j.dailyrollingfileappender (generate a log file every day) org.apache.log4j.rollingfileappender (generate a new file when the file size reaches the specified size) org.apache.log4j.writerappender (send log information to any specified place in stream format)

Configuration mode:

3、Layouts

Sometimes users want to format their own log output according to their preferences. Log4j can complete this function by adding layouts after Appenders. Layouts provides four log output styles, such as HTML style, freely specified style, style containing log level and information, and style containing log time, thread, category and other information.

The commonly used classes are as follows:

org. apache. log4j. Htmllayout (layout in HTML table form) org.apache.log4j.patternlayout (layout mode can be specified flexibly) org.apache.log4j.simplelayout (including the level and information string of log information) org.apache.log4j.ttcclayout (including the time, route, category and other information of log generation)

Configuration mode:

2、 Configuration details

In practical application, to make log4j run in the system, the configuration file must be set in advance. The configuration file actually sets the logger, appender and layout accordingly. Log4j supports two configuration file formats, one is an XML file and the other is a properties file. Next, take the properties property file as an example to introduce log4j Properties.

1. Configure root logger:

log4j. rootLogger = [ level ],appenderName1,appenderName2,…

log4j. additivity. org. Apache = false: indicates that the logger will not be output in the appender of the parent logger. It is true by default.

Level: sets the lowest level of logging. The values that can be set include off, fat, error, warn, info, debug, all or user-defined levels. Log4j recommends using only the middle four levels. By setting the level here, you can control the switch of log information at the corresponding level in the application. For example, if the info level is set here, all debug level log information 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.

For example: log4j rootLogger=INFO,A1,B2,C3

2. Configure log information output destination (appender):

log4j. appender. Appendername = classname appendername: Custom appendername, in log4j Used in rootlogger settings;

Classname: the following values can be set:

(1)org. apache. log4j. Consoleprovider (2) org apache. log4j. Fileappender (3) org apache. log4j. Dailyrollingfileappender (generate a log file every day) (4) org apache. log4j. Rollingfileappender (generate a new file when the file size reaches the specified size) (5) org apache. log4j. Writerappender (send log information in stream format to any specified place)

(1) Consoleappender options:

Threshold = warn: Specifies the minimum output level of log information. The default is debug. Immediateflush = true: indicates that all messages will be output immediately. If it is set to false, it will not be output. The default value is true. Target=System. Err: the default value is system out。

(2) Fileappender options:

Threshold = warn: Specifies the minimum output level of log information. The default is debug. Immediateflush = true: indicates that all messages will be output immediately. If it is set to false, it will not be output. The default value is true. Append = false: true indicates that the message is added to the specified file. False overwrites the content of the specified file. The default value is true. File=D:/logs/logging. Log4j: Specifies that the message is output to logging Log4j file.

(3) Dailyrollingfileappender option:

Threshold = warn: Specifies the minimum output level of log information. The default is debug. Immediateflush = true: indicates that all messages will be output immediately. If it is set to false, it will not be output. The default value is true. Append = false: true indicates that the message is added to the specified file. False overwrites the content of the specified file. The default value is true. File=D:/logs/logging. Log4j: Specifies that the current message is output to logging Log4j file. DatePattern='.' Yyyy mm: scroll the log file once a month, that is, generate a new log file every month. The log file name of the current month is logging Log4j, the log file name of the previous month is logging log4j. yyyy-MM。

In addition, you can specify to scroll log files by week, day, hour, minute, etc. the corresponding format is as follows:

1)'.' Yyyy mm: 2) per month '.' Yyyy WW: 3) per week '.' Yyyy MM DD: 4) '.' Yyyy-mm-dd-a: twice a day (5) '.' Yyyy MM DD HH: 6) 'per hour.' Yyyy MM DD HH mm: per minute

(4) Rollingfileappender option:

Threshold = warn: Specifies the minimum output level of log information. The default is debug. Immediateflush = true: indicates that all messages will be output immediately. If it is set to false, it will not be output. The default value is true. Append = false: true indicates that the message is added to the specified file. False overwrites the content of the specified file. The default value is true. File=D:/logs/logging. Log4j: Specifies that the message is output to logging Log4j file. Maxfilesize = 100kb: the suffix can be KB, MB or GB. When the log file reaches this size, it will automatically scroll, that is, move the original content to logging log4j. 1 file. Maxbackupindex = 2: Specifies the maximum number of scrolling files that can be generated. For example, setting it to 2 can generate logging log4j. 1,logging. log4j. 2 two scrolling files and a logging Log4j file.

3. Configure the output format of log information (layout):

log4j. appender. appenderName. layout=className

Classname: the following values can be set:

(1)org. apache. log4j. Htmllayout (layout in HTML table form) (2) org apache. log4j. Patternlayout (layout mode can be specified flexibly) (3) org apache. log4j. Simplelayout (level and information string containing log information) (4) org apache. log4j. Ttcclayout (including log generation time, thread, category, etc.)

(1) Htmllayout options:

Locationinfo = true: output the java file name and line number. The default value is false. Title = my logging: the default value is log4j log messages.

(2) Patternlayout options:

Conversionpattern =% m% n: sets the format in which messages are displayed.

Format symbol description:

%p: Priority of output log information, i.e. debug, info, warn, error, fatal.% n d: 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 {yyyy / mm / DD HH: mm: SS, SSS}.% r: The number of milliseconds it took to output the log information since the application started.% 0 t: Output the name of the thread that generated the log event.% n l: The occurrence location of the output log event is equivalent to the combination of% C.% m (% F:% L), including the full name of the class, method, file name and the number of lines in the code. For example: test TestLog4j. main(TestLog4j.java:10)。 % c: The category to which the output log information belongs, usually the full name of the class.% n M: Output the name of the method that generates log information.% n F: The name of the file where the output log message was generated.% n 50: : line number in output code.% 0 m: : the specific log information specified in the output code.% 0 n: Output a carriage return line feed character, which is "/ R / N" for Windows platform and "/ N" for UNIX platform.% n x: Output the NDC (nested diagnostic environment) associated with the current thread, especially for multi client and multi-threaded applications such as Java servlets.%%: Output a '%' character.

In addition, you can add modifiers between% and format characters to control their 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 name length of the category is less than 20, it is aligned right by default.

2) % - 20C: "-" indicates left alignment.

3) %. 30C: Specifies the name of the output category. The maximum length is 30. If the name length of the category is greater than 30, the extra characters on the left will be truncated, but if it is less than 30, no spaces will be filled.

Attachment: log4j relatively comprehensive configuration

Log4j configuration file realizes a full set of functions such as outputting to console, file, rollback file, sending log mail, outputting to database log table, custom label and so on.

The above is the log4j that Xiaobian brought to you_ All the contents of the configuration method (comprehensive explanation), I hope you like it~

If you want to know more about game activities and game strategies, please continue to pay attention to this site. This site will bring you the best, most fun and freshest game information at the first time. More exciting content in jb51 game channel!

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
分享
二维码
< <上一篇
下一篇>>