【问题标题】:How does Wildfly know that a number of files need to be deleted if the "max-backup-index" attribute has been configured?如果配置了“max-backup-index”属性,Wildfly 如何知道需要删除多个文件?
【发布时间】:2015-07-26 12:40:13
【问题描述】:

为了实现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 代码,以了解哪些代码负责读取 &lt;max-backup-index value="10"/&gt; sn-p 并删除多余的日志文件。

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 之间交互的代码。

【问题讨论】:

    标签: java xml oop logging wildfly


    【解决方案1】:

    您需要查看 JBoss Log Manager 而不是 WildFly。 WildFly 只是将所有内容委托给日志管理器。具体来说,您正在寻找的是this

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

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

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

    【讨论】:

      【解决方案2】:

      您可以将“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>
      

      【讨论】:

        猜你喜欢
        • 2020-11-06
        • 1970-01-01
        • 2020-09-06
        • 2019-11-04
        • 2011-05-02
        • 2023-02-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多