如何解决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));
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。