1. Definition:
Log4j2 refers to log4j 2.X and later
2. Installation
Log4j-core-xx.jar
Log4j-api-xx.jar
Log4j-web-xx.jar (required reference for Web project)
3. Configuration
The configuration file location is located in: src root directory, even if there is no configuration file, it will not error, the default is the form of console output.
Log4j2 configuration file with Log4 (1. The x version is very different) and can only be in. xml,. json, or. jsn format, which is configured as follows (${web:rootdir} represents the Web root directory):
1 <?XML version= "1.0" encoding= "UTF-8"?>2 3 <ConfigurationStatus= "Error">4 <!--define all the Appender first -5 <appenders>6 <!--The configuration of this output console -7 <Consolename= "Console"Target= "System_out">8 <!--This is the format of the output log -9 <Patternlayoutpattern= "%d{hh:mm:ss." SSS}%-5level%class{36}%l%M-%msg%xex%n "/>Ten </Console> One <!--The file will print out all the information, and this log will be automatically emptied each time the program is run, determined by the Append property, suitable for temporary testing - A <Filename= "Error"FileName= "${web:rootdir}/logs/error.log"Append= "false"> - <!--filerecord level and above information only (Onmatch), other direct rejection (Onmismatch) - - <Thresholdfilter Level= "Error"Onmatch= "ACCEPT"Onmismatch= "DENY"/> the <Patternlayoutpattern= "%d{hh:mm:ss." SSS}%-5level%class{36}%l%M-%msg%xex%n "/> - </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= "${web:rootdir}/logs/history.log" - Filepattern= "log/$${date:yyyy-mm}/history-%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"/> A <Sizebasedtriggeringpolicysize= "50MB"/> at </Rollingfile> - </appenders> - <!--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"> in <Appender-refref= "Error"/> - <Appender-refref= "Rollingfile"/> to <Appender-refref= "Console"/> + </Root> - </Loggers> the </Configuration>
4. General projects and Web projects
For ordinary projects, the above configuration is done to normal use, for Web projects, is not generated log files. You need to add the following configuration in the Web. XML <web-app> root node:
1 <!--log4j2.x Start -2 <Listener>3 <Listener-class>Org.apache.logging.log4j.web.Log4jServletContextListener</Listener-class>4 </Listener>5 <Filter>6 <Filter-name>Log4jservletfilter</Filter-name>7 <Filter-class>Org.apache.logging.log4j.web.Log4jServletFilter</Filter-class>8 </Filter>9 <filter-mapping>Ten <Filter-name>Log4jservletfilter</Filter-name> One <Url-pattern>/*</Url-pattern> A </filter-mapping> - <!--log4j2.x End -
(myeclipse example) click here to download
Using LOG4J2 to log logs under Java