Log4j编程配置 DefaultRolloverStrategy

huangapple go评论97阅读模式
英文:

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));

huangapple
  • 本文由 发表于 2020年9月21日 18:22:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/63990386.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定