LOG4J2 configuration file Log4j2.xml

Source: Internet
Author: User
Tags log4j

Recently the company in the overall push LOG4J2 upgrade, the framework gives a lot of documents, but there is no detail, in order to find out, in the online check the next log4j2, sorted as follows:
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

<?xml version= "1.0" encoding= "UTF-8"?> <configuration status=
 "WARN" >
    <Appenders>
      <console name= "Console" target= "System_out" >
        <patternlayout pattern= "%d{hh:mm:ss. SSS} [%t]%-5level%logger{36}-%msg%n '/>
      </Console>
    </Appenders>
    <Loggers>
      <root level= "Error" >
       <appenderref ref= "Console"/>
     </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 > D Ebug > TRACE > All--> 3 <!--configuration The status, which is used to set the information output within the LOG4J2 itself, can be set without setting, 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, setting the interval seconds--> 5 <configuration 
 "WARN" monitorinterval= > 6 <!--first Define all appender--> 7 <appenders> 8 <!--The configuration of this output console--> 9 <console name= "Console" target= "System_out" > <!--output log format--> &LT;PATTERNL Ayout pattern= "[%d{hh:mm:ss:sss}] [%p]-%l-%m%n"/> </console> <!--files Print out all the information, and this log runs every time the program Automatic emptying, determined by the Append property, is also useful for temporary testing--> <file name= "log" filename= "Log/test.log" append= "false" > ; Patternlayout pattern= "%d{hh:mm:ss. SSS}%-5level%class{36}%l%m-%msg%xex%n "/> </File> <!--This will print all info and the following levels of information, each larger than size, then the S Ize log will be the size of theMove into the folder created by year-month and compress it as archive--> <rollingfile name= "Rollingfileinfo filename=" ${SYS:USER.HOME}/LOGS/INFO.L             OG "filepattern=" ${sys:user.home}/logs/$${date:yyyy-mm}/info-%d{yyyy-mm-dd}-%i.log "> 20  <!--the console outputs only level and above levels of information (Onmatch), other direct rejections (Onmismatch)--> <thresholdfilter level= "Info" Onmatch= "ACCEPT" onmismatch= "DENY"/> <patternlayout pattern= "[%d{hh:mm:ss:sss}] [%p]-%l-%m%n"/&G
T <Policies> <TimeBasedTriggeringPolicy/> <sizebasedtrig Geringpolicy size= "MB"/> </Policies> </RollingFile> <rollingfil E name= "Rollingfilewarn" filename= "${sys:user.home}/logs/warn.log" filepattern= "${sys:user.home}/l Ogs/$${date:yyyy-mm}/warn-%d{yyyy-mm-dd}-%i.log "> <thresholdfilter level=" Warn "onmatch=" ACCEPT "OnMi" Smatch= "DENY"/> 31             <patternlayout pattern= "[%d{hh:mm:ss:sss}] [%p]-%l-%m%n"/> <Policies> 33             <TimeBasedTriggeringPolicy/> <sizebasedtriggeringpolicy size= "MB"/> 35 </Policies> <!--defaultrolloverstrategy properties if not set, the default is the maximum of 7 files in the same folder, which is set--> 37 & Lt;defaultrolloverstrategy max= "/> </RollingFile> <rollingfile name=" Rollingfileerror "Filename=" ${sys:user.home}/logs/error.log "filepattern=" ${sys:user.home}/logs/$${date:yyyy-mm}/e Rror-%d{yyyy-mm-dd}-%i.log "> <thresholdfilter level=" error "onmatch=" ACCEPT "onmismatch=" DENY "/> 4                 2 <patternlayout pattern= "[%d{hh:mm:ss:sss}] [%p]-%l-%m%n"/> <Policies> 44             <TimeBasedTriggeringPolicy/> <sizebasedtriggeringpolicy size= "mb"/> 46 </policies> </RollingFile> </appenders>!--and then define logger, only the appender,appender that defines logger and introduces it will take effect--&
Gt <loggers> I <!--filter out some useless debug information for spring and MyBatis--> <logger name= "Org.springframew Ork "level=" info "></logger> <logger name=" Org.mybatis "level=" info "></logger> 54 & Lt;root level= "All" > <appender-ref ref= "Console"/> "<appender-ref ref=" Rollingfil Einfo "/> <appender-ref ref=" Rollingfilewarn "/> <appender-ref" ref= or "/> </root> </loggers> </configuration>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.