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="moErrorLogFileName">../log/moError.log</Property>
<Property name="impLogDeleteGlob">*/imp-*.log.gz</Property>
<Property name="moErrorDeleteGlob">*/moError-*.log.gz</Property>
<Property name="moErrorLogFilePatten">../log/%d{yyyy-MM}/moError-%d{MM-dd-yyyy_HH.mm.ss}.log.gz</Property>
</Properties>
/*如上 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="moErrorLogFileAppender" fileName="${moErrorLogFileName}"
filePattern="${impLogFilePatten}" immediateFlush="false">
<PatternLayout pattern="${impLogLayout}"/>
/*<=ERROR级别的log受到filter的限制,平均每秒打印 <10,排队数量不超过100*/
<BurstFilter level="ERROR" rate="10" maxBurst="100"/>
<SizeBasedTriggeringPolicy size="100MB"/> //一个文件超过 100M 就产生一个 gz
<DefaultRolloverStrategy>
<Delete basePath="${baseDir}" maxDepth="2">
<IfFileName glob="${moErrorDeleteGlob}">
<IfAccumulatedFileCount exceeds="30"/> // gz 文件超过30个就覆盖
</IfFileName>
</Delete>
</DefaultRolloverStrategy>
</RollingRandomAccessFile>
</Appenders>
/*以下 Loggers 定义了工程代码中的log输出到哪些文件*/
<Loggers>
<AsyncLogger name="com.nokia.imp.conversion.impl.domains.ParameterConverter" level="ERROR" additivity="false">
<AppenderRef ref="moErrorLogFileAppender" />
</AsyncLogger>
<root level="INFO"> /*未定义的默认输出到 root定义中的 appender*/
<appender-ref ref="impLogFileAppender"/>
<appender-ref ref="errorAppender"/>
</root>
</Loggers>