You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-user@logging.apache.org by as...@hotmail.com, as...@hotmail.com on 2020/02/22 16:59:16 UTC

How to programmatically set a rolloverStrategy?

Hello everyone,
coming from the old log4j I'm trying to migrate our XML configuration into just code.
Sadly I cannot find any info about how to set a rolloverStrategy programmatically.
I am using this code to initialize a mainLogBuilder.

LayoutComponentBuilder layoutBuilder = builder.newLayout("PatternLayout").addAttribute("pattern", LOG_PATTERN);
ComponentBuilder triggeringRollFile = builder.newComponent("Policies").addComponent(builder.newComponent("SizeBasedTriggeringPolicy").addAttribute("size", LOG_SIZE));

builder.add(builder.newFilter("ThresholdFilter", Filter.Result.ACCEPT, Filter.Result.NEUTRAL).addAttribute("level", Level.INFO));
AppenderComponentBuilder mainLogBuilder = builder
        .newAppender("mainLog", "RollingFile")
        .addAttribute("fileName", "log/main.log")
        .addAttribute("filePattern", "log/main.%i.log")
        .add(layoutBuilder)
        .addComponent(triggeringRollFile);

But I want to set the max number of backup files to 5. How do I do this?

Thanks in advance

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


Re: How to programmatically set a rolloverStrategy?

Posted by as...@hotmail.com, as...@hotmail.com.
Ok I found out how to do that.

Its just a component builder added to the AppenderBuilder:

ComponentBuilder rollOverStrategy = builder.newComponent("DefaultRolloverStrategy").addAttribute("max", 5);
...
mainLogBuilder .addComponent(rollOverStrategy)

On 2020/02/22 16:59:16, astaldo1977@hotmail.com <as...@hotmail.com> wrote: 
> Hello everyone,
> coming from the old log4j I'm trying to migrate our XML configuration into just code.
> Sadly I cannot find any info about how to set a rolloverStrategy programmatically.
> I am using this code to initialize a mainLogBuilder.
> 
> LayoutComponentBuilder layoutBuilder = builder.newLayout("PatternLayout").addAttribute("pattern", LOG_PATTERN);
> ComponentBuilder triggeringRollFile = builder.newComponent("Policies").addComponent(builder.newComponent("SizeBasedTriggeringPolicy").addAttribute("size", LOG_SIZE));
> 
> builder.add(builder.newFilter("ThresholdFilter", Filter.Result.ACCEPT, Filter.Result.NEUTRAL).addAttribute("level", Level.INFO));
> AppenderComponentBuilder mainLogBuilder = builder
>         .newAppender("mainLog", "RollingFile")
>         .addAttribute("fileName", "log/main.log")
>         .addAttribute("filePattern", "log/main.%i.log")
>         .add(layoutBuilder)
>         .addComponent(triggeringRollFile);
> 
> But I want to set the max number of backup files to 5. How do I do this?
> 
> Thanks in advance
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
> For additional commands, e-mail: log4j-user-help@logging.apache.org
> 
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org