A.R
A.R

Reputation: 45

Log4j Programmatic Configuration DefaultRolloverStrategy

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

Upvotes: 0

Views: 683

Answers (1)

Vishal
Vishal

Reputation: 724

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

Upvotes: 2

Related Questions