How to programmatically set a rolloverStrategy?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

How to programmatically set a rolloverStrategy?

astaldo1977@hotmail.com
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: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: How to programmatically set a rolloverStrategy?

astaldo1977@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, [hidden email] <[hidden email]> 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: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]