LOG4J2 Configuration (node and output format)

Source: Internet
Author: User
Tags truncated log4j

This article mainly introduces the configuration of LOG4J2!!


1. 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, create new (through the max attribute), default to 7 files.

(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.


2. Detailed output format:

%c Lists the full name of the logger namespace, and if you add the {< layer, the name space that lists the specified number of layers from the inner layer log4j configuration File Parameters Example Output Display Media
Suppose the current logger namespace is "A.B.C"
%c A.b.c
%C{2} B.c
%20c (If the name space length is less than 20, then the left is padded with a space)
%-20c (If the name space length is less than 20, then the right with a space padding)
%.30c (If the name space is longer than 30, the extra characters are truncated)
%20.30c (If the name space length is less than 20, then the left is padded with a space; If the name space is longer than 30, the extra characters are truncated)
%-20.30c (If the name space length is less than 20, then the right is padded with a space; If the name space is longer than 30, the extra characters are truncated)
%c Lists the full name of the class that called logger (including the package path) Suppose the current class is "Org.apache.xyz.SomeClass"
%c Org.apache.xyz.SomeClass
%c{1} SomeClass
%d Show logging time,{< date format using the date format defined by ISO8601 %d{yyyy/mm/dd Hh:mm:ss,sss} 2005/10/12 22:23:30,117
%d{absolute} 22:23:30,117
%d{date} OCT 2005 22:23:30,117
%d{iso8601} 2005-10-12 22:23:30,117
%F Displays the source file name of the calling logger %F Myclass.java
%l The location where the output log event occurred, including the class name, the thread that occurred, and the number of lines in the code %l Myclass.main (myclass.java:129)
%l Show lines of code calling Logger %l 129
%m Display output messages %m This is a message for debug.
%m Displays the name of the method calling logger %m Main
%n Line breaks under the current platform %n The RN is represented under Windows platform
The Unix platform represents N
%p Show priority for this log %p INFO
%r Displays the number of milliseconds that have elapsed since the program was started and when the log was logged %r 1215
%t Output the name of the thread that generated the log event %t MyClass
%x Press NDC (Nested diagnostic context, thread stack) Order output log Suppose that the order of a program invocation is MyApp call Com.foo.Bar
%c%x-%m%n Myapp-call Com.foo.Bar.
Com.foo.bar-log in Bar
Myapp-return to MYAPP.
%x Logs are output by MDC (mapped diagnostic context, thread mapping table). It is commonly used for multiple clients to connect to the same server, which makes it easier for the server to differentiate between the logs that the client accesses. %X{5} (Log of the client with code number 5)
%% Show a percent semicolon %% %




A relatively complete template:

<?xml version= "1.0" encoding= "UTF-8"?> <!--the status behind the configuration, which is used to set the output of the information inside the log4j2 itself, can be set without--> <! --MONITORINTERVAL:LOG4J can automatically detect modification of configuration files and reconfigure itself, set interval seconds--> <configuration status= "WARN" monitorinterval= ">" lt;! --First Define all appender--> <appenders> <!--output to console--> <console name= console target= system_out
         "> <!--output log format--> <patternlayout pattern=" [%d{hh:mm:ss:sss}] [%p]-%l-%m%n "/> </console> <!--Print to the file, this log is automatically emptied each time the program is run, determined by the Append property--> <file name= "log" filename= "Log/test.log" app End= "false" > <patternlayout pattern= "%d{hh:mm:ss. SSS}%-5level%class{36}%l%m-%msg%xex%n "/> </File> <!--file scrolling, this will print out all info and the following level of information, each size larger than, then this size Small logs are automatically saved under the folder created by year-month and compressed, as archived--> <rollingfile name= "Rollingfileinfo filename=" ${sys:user.home}/logs/in Fo.log "filepattern=" ${sys:user.home}/logs/$${date:yyyy-mm}/info-%d{yyYy-mm-dd}-%i.log "> <!--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 "/> <Policies> <defaultrolloverstrategy max=" "/> <!--folder most
                 Number of files--> <timebasedtriggeringpolicy interval= "2 hour"/> <!--how long to scroll once--> <sizebasedtriggeringpolicy size= "MB"/> <!--maximum size of a log file--> </Policies> </ Rollingfile> </appenders> <!--then define logger, and only appender,appender that define logger and introduce them will take effect--&G
     T <loggers> <!--filter out some useless debug information for spring and MyBatis--> <logger name= "Org.springframework" Info "></logger> <logger name=" Org.mybatis "level=" info "></logger> <root level=" a		 
     ll ">       <appender-ref ref= "Console"/> <appender-ref ref= "Rollingfileinfo"/> <appen Der-ref ref= "Rollingfilewarn"/> <appender-ref ref= "Rollingfileerror"/> </root> &
 
  Lt;/loggers> </configuration>



Reference articles:

Http://www.cnblogs.com/hafiz/p/6170702.html

http://blog.csdn.net/u012729481/article/details/21094003

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.