Java Log4j2 DefaultRolloverStrategy 的 max 属性如何真正起作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24551768/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 12:55:22  来源:igfitidea点击:

How does Log4j2 DefaultRolloverStrategy's max attribute really work?

javaloggingconfigurationlog4j2rollingfileappender

提问by Ceiling Gecko

I've configured a RollingRandomAccessFileAppenderwith only the OnStartupTriggeringPolicyset, but when I set the max attribute of the DefaultRolloverStrategyto some number, the logs keep generating past that amount indefinitely.

RollingRandomAccessFileAppender只用OnStartupTriggeringPolicyset配置了 a ,但是当我将 the 的 max 属性设置DefaultRolloverStrategy为某个数字时,日志会无限期地生成超过该数量。

Here's my log4j2.xml:

这是我的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{[email protected]}.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?

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

Is it because I only have the OnStartupTriggeringPolicyset?

是因为我只有OnStartupTriggeringPolicy套装吗?

Or what's going on here?

或者这里发生了什么?

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

我的目标是设置一个滚动配置,记录最近 5 次应用程序运行。

回答by Remko Popma

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

如果指定了 TimeBasedTriggeringPolicy,则 DefaultRolloverStrategy 将使用 filePattern 中指定的日期模式。要使用该max属性,请%i在 filePattern 中指定一个模式,并将其添加<SizeBasedTriggeringPolicy size="20 MB" />到翻转策略中。(当然也可以是其他尺寸。)

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{[email protected]}) no more than 5 files will be created when a size-based rollover was triggered.

max in 的值<DefaultRolloverStrategy max="5"/>将确保在相同的滚动周期内(自您指定日期模式为 1 秒%d{[email protected]})当触发基于大小的滚动时创建的文件不超过 5 个。

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.

如果您的滚动窗口更长,这将更有用,例如每天滚动到一个新文件夹,并确保在该文件夹内创建的文件不超过 5 个,最大大小为 20 MB。



Update:

更新:

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).

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