I. Background
Recently, due to the needs of the project, we migrated all versions of log4j 1.x to the log4j 2.x version, followed by slf4j consolidation log4j configuration (the perfect solution for SLF4J Integration LOG4J2 building the Project log system) and the LOG4J2 configuration file. We need to have a good chat. This article is devoted to explain the meaning of the tags of the log4j2.xml configuration file. ii. configuration of the whole solution
1. About the name of the configuration file and where it is stored in the project
The log4j 2.x version no longer supports file configuration like the. Properties suffix in 1.x, and the 2.x version profile suffix name can only be ". xml", ". JSON" or ". Jsn".
The priority of the System selection profile (from) is as follows:
(1) A file named Log4j2-test.json or LOG4J2-TEST.JSN under Classpath.
(2). A file named Log4j2-test.xml under Classpath.
(3). classpath a file named Log4j2.json or LOG4J2.JSN.
(4). classpath the file named Log4j2.xml.
We typically use Log4j2.xml to name them by default. If you want to test locally, you can put Log4j2-test.xml into the classpath, and the formal environment uses log4j2.xml, do not pack log4j2-test.xml when you package the deployment.
2. Default configuration file
1 <?xml version= "1.0" encoding= "UTF-8"?>
2 <configuration status= "WARN" >
3 <appenders >
4 <console name= "Console" target= "System_out" >
5 <patternlayout pattern= "%d{hh:mm: Ss. SSS} [%t]%-5level%logger{36}-%msg%n "/>
6 </Console>
7 </Appenders>
8 <Loggers>
9 <root level= "error" > <appenderref ref= "Console"/>
11 </Root> </Loggers>
</Configuration>
3. configuration file Node resolution
(1). The root node configuration has two properties: status and Monitorinterval, two child nodes: Appenders and loggers (indicating that multiple appender and logger can be defined).
Status is used to specify the level of the print log for the log4j itself.
Monitorinterval is used to specify the monitoring interval for log4j automatic reconfiguration, in S, and minimum 5s.
(2). Appenders nodes, there are three common seed nodes: Console, Rollingfile, File.
The console node is used to define the output to the console Appender.
Name: Specifies the Appender.
Target:system_out or System_err, usually set by default only: System_out.
Patternlayout: Output format, do not set default to:%m%n.
The file node is used to define the appender of the files that are exported to the specified location.
Name: Specifies the Appender.
FileName: Specifies the file name with the full path to the destination file for the output log.
Patternlayout: Output format, do not set default to:%m%n.
The Rollingfile node is used to define the automatic deletion of the old created new Appender over the specified size.
Name: Specifies the Appender.
FileName: Specifies the file name with the full path to the destination file for the output log.
Patternlayout: Output format, do not set default to:%m%n.
Filepattern: Specifies the name format of the new log file.
Policies: Specifies the policy for scrolling logs when a new log file output log is made.
Timebasedtriggeringpolicy:policies, a time based scrolling policy, Interval property is used to specify how often to scroll once and the default is 1 hour. Modulate=true is used to adjust the time: for example, this is the morning 3am,interval is 4, then the first scroll is 4am, then 8am,12am ... Rather than 7am.
Sizebasedtriggeringpolicy:policies, the Size property is used to define the sizes of each log file, based on the scrolling policy for the specified file size.
Defaultrolloverstrategy: Used to specify a maximum of several log files under the same folder to begin deleting the oldest, creating new (through the Max attribute).
(3). Loggers nodes, there are two common types: root and logger.
The root node is used to specify an item's roots log, and if logger is not specified separately, the root log output is used by default
Level: Log output levels, a total of 8 levels, from low to High: all < Trace < Debug < Info < Warn < Error < Fatal <.
Appenderref:root that specifies which appender the log output to.
The logger node is used to specify the form of the log individually, such as specifying a different log level for the class under the specified package.
Level: Log output levels, a total of 8 levels, from low to High: all < Trace < Debug < Info < Warn < Error < Fatal <.
Name: The full path of the package that specifies the class or class in which the logger applies, inherited from the root node.
Appenderref:logger, which specifies the log output to which appender, if not specified, The default is inherited from root. If specified, it will be output in the specified Appender and root appender, at which point we can set logger additivity= "false" to output only in the custom Appender.
(4). About log level.
There are 8 levels, from lowest to highest: all < Trace < Debug < Info < Warn < Error < Fatal <.
All: lowest level, used to open all log records.
Trace: Is tracking, that is, the program pushes the following, you can write a trace output, so trace should be very much, but it doesn't matter, we can set the minimum log level to not let him output.
Debug: Indicates that fine-grained informational events are useful for debugging applications.
Info: Messages highlight the application's running process at the coarse-grained level.
Warn: Output warnings and Warn logs at the following levels.
Error: Output errors message log.
Fatal: outputting Every serious error event will cause the application to exit the log.
Off: The highest level, used to turn off all logging.
The program prints a log that is above or equal to the set level, and the higher the log level, the fewer logs will be printed.
4. More complete Log4j2.xml configuration template
1 <?xml version= "1.0" encoding= "UTF-8"?>
2 <!--log level and priority order: off > FATAL > ERROR > WARN > INFO ; DEBUG > TRACE > All-->
3 <!--configuration, this is used to set the information output inside the LOG4J2 itself, not set, when set to TRACE, You will see LOG4J2 internal various detailed output-->
4 <!--monitorinterval:log4j can automatically detect and reconfigure the configuration file and reconfiguration itself, set the interval seconds of-->
5 < Configuration status= "WARN" monitorinterval= ">
6 <!--first define all appender-->
7 < Appenders>
8 <!--The configuration of this output console-->
9 <console name= "console" target= "System_out"