Log4j2 DefaultRolloverStrategy的max属性如何真正起作用? [英] How does Log4j2 DefaultRolloverStrategy's max attribute really work?

查看:1464
本文介绍了Log4j2 DefaultRolloverStrategy的max属性如何真正起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只配置了OnStartupTriggeringPolicy,但配置了RollingRandomAccessFileAppender,但是当我将DefaultRolloverStrategy的max属性设置为某个数字时,日志将无限期地超过该数量.

I've configured a RollingRandomAccessFileAppender with only the OnStartupTriggeringPolicy set, but when I set the max attribute of the DefaultRolloverStrategy to some number, the logs keep generating past that amount indefinitely.

这是我的 log4j2.xml :

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </Console>
        <RollingRandomAccessFile 
            name="RollingRAF" 
            fileName="logs/app.log"
            filePattern="logs/app-%d{dd-MMM-yyyy@HH.mm.ss}.log">
            <PatternLayout>
                <Pattern>%d %p %c{1.} %m%n</Pattern>
            </PatternLayout>
            <Policies>
                <OnStartupTriggeringPolicy />
            </Policies>
            <DefaultRolloverStrategy max="5"/>
        </RollingRandomAccessFile>
    </Appenders>
    <Loggers>
        <Logger name="myLogger" level="warn">
            <AppenderRef ref="RollingRAF"/>
        </Logger>
        <Root level="error">
            <AppenderRef ref="Console"/>
        </Root>
    </Loggers>
</Configuration>

是因为我的姓名模式中没有迭代器吗?

Is it because I don't have an iterator in my name pattern?

是因为我的文件名精度设置为秒?

Is it because my file name precision is set to seconds?

是因为只设置了OnStartupTriggeringPolicy吗?

或者这是怎么回事?

我在这里的目标是建立一个滚动配置,该配置将记录最近5个应用程序的运行情况.

My goal here was to set up a rolling configuration that will log the last 5 application runs.

推荐答案

如果指定了TimeBasedTriggeringPolicy,则DefaultRolloverStrategy将使用filePattern中指定的日期模式.要使用max属性,请在filePattern中指定一个%i模式,然后将<SizeBasedTriggeringPolicy size="20 MB" />添加到过渡策略中. (当然还有其他一些大小.)

The DefaultRolloverStrategy will use the date pattern specified in the filePattern if a TimeBasedTriggeringPolicy is specified. To use the max attribute, specify a %i pattern in the filePattern, and add <SizeBasedTriggeringPolicy size="20 MB" /> to the rollover policies. (Or some other size of course.)

然后,<DefaultRolloverStrategy max="5"/>中的max值将确保在相同的过渡期内(由于您将日期格式指定为%d{dd-MMM-yyyy@HH.mm.ss},因此需要一秒钟),基于大小的过渡不会创建超过5个文件被触发了.

The value for max in <DefaultRolloverStrategy max="5"/> will then ensure that within the same rollover period (one second for you since you specified a date pattern of %d{dd-MMM-yyyy@HH.mm.ss}) no more than 5 files will be created when a size-based rollover was triggered.

如果您的过渡窗口较长(例如每天都滚动到一个新文件夹,并且在该文件夹中,请确保最多创建20个最大文件大小为20 MB的文件),此功能将更为有用.

This is more useful if your rollover window is longer, like rolling over to a new folder every day, and within that folder, ensure that no more than 5 files are created with max size=20 MB.

更新:

Log4j 2.5添加了配置自定义删除操作的功能.开箱即用,您可以根据使用期限,数量或它们占用的磁盘空间(累积的文件大小)删除文件.

Log4j 2.5 added the ability to configure custom delete actions. Out of the box you can delete files based on age, count or how much disk space they take up (accumulated file size).

这篇关于Log4j2 DefaultRolloverStrategy的max属性如何真正起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