Log4j2的DefaultRolloverStrategy详解

时间:2025-03-22 21:11:02

目录

  • 什么是 DefaultRolloverStrategy?
    • DefaultRolloverStrategy节点
    • Delete节点

什么是 DefaultRolloverStrategy?

当触发器(例如SizeBasedTriggeringPolicyTimeBasedTriggeringPolicy)决定需要进行日志文件滚动时,DefaultRolloverStrategy将负责处理滚动过程中的具体行为。

<RollingFile name="api_json"
             fileName="${LOG_HOME}/${FILE_NAME}.log"
             filePattern="${LOG_HOME}/${FILE_NAME}_%d{yyyy-MM-dd-HH}_%"
             createOnDemand="true">
    <ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
    <PatternLayout pattern="${LOG_PATTERN}"/>
    <Policies>
        <SizeBasedTriggeringPolicy size="100K"/>
    </Policies>

    <DefaultRolloverStrategy fileIndex="nomax">
        <Delete basePath="${LOG_HOME}" maxDepth="2">
            <IfFileName glob="*.">
                <IfAny>
                    <IfAccumulatedFileSize exceeds="100M"/>
                    <IfAccumulatedFileCount exceeds="100"/>
                    <IfLastModified age="30d"/>
                </IfAny>
            </IfFileName>
        </Delete>
    </DefaultRolloverStrategy>
</RollingFile>

DefaultRolloverStrategy节点

参数 Type Description
fileIndex String 可选值 max, min,nomax
max:删小留大
min:删大留小
nomax:没上限都保留
min integer The minimum value of the counter. The default value is 1.
max integer The maximum value of the counter. The default value is 7.
一旦达到这个值,旧的存档将在随后的滚动中被删除。
compressionLevel integer 设置压缩级别,0-9,其中0→无,1→最佳速度,到9→最佳压缩。仅对ZIP文件实现。
tempCompressedFilePattern String 归档日志文件在压缩过程中的文件名模式。
<DefaultRolloverStrategy fileIndex="max" max="3" min="1">:
只保留3个日志文件,当到归档第4个文件时,
 被删除,
 被重命名为 ,
 被重命名为 ,
   被重命名为 。
创建新的   文件并继续写入。
<DefaultRolloverStrategy fileIndex="min" max="3" min="1">:
只保留3个日志文件,当到归档第4个文件时,
 被删除,
 重命名为,
 重命名为,
   重命名为。
创建新的   文件并继续写入。
<DefaultRolloverStrategy fileIndex="nomax">
这里的fileIndex="nomax"属性值表示文件索引没有最大限制。
换句话说,当使用%i作为文件名中的占位符时,日志文件将无限滚动,不会因为索引达到某个上限而停止创建新的日志文件。

Delete节点

参数 Type Description
basePath String Required. Base path from where to start scanning for files to delete.
maxDepth int 1、The maximum number of levels of directories to visit.
1.1、0 Log4j2只会检查${LOG_HOME}直接指向的目录下的.格式文件是否符合删除条件,
1.2、Integer.MAX_VALUE indicates that all levels should be visited.
1.3、The default is 1,Log4j2不仅会检查${LOG_HOME}指向的目录下的.格式文件是否符合删除条件,还会检查该目录下1层的所有子目录中的相同格式文件。
pathConditions PathCondition[] PathCondition如果进行嵌套,则是先判断外层的PathCondition,然后进行内层的判断。如果没有嵌套,则是按顺序进行判断。

也可以创建自定义条件或使用内置条件:
IfFileName 如果文件名与此参数匹配则结果为true,此参数为正则表达式或 glob的文件。
IfLastModified 最后修改时间早于或等于此参数则结果为true,此参数为duration。
IfAccumulatedFileCount 文件数超过指定个数则结果为true,此参数为整型。
IfAccumulatedFileSize 所有文件总大小达到此参数则结果为true,此参数为KB、MB、GB。

IfAll 如果此标签下的所有条件都配置成功(逻辑与),则结果为true。
IfAny 如果此标签下的任何一个条件匹配成功(逻辑或),则结果为true。
IfNot 如果此标签下的所有条件都不匹配(逻辑非),则结果为true。
<DefaultRolloverStrategy fileIndex = "nomax">
    <Delete basePath="${LOG_HOME}" maxDepth="2">
        <IfFileName glob="*.">
            <IfLastModified age="7d">
                <IfAny>
                    <IfAccumulatedFileSize exceeds="100M"/>
                    <IfAccumulatedFileCount exceeds="100"/>
                </IfAny>
            </IfLastModified>
        </IfFileName>
    </Delete>
</DefaultRolloverStrategy>
<DefaultRolloverStrategy fileIndex = "nomax">
    <Delete basePath="${LOG_HOME}" maxDepth="2">
        <IfFileName glob="*.">
            <IfAny>
                <IfAccumulatedFileSize exceeds="100M"/>
                <IfAccumulatedFileCount exceeds="100"/>
                <IfLastModified age="30d"/>
            </IfAny>
        </IfFileName>
    </Delete>
</DefaultRolloverStrategy>