Log4j程序化配置DefaultRolloverStrategy

问题描述

我将此参考用于Log4j的程序配置

https://logging.apache.org/log4j/2.x/manual/customconfig.html

但是我不知道如何在追加程序上配置“ DefaultRolloverStrategy”

LayoutComponentBuilder layoutBuilder = builder.newLayout("PatternLayout")
                    .addAttribute("pattern","%d{yyyy-MM-dd HH:mm:ss} %t - %-5p %c{1}:%L - %m%n");
            ComponentBuilder triggeringPolicy = builder.newComponent("Policies")
                    .addComponent(builder.newComponent("SizeBasedTriggeringPolicy").addAttribute("size","50M"));



          AppenderComponentBuilder appenderBuilder = builder.newAppender("file","RollingFile")
                    .addAttribute("fileName",logPath + "/isf_tms_1.log")
                    .addAttribute("filePattern",logPath + "/archive/isf_tms_1-%d{MM-dd-yy}-%i.log.gz")

                    .add(layoutBuilder)
                    .addComponent(triggeringPolicy);
            builder.add(appenderBuilder);

我尝试过

ComponentBuilder strategy = builder.newComponent("Strategy")
                    .addComponent(builder.newComponent("DefaultRolloverStrategy").addAttribute("max","2"));

或将组件添加到策略

.addComponent(builder.newComponent("DefaultRolloverStrategy").addAttribute("max","2"))

但无法正常工作,我找不到任何文档

解决方法

您可以将DefaultRolloverStrategy这样添加到您的appenderBuilder

   AppenderComponentBuilder appenderBuilder = builder.newAppender("file","RollingFile")
                .addAttribute("fileName",logPath + "/isf_tms_1.log")
                .addAttribute("filePattern",logPath + "/archive/isf_tms_1-%d{MM-dd-yy}-%i.log.gz")

                .add(layoutBuilder)
                .addComponent(triggeringPolicy)
                .addComponent(builder.newComponent("DefaultRolloverStrategy").addAttribute("max",2));