如果设置了 "max-backup-index" 属性,Wildfly 如何知道需要删除多个文件?

How does Wildfly know that a number of files need to be deleted if the "max-backup-index" attribute has been configured?

为了实现 this issue,即 periodic-rotating-file-handlermax-backup-index 功能,我试图了解 max-backup-index 属性在 size-rotating-file-handler 中的工作原理.

尝试次数

Wildfly 读取xml配置:

<size-rotating-file-handler name="FILE" autoflush="true">
    <level name="INFO"/>
    <formatter>
        <pattern-formatter pattern="%d{HH:mm:ss,SSS} %-5p [%c] (%t) %s%E%n"/>
    </formatter>
    <file relative-to="jboss.server.log.dir" path="server.log"/>
    <rotate-size value="100K"/>
    <max-backup-index value="10"/>
    <append value="true"/>
</size-rotating-file-handler>

如果日志文件的数量大于 10 个,Wildfly 将删除文件直到剩下 10 个。

已查看驻留在 Github 上的 Wildfly 代码,以了解哪些代码负责读取 <max-backup-index value="10"/> 片段并删除多余的日志文件。

PeriodicSizeRotatingHandlerResourceDefinition

class PeriodicSizeRotatingHandlerResourceDefinition extends AbstractFileHandlerDefinition {

    public static final String PERIODIC_SIZE_ROTATING_FILE_HANDLER = "periodic-size-rotating-file-handler";
    static final PathElement PERIODIC_SIZE_ROTATING_HANDLER_PATH = PathElement.pathElement(PERIODIC_SIZE_ROTATING_FILE_HANDLER);    

    static final AttributeDefinition[] ATTRIBUTES = Logging.join(DEFAULT_ATTRIBUTES, AUTOFLUSH, APPEND, MAX_BACKUP_INDEX, ROTATE_SIZE, ROTATE_ON_BOOT, SUFFIX, NAMED_FORMATTER, FILE);

    public PeriodicSizeRotatingHandlerResourceDefinition(final ResolvePathHandler resolvePathHandler) {
        super(PERIODIC_SIZE_ROTATING_HANDLER_PATH, false, PeriodicSizeRotatingFileHandler.class, resolvePathHandler, ATTRIBUTES);
    }

super 指超类的构造函数,即AbstractFileHandlerDefinition

abstract class AbstractFileHandlerDefinition extends AbstractHandlerDefinition {
    protected AbstractFileHandlerDefinition(final PathElement path, final boolean registerLegacyOps,
                                            final Class<? extends Handler> type,
                                            final ResolvePathHandler resolvePathHandler,
                                            final AttributeDefinition... attributes) {
        super(path, registerLegacyOps, type, new DefaultPropertySorter(FileNameLastComparator.INSTANCE), attributes);
        this.registerLegacyOps = registerLegacyOps;
        this.resolvePathHandler = resolvePathHandler;
    }

super指的是AbstractHandlerDefinition

abstract class AbstractHandlerDefinition extends TransformerResourceDefinition {

    protected AbstractHandlerDefinition(final PathElement path,
                                        final boolean registerLegacyOps,
                                        final Class<? extends Handler> type,
                                        final PropertySorter propertySorter,
                                        final AttributeDefinition[] attributes) {
        this(path, registerLegacyOps, type, propertySorter, attributes, null, attributes);
    }

当前结果

此刻感觉自己迷失在了迷宫中。我找不到解释 max-backup-index 属性和 size-rotating-file-handler.

之间交互的代码

您需要查看 JBoss Log Manager not WildFly. WildFly just delegates everything to the log manager. Specifically what you're looking for is this

也就是说这是一个棘手的问题。没有一个好的可靠方法来确定应该删除哪些文件。使用日期后缀,您只能做出最好的猜测,这不是很好。您不希望日志轮换删除您有意保存的文件。

例如,假设您有一个 server.log.2015-01-01 和一个 server.log.2015-01-01.save。你怎么知道只删除第一个?我们不想删除第二个,因为它可能已被用户重命名。

还有性能需要考虑。当复杂的文件匹配算法是 运行 时,您不想阻塞。默认情况下,日志记录是阻塞的,因此任何尝试记录的代码都将被阻塞,直到轮换完成。

您可以将 "periodic-size-rotating-file-handler" 配置为:

  1. 时间戳文件名(后缀)
  2. 达到一定尺寸后旋转(rotate-size)
  3. 达到一定数量的文件后删除最后一个文件(最大备份索引)

例如:

<periodic-size-rotating-file-handler name="FILE" autoflush="true">
    <formatter>
        <named-formatter name="PATTERN"/>
    </formatter>
    <file relative-to="jboss.server.log.dir" path="server.log"/>
    <suffix value=".yyyy-MM-dd"/>
    <max-backup-index value="10"/>
    <rotate-size value="5m"/>
    <append value="false"/>
</periodic-size-rotating-file-handler>