log4j2-每日RollingFile附加程序的DefaultRolloverStrategy,最大值不受尊重 [英] log4j2 - DefaultRolloverStrategy for daily RollingFile appender, max not respected

查看:86
本文介绍了log4j2-每日RollingFile附加程序的DefaultRolloverStrategy,最大值不受尊重的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个每日滚动策略的追加程序,该策略每天最多可容纳10个文件,无论何时.

I am trying to write an appender with a daily rolling policy with 10 maximum total files no matter the day.

每10 MB,它将创建一个新的日志文件,并将其与今天的日期和索引一起存储.

Every 10 MB it creates a new log file and stores it with today's date and an index.

这是我到目前为止所拥有的:

This is what I have so far:

<RollingFile name="MyRollingFile" fileName="./log/logs.log"
            filePattern="./log/logs-%d{yyyy-MM-dd}-%i.log">
            <PatternLayout>
                <pattern>%n%d{yy-MM-dd HH:mm:ss.SSS} [%p] %m</pattern>
            </PatternLayout>
            <Policies>
                <SizeBasedTriggeringPolicy size="10MB" />
            </Policies>
            <DefaultRolloverStrategy max="10" />
        </RollingFile>

这是不正确的,因为这每天最多可保存十个文件 ,但是我希望总共最多保存十个文件 .所以很明显,十天后我发现多达100个文件.我想将最大文件总数限制为10个.我该怎么办?

It is not correct because this saves a maximum of ten files every single day but I want ten maximum files in total. So obviously I find up to 100 files after 10 days. I want to limit the total file maximum to 10. How could I do that?

推荐答案

请参阅

See the Delete operation for the DefaultRolloverStrategy documented at http://logging.apache.org/log4j/2.x/manual/appenders.html#RollingFileAppender. In short what you want is

<DefaultRolloverStrategy max="10">
  <Delete basePath="./logs">
      <IfFileName glob="*/logs-*.log" />
      <IfAccumulatedFileCount exceeds="10" />
    </Delete>
</DefaultRolloverStrategy>

我还应该指出,由于您没有基于时间的过渡策略,因此filePattern中的日期将始终包含配置Log4j的时间,并且索引将不断递增.您需要添加TimeBasedTriggeringPolicy或CronTriggeringPolicy才能每天更改日期.

I should also point out that since you do not have a time-based rollover policy the date in your filePattern is always going to contain the time of when Log4j configured and the index will endlessly increment. You need to add either the TimeBasedTriggeringPolicy or CronTriggeringPolicy to have the date change every day.

这篇关于log4j2-每日RollingFile附加程序的DefaultRolloverStrategy,最大值不受尊重的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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