章
目
录
Log4j2的RollingFileAppender是一种OutputStreamAppender,它将日志消息写入文件,并按照配置的触发策略来确定何时进行滚动(备份)。它还具有配置的滚动策略,以决定如何滚动文件。
一般来说,日志文件的备份是基于文件大小、当前日期或两者的组合创建的。
1. Log4j2 Maven依赖项
2. SizeBasedTriggeringPolicy – 基于日志文件大小的滚动策略
这种给定的配置基于日志文件大小来进行日志滚动。我已经将日志文件大小配置为10 MB,您可以根据需要进行更改。
2.1 属性配置
我们可以在log4j.properties中以给定的方式配置RollingFileAppender。
log4j.properties:
name = PropertiesConfig
appender.rolling.type = RollingFile
appender.rolling.name = RollingFile
appender.rolling.fileName = ${LOG_DIR}/application.log
appender.rolling.filePattern = ${LOG_DIR}/application.%d{dd-MMM}.log.gz
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %m%n
appender.rolling.policies.type = Policies
appender.rolling.policies.size.type = SizeBasedTriggeringPolicy
appender.rolling.policies.size.size=10MB
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.max = 5
appender.rolling.strategy.action.type = Delete
appender.rolling.strategy.action.basepath = ${LOG_DIR}
appender.rolling.strategy.action.maxdepth = 2
appender.rolling.strategy.action.condition.type = IfLastModified
appender.rolling.strategy.action.condition.age = 30D
appender.rolling.strategy.action.PathConditions.type = IfFileName
appender.rolling.strategy.action.PathConditions.glob = */app-*.log.gz
appender.rolling.filter.threshold.type = ThresholdFilter
appender.rolling.filter.threshold.level = DEBUG
logger.rolling.name = rollingFile
logger.rolling.level = debug
logger.rolling.additivity = false
logger.rolling.appenderRef.rolling.ref = RollingFile
2.2 XML配置
log4j2.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="MyApp" packages="">
<Properties>
<Property name="LOG_DIR">/logs</Property>
</Properties>
<Appenders>
<RollingFile
name="rollingFile"
fileName="${LOG_DIR}/application.log"
filePattern="${LOG_DIR}/application.%i.log.gz"
ignoreExceptions="false">
<PatternLayout>
<Pattern>%d{yyyy-MM-dd HH:mm:ss} %-5p %m%n</Pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="10MB" />
</Policies>
<DefaultRolloverStrategy max="5">
<Delete basePath="${LOG_DIR}" maxDepth="2">
<IfFileName glob="*/app-*.log.gz" />
<IfLastModified age="P30D" />
</Delete>
</DefaultRolloverStrategy>
</RollingFile>
</Appenders>
<Loggers>
<Root level="debug">
<AppenderRef ref="rollingFile"/>
</Root>
</Loggers>
</Configuration>
3. TimeBasedTriggeringPolicy – 基于日期和时间的滚动策略
我们也可以根据日期和时间来进行日志滚动。
3.1 每小时滚动一次
如果使用RollingFileAppender,请使用TimeBasedRollingPolicy来指定基于日期和时间的滚动策略。
注意FileNamePattern属性。它定义了已滚动文件的名称模式。在给定的示例中,它将使用日期月份来重命名日志文件中的回滚文件。
例如,模式'{MM-dd-yyyy-HH}’将每小时滚动一次日志文件。
我们还使用.gz扩展名,因此log4j将自动压缩日志文件。
<RollingFile
name="rollingFile"
fileName="${LOG_DIR}/application.log"
filePattern="${LOG_DIR}/application.%d{dd-MMM}.log.gz"
ignoreExceptions="false">
<PatternLayout>
<Pattern>%d{yyyy-MM-dd HH:mm:ss} %-5p %m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy filePattern="${LOG_DIR}/application.%d{dd-MMM-hh}.log.gz" />
</Policies>
<DefaultRolloverStrategy max="5" />
</RollingFile>
3.2 每天滚动一次
要启用每日滚动,log4j2不再使用早期log4j中存在的DailyRollingFileAppender。要每天进行日志滚动,请在TimeBasedTriggeringPolicy中将间隔设置为1。
<RollingFile
name="rollingFile"
fileName="${LOG_DIR}/application.log"
filePattern="${LOG_DIR}/application.%d{dd-MMM}.log.gz"
ignoreExceptions="false">
<PatternLayout>
<Pattern>%d{yyyy-MM-dd HH:mm:ss} %-5p %m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1"/>
</Policies>
<DefaultRolloverStrategy max="5" />
</RollingFile>
4. 基于日志大小和日期的滚动策略
如果我们想要根据文件大小和日期时间进行日志滚动,那么我们需要同时使用SizeBasedTriggeringPolicy和TimeBasedRollingPolicy。
在给定的示例中,appender能够引用文件名模式和基于时间的滚动策略,使用filePattern属性包括{dd-MMM}。基于大小的滚动将在10 MB时发生。
<RollingFile
name="rollingFile"
fileName="${LOG_DIR}/application.log"
filePattern="${LOG_DIR}/application.%d{dd-MMM}.log.gz"
ignoreExceptions="false">
<PatternLayout>
<Pattern>%d{yyyy-MM-dd HH:mm:ss} %-5p %m%n</Pattern>
</PatternLayout>
<Policies>
<OnStartupTriggeringPolicy />
<SizeBasedTriggeringPolicy size="10 MB" />
<TimeBasedTriggeringPolicy />
</Policies>
<DefaultRolloverStrategy max="5" />
</RollingFile>
5. 在滚动时删除日志
Log4j 2.5 引入了一项删除操作,以便在滚动时对要删除的文件有更多的控制权。
以下示例中,在基础目录下的所有与 “/app-.log.gz” 匹配的文件,如果它们已经存在 30 天或更长时间,那么在滚动时将被删除。
<Properties>
<Property name="baseDir">/logs</Property>
</Properties>
<DefaultRolloverStrategy>
<Delete basePath="${baseDir}" maxDepth="2">
<IfFileName glob="*/app-*.log.gz" />
<IfLastModified age="P30D" />
</Delete>
</DefaultRolloverStrategy>
在版本 2.5 之前,只有使用默认的 RolloverStrategy 才可能删除文件。
<DefaultRolloverStrategy max="5" />
请注意,我们可以同时使用这两种策略。