当前位置:网站首页>Log4j2.xml configuration details
Log4j2.xml configuration details
2022-07-27 12:20:00 【LOOPY_ Y】
log4j2.xml Configuration details
Reference article :
https://mp.weixin.qq.com/s/8mEU5ofwQPWA14JbtmQNrw
https://www.cnblogs.com/keeya/p/10101547.html
1 log4j2 Introduce
log4j2.x Version no longer supports things like 1.x Medium .properties How to configure the suffix file ,2.x The suffix of the version configuration file can only be ".xml",".json" perhaps ".jsn".
The format of the configuration file :log2j The configuration file can be xml Format , It can also be json Format .
The location of the configuration file :log4j2 The default will be classpath Look for log4j2.xml、log4j.json、log4j.jsn Wait for the name of the document .
The system selects the priority of the configuration file ( From first to last ) as follows :
(1).classpath The name below is log4j2-test.json perhaps log4j2-test.jsn The file of .
(2).classpath The name below is log4j2-test.xml The file of .
(3).classpath It's called log4j2.json perhaps log4j2.jsn The file of .
(4).classpath It's called log4j2.xml The file of .
We usually default to log4j2.xml Name it .
2 Profile node resolution
The root node Configuration
Two attributes :
- status: Used to specify log4j The level of printing log itself
- monitorinterval: Is used to specify the log4j Automatic reconfiguration of monitoring intervals , The unit is s, The minimum is 5s
Two child nodes : - Appenders
- Loggers( Indicates that more than one... Can be defined Appender and Logger).
Appenders node
There are three common seed nodes :Console、RollingFile、File
Console Nodes are used to define the output to the console Appender.
Console Nodes are used to define the output to the console Appender
- name: Appoint Appender Name .
- target:SYSTEM_OUT or SYSTEM_ERR, Generally, only the default is set :SYSTEM_OUT.
- PatternLayout: Output format , Do not set the default to :%m%n.
- File Node is used to define the file output to the specified location Appender.
File Node is used to define the file output to the specified location Appender
- name: Appoint Appender Name .
- fileName: Specifies the file name with full path of the destination file of the output log .
- PatternLayout: Output format , Do not set the default to :%m%n.
- RollingFile The node is used to define to automatically delete the old ones and create new ones beyond the specified conditions Appender.
RollingFile The node is used to define to automatically delete the old ones and create new ones beyond the specified conditions Appender
- name: Appoint Appender Name .
- fileName: Specifies the file name with full path of the destination file of the output log .
- PatternLayout: Output format , Do not set the default to :%m%n.
- filePattern : Specify when Rolling when , File transfer and rename rules .
- Policies: Specify the policy for rolling logs , When to create a new log file and output the log .
- TimeBasedTriggeringPolicy:Policies Child node , Time based rolling strategy ,interval Property is used to specify how often to scroll , The default is 1 hour.modulate=true Used to adjust the time : For example, it's morning 3am,interval yes 4, So the first roll is in 4am, Next is 8am,12am… instead of 7am.
- SizeBasedTriggeringPolicy:Policies Child node , Scrolling strategy based on the specified file size ,size Property is used to define the size of each log file .
- DefaultRolloverStrategy: Used to specify a maximum of several log files in the same folder to start deleting the oldest , Create a new ( adopt max attribute ).
Loggers node , There are two kinds of common :Root and Logger
Root Node is used to specify the root log of the project , If not specified separately Logger, Then the default is to use the Root Log output
- level: Log output level , share 8 A level , From low to high :All < Trace < Debug < Info < Warn < Error < - AppenderRef:Root Child nodes of , It is used to specify to which Appender.
- Logger The node is used to specify the form of the log separately , For example, to specify the package under class Specify different log levels, etc .
- level: Log output level , share 8 A level , From low to high :All < Trace < Debug < Info < Warn < Error < Fatal < OFF.
- name: To specify the Logger The applicable class or the full path of the package where the class is located , Inherited from Root node .
- AppenderRef:Logger Child nodes of , It is used to specify to which Appender, If not specified , Will inherit from by default Root. If you specify , So it'll be in the designated Appender and Root Of Appender All of them will output , Now we can set Logger Of additivity="false" Only in custom Appender Output in
3 Configuration parameter explanation
3.1 The level of logging
| Level | explain |
|---|---|
| trace | track , It's just the process of pushing forward , You can write a trace Output |
| debug | debugging , Generally as the lowest level ,trace Basically not . |
| info | Output important information , More use |
| warn | Warning , Some messages are not error messages , But also give the programmer some tips . |
| error | error message . It's also used a lot . |
| fatal | Fatal error |
3.2 output source
| Category | explain |
|---|---|
| CONSOLE | Output to console |
| FILE | output to a file |
3.3 Format
| Category | explain |
|---|---|
| SimpleLayout | Show in a simple form |
| HTMLLayout | With HTML The table shows |
| PatternLayout | Custom form display |
PatternLayout Custom log layout :
| Format | explain |
|---|---|
| %d{HH:mm:ss.SSS} | Represents the time from output to milliseconds |
| %t | Output the current thread name |
| %-5level | Output log level ,-5 Indicates left alignment and fixed output 5 Characters , If it's not enough, fill it on the right 0 |
| %logger | Output logger name |
| %msg | Log text |
| %n | Line break |
| %F | The name of the class file where the output is located , Such as Log4j2Test.java |
| %L | Output line number |
| %M | Output method name |
| %l | The number of lines where the output statement is located , Including the name of the class 、 Method name 、 file name 、 Row number |
4 Simplest configuration
<?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>
5 Templates
<?xml version="1.0" encoding="UTF-8"?>
<!--Configuration hinder status, This is used to set log4j2 Internal information output , You can leave it blank , When set to trace when , You'll see log4j2 Various internal detailed output -->
<!--monitorInterval:Log4j It can automatically detect and modify the configuration Files and reconfiguration itself , Set interval seconds -->
<configuration monitorInterval="5">
<!-- Log level and prioritization : OFF > FATAL > ERROR > WARN > INFO > DEBUG > TRACE > ALL -->
<!-- Variable configuration -->
<Properties>
<!-- Format output :%date Indicates the date ,%thread Represents the thread name ,%-5level: The level is shown from the left 5 Character width %msg: Log message ,%n Is a newline -->
<!-- %logger{36} Express Logger The longest name 36 Characters -->
<property name="LOG_PATTERN" value="%date{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n" />
<!-- Define the path of log storage -->
<property name="FILE_PATH" value=" Change to your log path " />
<property name="FILE_NAME" value=" Change to your project name " />
</Properties>
<appenders>
<console name="Console" target="SYSTEM_OUT">
<!-- Format of output log -->
<PatternLayout pattern="${LOG_PATTERN}"/>
<!-- Console output only level And above (onMatch), Other direct rejection (onMismatch)-->
<ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
</console>
<!-- The file will print out all the information , This log Every time the program is run, it will automatically empty , from append Attribute decision , Suitable for temporary testing -->
<File name="Filelog" fileName="${FILE_PATH}/test.log" append="false">
<PatternLayout pattern="${LOG_PATTERN}"/>
</File>
<!-- This will print out all the info And below , Each time the size exceeds size, Then this size The size of the log is automatically saved by year - Under the folder created in the month and compressed , As archive -->
<RollingFile name="RollingFileInfo" fileName="${FILE_PATH}/info.log" filePattern="${FILE_PATH}/${FILE_NAME}-INFO-%d{yyyy-MM-dd}_%i.log.gz">
<!-- Console output only level And above (onMatch), Other direct rejection (onMismatch)-->
<ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="${LOG_PATTERN}"/>
<Policies>
<!--interval Property is used to specify how often to scroll , The default is 1 hour-->
<TimeBasedTriggeringPolicy interval="1"/>
<SizeBasedTriggeringPolicy size="10MB"/>
</Policies>
<!-- DefaultRolloverStrategy Property if not set , The default is at most under the same folder 7 Files starting to overwrite -->
<DefaultRolloverStrategy max="15"/>
</RollingFile>
<!-- This will print out all the warn And below , Each time the size exceeds size, Then this size The size of the log is automatically saved by year - Under the folder created in the month and compressed , As archive -->
<RollingFile name="RollingFileWarn" fileName="${FILE_PATH}/warn.log" filePattern="${FILE_PATH}/${FILE_NAME}-WARN-%d{yyyy-MM-dd}_%i.log.gz">
<!-- Console output only level And above (onMatch), Other direct rejection (onMismatch)-->
<ThresholdFilter level="warn" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="${LOG_PATTERN}"/>
<Policies>
<!--interval Property is used to specify how often to scroll , The default is 1 hour-->
<TimeBasedTriggeringPolicy interval="1"/>
<SizeBasedTriggeringPolicy size="10MB"/>
</Policies>
<!-- DefaultRolloverStrategy Property if not set , The default is at most under the same folder 7 Files starting to overwrite -->
<DefaultRolloverStrategy max="15"/>
</RollingFile>
<!-- This will print out all the error And below , Each time the size exceeds size, Then this size The size of the log is automatically saved by year - Under the folder created in the month and compressed , As archive -->
<RollingFile name="RollingFileError" fileName="${FILE_PATH}/error.log" filePattern="${FILE_PATH}/${FILE_NAME}-ERROR-%d{yyyy-MM-dd}_%i.log.gz">
<!-- Console output only level And above (onMatch), Other direct rejection (onMismatch)-->
<ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="${LOG_PATTERN}"/>
<Policies>
<!--interval Property is used to specify how often to scroll , The default is 1 hour-->
<TimeBasedTriggeringPolicy interval="1"/>
<SizeBasedTriggeringPolicy size="10MB"/>
</Policies>
<!-- DefaultRolloverStrategy Property if not set , The default is at most under the same folder 7 Files starting to overwrite -->
<DefaultRolloverStrategy max="15"/>
</RollingFile>
</appenders>
<!--Logger The node is used to specify the form of the log separately , For example, to specify the package under class Specify different log levels, etc .-->
<!-- Then define loggers, Only defined logger And introduced appender,appender Will take effect -->
<loggers>
<!-- To filter out spring and mybatis Some of them are useless DEBUG Information -->
<logger name="org.mybatis" level="info" additivity="false">
<AppenderRef ref="Console"/>
</logger>
<!-- Monitoring system information -->
<!-- if additivity Set to false, be Son Logger Only in my own appender Internal output , Not in Father Logger Of appender Internal output .-->
<Logger name="org.springframework" level="info" additivity="false">
<AppenderRef ref="Console"/>
</Logger>
<root level="info">
<appender-ref ref="Console"/>
<appender-ref ref="Filelog"/>
<appender-ref ref="RollingFileInfo"/>
<appender-ref ref="RollingFileWarn"/>
<appender-ref ref="RollingFileError"/>
</root>
</loggers>
</configuration>
边栏推荐
- STS download tutorial (the solution cannot be downloaded on the include official website)
- 硬刚甲方后:中国移动 4908 万大单被废
- CLS 监控告警:实时保障线上服务高可用性
- 浪潮之巅——读书笔记+摘录+感悟
- Implicit indicators for evaluating the advantages and disadvantages of automated testing
- Plus版SBOM:流水线物料清单PBOM
- Sword finger offer notes: T53 - I. find numbers in the sorted array
- The first case of monkeypox in pregnant women in the United States: the newborn was injected with immunoglobulin and was safely born
- Introduction to box diagram
- Simple blockchain day based on bolt database (2)
猜你喜欢
![[untitled] multimodal model clip](/img/f0/8ae72ae0845372b6fe2866fae83f52.png)
[untitled] multimodal model clip

