Java TimeBasedFileNamingAndTriggeringPolicyBase-class And Method Code Example


Here is an example of a class extending TimeBasedFileNamingAndTriggeringPolicyBase in Logback:

import ch.qos.logback.core.rolling.TimeBasedFileNamingAndTriggeringPolicyBase;
import ch.qos.logback.core.rolling.TimeBasedRollingPolicy;

public class MyTimeBasedFileNamingAndTriggeringPolicy extends TimeBasedFileNamingAndTriggeringPolicyBase {
    private TimeBasedRollingPolicy timeBasedRollingPolicy;

    public void setTimeBasedRollingPolicy(TimeBasedRollingPolicy timeBasedRollingPolicy) {
        this.timeBasedRollingPolicy = timeBasedRollingPolicy;
    }

    @Override
    public void start() {
        if (timeBasedRollingPolicy == null) {
            throw new IllegalStateException("timeBasedRollingPolicy must be set");
        }
        // additional initialization code
        super.start();
    }

    @Override
    public void rollover() {
        // rollover logic
    }
    
    @Override
    public String getElapsedPeriodsFileName() {
        // return the file name
    }
}

This class is an example of a custom triggering and rollover policy in Logback. The class is based on time and it is set using the setTimeBasedRollingPolicy method. The start method is overridden to perform additional initialization and to check if the timeBasedRollingPolicy has been set. The rollover method is overridden to define the specific rollover logic. Also, the getElapsedPeriodsFileName method is overridden to return the file name that is to be rolled over.

This class is extending TimeBasedFileNamingAndTriggeringPolicyBase, which is a composite triggering and rollover policy that rolls over the file when the current period of time elapses. This class also provides file naming functionality in addition to triggering functionality.