log4j2.xml总共分为三大块:Properties,Appenders,Loggers.
<?xml version="1.0" encoding="UTF-8"?>
<configuration monitorInterval="30" shutdownHook="disable">
<Properties>
<Property name="baseDir">../log</Property>
<Property name="impLogFileName">../log/imp.log</Property>
<Property name="limitedLogFileName">../log/limited.log</Property>
<Property name="impLogDeleteGlob">*/imp-*.log.gz</Property>
<Property name="limitedDeleteGlob">*/limited-*.log.gz</Property>
/*如上 Property 定义log文件名,包含相对路径*/
/*如下 Appenders 定义了 Property 中各个log文件的打印规范和限制*/
<Appenders>
<Console name="stdout" target="System.out">
<PatternLayout pattern="%d{ISO8601} %sn [%t] %-5p %c - %m%n"/>
</Console>
<RollingRandomAccessFile name="impLogFileAppender" fileName="${impLogFileName}"
filePattern="${impLogFilePatten}" immediateFlush="false">
<PatternLayout pattern="${impLogLayout}"/>
<SizeBasedTriggeringPolicy size="100MB"/>
<DefaultRolloverStrategy> /*超过限制要被删掉的文件*/
<Delete basePath="${baseDir}" maxDepth="2">
<IfFileName glob="${impLogDeleteGlob}">
<IfAccumulatedFileCount exceeds="30"/>
</IfFileName>
</Delete>
</DefaultRolloverStrategy>
</RollingRandomAccessFile>
<RollingRandomAccessFile name="limitedLogFileAppender" fileName="${limitedLogFileName}"
filePattern="${impLogFilePatten}" immediateFlush="false">
<PatternLayout pattern="${impLogLayout}"/>
/*<=ERROR级别的log受到filter的限制,平均每秒打印 <10,排队数量不超过100*/
<BurstFilter level="ERROR" rate="10" maxBurst="100"/>
<SizeBasedTriggeringPolicy size="100MB"/>
<DefaultRolloverStrategy>
<Delete basePath="${baseDir}" maxDepth="2">
<IfFileName glob="${limitedDeleteGlob}">
<IfAccumulatedFileCount exceeds="30"/>
</IfFileName>
</Delete>
</DefaultRolloverStrategy>
</RollingRandomAccessFile>
</Appenders>
/*以下 Loggers 定义了工程代码中的log输出到哪些文件*/
<Loggers>
<AsyncLogger name="com.nokia.imp.conversion.impl.domains.ParameterConverter" level="ERROR" additivity="false">
<AppenderRef ref="limitedLogFileAppender" />
</AsyncLogger>
<root level="INFO"> /*未定义的默认输出到 root定义中的 appender*/
<appender-ref ref="impLogFileAppender"/>
<appender-ref ref="errorAppender"/>
</root>
</Loggers>
网友评论