Fundamentals of mathematics 01

广东:剧本杀等新行业新业态场所,消防安全监管不再“缺位”

Go replace with local code

EfficientNet

Advance in the flutter project_ image_ Picker component usage

解决@OneToMany查询陷入循环引用问题

最强分布式锁工具:Redisson

Go Beginner (4)

How to make a graph? Multiple subgraphs in a graph are histogram (or other graphs)
随机推荐
Chapter 10 enumeration classes and annotations
NPM step pit
Unity shader - Laser special effect shader[easy to understand]
微信小程序必用接口「建议收藏」
Check the number of file descriptors opened by each process under the system
开关量输入输出模块DAM-5055
[product] about wechat product analysis
matlab二分法例题(用二分法求零点例题)
NewTicker使用
JVM memory model
基于bolt数据库实现简单的区块链 day(2)
浪潮之巅——读书笔记+摘录+感悟
2021-3-22-directed graph sorting
Principle, concept and construction process of MySQL database master-slave replication cluster
Ali II: what if the AOF file in redis is too large?
Sword finger offer note: T39. Numbers that appear more than half of the time in the array
Sword finger offer notes: t57 - I. and two numbers of S
MySQL paging query instance_ MySQL paging query example explanation "suggestions collection"
硬刚甲方后:中国移动 4908 万大单被废
Go replace with local code