英文:
Log4j Programmatic Configuration DefaultRolloverStrategy
问题
我使用了这个参考链接来进行 Log4j 的编程配置:
https://logging.apache.org/log4j/2.x/manual/customconfig.html
但是我不知道如何在 appender 上配置 "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"));
或者将组件添加到 Policies 中:
.addComponent(builder.newComponent("DefaultRolloverStrategy").addAttribute("max", "2"))
但是都没有生效,并且我找不到任何文档说明。
英文:
I used this reference to Programmatic Configuration of Log4j
https://logging.apache.org/log4j/2.x/manual/customconfig.html
but i don't know how to config "DefaultRolloverStrategy" on appender
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);
I tried
ComponentBuilder strategy = builder.newComponent("Strategy")
.addComponent(builder.newComponent("DefaultRolloverStrategy").addAttribute("max", "2"));
or adding component to Polices
.addComponent(builder.newComponent("DefaultRolloverStrategy").addAttribute("max", "2"))
but not working and I couldn't find any documention
答案1
得分: 1
你可以像这样向你的appenderBuilder
添加DefaultRolloverStrategy
:
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));
英文:
You can add DefaultRolloverStrategy
like this to your 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));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论