1. Import of Log4j2
First download the latest LOG4J2 jar package to http://logging.apache.org/log4j/2.x/download.html, Then add Log4j-api-2.4.1.jar and Log4j-core-2.4.1.jar to eclipse, and be careful not to import all the jars into the project causing unnecessary clutter.
2.LogEvent
The left vertical bar is the level of event, and the right bar is the level of loggerconfig (that is, filter). Yes means that the event can be filter,no by means of a filter.
As you can see, the event at info level cannot be accepted by the Loggerconfig filter of the error level. Therefore, info information is not output.
3. Configuration file Authoring
Log4j2 differs from previous log4j in that its configuration file can only be used with. XML,. json, or. jsn instead of. properties files. The format is as follows:
1 <?XML version= "1.0" encoding= "UTF-8"?>2 <ConfigurationStatus= "Error">3 <!--define all the Appender first -4 <appenders>5 <!--The configuration of this output console -6 <Consolename= "Console"Target= "System_out">7 <!--console only outputs level and above information (Onmatch), other direct rejections (Onmismatch) -8 <Thresholdfilter Level= "Trace"Onmatch= "ACCEPT"Onmismatch= "DENY"/>9 <!--This is all about the format of the output log. -Ten <Patternlayoutpattern= "%d{hh:mm:ss." SSS}%-5level%class{36}%l%M ==>%msg%xex%n "/> One </Console> A <!--The file will print out all the information, this log is automatically emptied each time the program is run by the Append property, which is useful for temporary testing - - <Filename= "Log"FileName= "Log/test.log"Append= "false"> - <Patternlayoutpattern= "%d{hh:mm:ss." SSS}%-5level%class{36}%l%M ==>%msg%xex%n "/> the </File> - <!--This will print out all the information, each time the size exceeds size, the size of the log will be automatically saved by the year-month folder created under and compressed, as the archive - - <Rollingfilename= "Rollingfile"FileName= "Logs/app.log" - Filepattern= "log/$${date:yyyy-mm}/app-%d{mm-dd-yyyy}-%i.log.gz"> + <Patternlayoutpattern= "%d{yyyy-mm-dd ' at ' HH:mm:ss z}%-5level%class{36}%l%M-%msg%xex%n"/> - <Sizebasedtriggeringpolicysize= "50MB"/> + </Rollingfile> A </appenders> at <!--then define the logger, only the appender,appender that define the logger and introduce will take effect - - <Loggers> - <!--Create a default root logger - - <Root Level= "Trace"> - <Appender-refref= "Rollingfile"/> - <Appender-refref= "Console"/> in </Root> - </Loggers> to </Configuration>
View Code
4. configuration file name and path
(1) placed under Classpath (SRC), named after Log4j2.xml
The use of log4j2 generally used to write a log4j2.xml placed in the SRC directory.
(2) Place the configuration file elsewhere
In Systems engineering, it is inconvenient to put log4j2 configuration files under the SRC directory. If you can put all the configuration files used in the project in a folder, of course, more orderly and better management. But to achieve this, the premise is that the LOG4J2 configuration file can be relocated elsewhere, not under classpath.
Let's see what the document says:
1.LOG4J would inspect the "Log4j.configurationfile" system property and, if Set,will attempt to load the configuration usin G The configurationfactory that matches the file extension.
2.If No system property is set the JSON configurationfactory would look for Log4j2-test.json or log4j2-test.jsn in the Clas spath.
3.If No such file is found the XML configurationfactory would look for log4j2-test.xml in the classpath.
4.If a test file cannot be located the JSON configurationfactory would look for Log4j2.json or LOG4J2.JSN on the classpath.
5.If a JSON file cannot be located the XML Configurationfactory would try to locate log4j2.xml on the classpath.
6.If no configuration file could be located the defaultconfiguration would be used. This would cause logging output to go to the console.
Visible, if the "Log4j.configurationfile" system property is not set, application will find the configuration file in Classpath in the following order:
Log4j2-test.json or LOG4J2-TEST.JSN file
Log4j2-test.xml file
Log4j2.json or LOG4J2.JSN file
Log4j2.xml file
If you want to rename the configuration file and place it elsewhere, you need to set the system property Log4j.configurationfile.
It is set by the key and value of the property written in VM arguments:
-dlog4j.configurationfile= "D:\learning\blog\20130115\config\LogConfig.xml"
"(x) =argument" = "VM arguments" in MyEclipse, right-"run as-" Run configuration-right window
Then write the above key and value.
-D is a parameter and cannot be missing.
Test
To write a file under the "D:\learning\blog\20130115\config\" path:
The level of root loggerconfig is set to info.
Write Log4j.configurationfile System Properties in MyEclipse:
Test the Java program as above, no longer repeat here. operation, the console output is as follows:
LOG4J2-LOG4J2 Import, LogEvent, configuration file authoring, and path