Log4j2之RollingFileAppender学习笔记

来源:互联网 发布:mac文件夹属性隐藏 编辑:程序博客网 时间:2024/06/07 07:23

RollingFileAppender

The RollingFileAppender is an OutputStreamAppender that writes to the File named in the fileName parameter and rolls the file over according the TriggeringPolicy and the TolloverPolicy. The RollingFileAppender uses a RollingFileManager to actually perform the file I/O and perform the rollover.
A RollingFileAppender requires a TriggeringPolicy and a RolloverStrategy. The triggering policy determines if a rollover should be performed while the RolloverStrategy defines how the rollover should be done.

Triggering Policies

Composite Triggering Policy

The CompositeTriggeringPolicy combines multiple triggering policies and returns true if any of the configured policies return true. The CompositeTriggeringPolicy is configured simply by wrapping other policies in a Policies element.
Cron Triggering Policy
The CronTriggeringPolicy triggers rollover based on a cron expression.

CronTriggeringPolicy Parameters

Parameter Name Type Description schedule String The cron expression. The expression is the same as what is allowed in the Quartz scheduler. See CronExpression for a full description of the expression. evaluateOnStartup boolean On startup the cron expression will be evaluated against the file’s last modification timestamp. If the cron expression indicates a rollover should have occurred between that time and the current time the file will be immediately rolled over.

OnStartup Triggering Policy

The OnStartupTriggeringPolicy policy causes a rollover if the log file is older than the current JVM’s start time and the minimum file size if met or exceeded.

OnStartupTriggeringPolicy Parameters

Parameter Name Type Description minSize long The minimum size the file must have to rollover. A size of zero will cause a roll over no matter what the file size is. The default value is 1, which will prevent rolling over an empty file.

SizeBased Triggering Policy
The SizeBasedTriggeringPolicy causes a rollover once the file has reached the specified size. The size can be specified in bytes, with the suffix KB, MB, or GB for example 20MB.

TimeBased Triggering Policy
The TimeBasedTriggeringPolicy causes a rollover once the date/time pattern no longer applies to the active file. This policy accepts an interval attribute which indicates how frequently the rollover should occur based on the time pattern and a modulate boolean attribute.

TimeBasedTriggeringPolicy Parameters

Parameter Name Type Description interval integer How often a rollover should occur based on the most specific time unit in the date pattern. For example, with a date pattern with hours as the most specific item and increment of 4 rollovers would occur every 4 hours. The default value is 1. modulate boolean Indicates whether the interval should be adjusted to cause the next rollover to occur on the interval boundary. For example, if the item is hours, the current hour is 3 am and the interval is 4 then the first rollover will occur at 4 am and then next ones will occur at 8am, noon, 4pm, etc. maxRandomDelay integer Indicates the maximum number of seconds to randomly delay a rollover. By default, this is 0 which indicates no delay. This setting is useful on serers where multiple applications are configured to rollover log files at the same time and can spread the load of doing so across time.
原创粉丝点击