Java SizeAndTimeBasedFNATP-class And Method Code Example


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

import ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP;
import ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy;
import ch.qos.logback.core.rolling.TimeBasedRollingPolicy;

public class MySizeAndTimeBasedFNATP extends SizeAndTimeBasedFNATP {
    private SizeBasedTriggeringPolicy sizeBasedTriggeringPolicy;
    private TimeBasedRollingPolicy timeBasedRollingPolicy;

    public void setSizeBasedTriggeringPolicy(SizeBasedTriggeringPolicy sizeBasedTriggeringPolicy) {
        this.sizeBasedTriggeringPolicy = sizeBasedTriggeringPolicy;
    }

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

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

    @Override
    public void rollover() {
        // rollover logic
    }
}

This class is an example of a custom triggering and rollover policy in Logback. The class is based on both file size and time, and is set using the setSizeBasedTriggeringPolicy and setTimeBasedRollingPolicy methods. The start method is overridden to perform additional initialization and to check if the sizeBasedTriggeringPolicy and timeBasedRollingPolicy have been set. The rollover method is overridden to define the specific rollover logic.

Note that this class is extending SizeAndTimeBasedFNATP(FileNameAndTimestamp), which is a combination of TimeBasedRollingPolicy and SizeBasedTriggeringPolicy, this is a composite triggering and rollover policy which rollover the file when either the maximum file size is reached or the current period of time elapses.